List of utility methods to do Reflection Method Invoke
Object | invokeMethodOnObject(Object target, String methodName) Invoke an arbitrary method that takes no arguments. try { Class<?> cls = target.getClass(); while (cls != null) { Method[] allMethods = cls.getDeclaredMethods(); for (Method meth : allMethods) { if (meth.getName().equals(methodName) && meth.getParameterTypes().length == 0) { meth.setAccessible(true); return meth.invoke(target, (Object[]) null); ... |
Object | invokeMethodQuietly(Object receiver, Method method, Object... args) invoke Method Quietly try { return method.invoke(receiver, args); } catch (IllegalAccessException ex) { return null; } catch (InvocationTargetException ex) { return null; |
List | invokeMethods(final Object target, final List Invoke the following list of methods (with no parameter) and return the result in a List . final List<Object> results = new ArrayList<>(methods.size()); for (final Method method : methods) { results.add(method.invoke(target, (Object[]) null)); return results; |
void | invokeMethodsWithAnnotation(final Class extends Annotation> annotation, final Object instance, final Object... args) Invokes all methods on the given instance that have been annotated with the given Annotation. for (final Method method : instance.getClass().getMethods()) { if (method.isAnnotationPresent(annotation)) { final boolean isAccessible = method.isAccessible(); method.setAccessible(true); try { final Class<?>[] argumentTypes = method.getParameterTypes(); if (argumentTypes.length > args.length) { throw new IllegalArgumentException(String.format( ... |
Object | invokeMethodWithArray2(Object target, Method method, Object[] args) invoke Method With Array try { return method.invoke(target, args); } catch (IllegalAccessException e) { throw new RuntimeException(e); |
Object | invokeMethodWithKnownParamTypes(Object object, String methodName, Class[] methodParamTypes, Object... args) invoke Method With Known Param Types Throwable t = null; try { Class clazz = object.getClass(); Method setter = clazz.getMethod(methodName, methodParamTypes); return setter.invoke(object, args); } catch (NoSuchMethodException e) { t = e; e.printStackTrace(); ... |
Object | invokeMethodWithNoArgs(Object o, Method method) invoke Method With No Args try { return method.invoke(o); } catch (InvocationTargetException | IllegalAccessException e) { return null; |
boolean | invokeMethodWithoutException(Object object, String methodName, Class>[] parameterTypes, Object[] parameters) Invoke a method. try { invokeMethod(object, methodName, parameterTypes, parameters); return true; } catch (SecurityException e) { } catch (IllegalArgumentException e) { } catch (NoSuchMethodException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { ... |