List of utility methods to do Reflection Method Invoke
Object | invokeMethodAndGetRecursively(Object object, String methodName, Object... args) Invokes and returns result of method with given name and arguments of target object's class or one of it's superclasses. Class clazz = object.getClass(); boolean ended = false; do { for (Method m : clazz.getDeclaredMethods()) if (m.getName().equalsIgnoreCase(methodName)) { ended = true; m.setAccessible(true); try { ... |
Object | invokeMethodAndReturn(Class> clazz, String method, Object object) invoke Method And Return Object value = null; try { Method methodToInvoke = getMethod(clazz, method); methodToInvoke.setAccessible(true); value = methodToInvoke.invoke(object); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { ... |
void | invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg) Do method from superclass by name. Method method = object.getClass().getDeclaredMethod(name, typeArg); method.setAccessible(true); method.invoke(object, arg); |
Object | invokeMethodByName(final Object obj, final String methodName, final Object[] args) invoke Method By Name Method method = getAccessibleMethodByName(obj, methodName); if (method == null) { throw new IllegalArgumentException( "Could not find MethodDefine [" + methodName + "] on target [" + obj + "]"); try { return method.invoke(obj, args); } catch (Exception e) { ... |
Object | invokeMethodHandleException(Method method, Object... args) Invokes given method with given arguments. try { method.setAccessible(true); if (Modifier.isStatic(method.getModifiers())) { return method.invoke(null, args); } else { Object this_ = args[0]; Object[] newArgs = new Object[args.length - 1]; System.arraycopy(args, 1, newArgs, 0, args.length - 1); ... |
Object | invokeMethodNoArgs(final Object obj, final String methodName, final Class>... returnTypePreference) Invoke a method without arguments, get the method matching the return types best, i.e. final Class<?> objClass = obj.getClass(); Method methodFound = getMethodNoArgs(objClass, methodName, returnTypePreference); if (methodFound == null) { methodFound = seekMethodNoArgs(objClass, methodName, returnTypePreference); if (methodFound != null) { try { final Object res = methodFound.invoke(obj); ... |
Object | invokeMethodNoArgs(Object target, String methodName) invoke Method No Args try { Method method = target.getClass().getDeclaredMethod(methodName); method.setAccessible(true); Object result = method.invoke(target); return result; } catch (Exception e) { throw new RuntimeException(e); |
Object | invokeMethodOfClass(Class target, Method method) invoke Method Of Class return invokeMethod(target.newInstance(), method, null);
|
void | invokeMethodOnLoader(ClassLoader cl, String methodName, Object... params) invoke Method On Loader Class<?>[] paramClasses = new Class<?>[params.length]; for (int i = 0; i != params.length; i++) { paramClasses[i] = params[i].getClass(); Method method = cl.getClass().getMethod(methodName, paramClasses); method.invoke(cl, params); |
Object | invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args) This method uses reflection to invoke the given Method on the passed in Object instance. try { return method.invoke(objectToInvokeOn, args); } catch (InvocationTargetException invocationException) { throw invocationException.getTargetException(); |