public Object invokeMethod(String name, Class<?>[] parameterTypes, Object[] args) throws InvocationException { try { Method method = cls.getDeclaredMethod(name, parameterTypes); if (method == null) { throw new InvocationException("Unable to invoke method '%s' on class '%s': method not found", name, cls); } return method.invoke(target, args); } catch (InvocationException e) { throw e; } catch (Exception e) { throw new InvocationException(e, "Unable to invoke method '%s' on class '%s'", name, cls); } } }
public boolean invokeBooleanMethod(String name) throws InvocationException { Boolean result = invokeMethod(name, Boolean.class); if (result == null) { throw new InvocationException("Unable to invoke method '%s' on class '%s': null returned", name, cls); } return result; }
@SuppressWarnings("unchecked") public <T> T invokeMethod(String name, Class<?>[] parameterTypes, Object[] args, Class<? extends T> cls) throws InvocationException { Object result = invokeMethod(name, parameterTypes, args); if (result != null && !cls.isInstance(result)) { throw new InvocationException("Unable to invoke method '%s' on class '%s': mismatch return type '%s'", name, cls, result.getClass()); } return (T) result; }
public static Invocation fromClass(String name) throws InvocationException { if (name == null) { throw new IllegalArgumentException("Class name is null"); } Class<?> cls = RuntimeUtils.classForName(name); if (cls == null) { throw new InvocationException("Unable to find class '%s'", name); } return fromClass(cls); }