List of utility methods to do Method Call
T | invoke(@Nonnull Object source, @Nonnull String name, Object... args) invoke Class<?>[] argTypes = new Class<?>[args.length]; for (int i = 0; i < args.length; i++) { argTypes[i] = args[i] == null ? Object.class : args[i].getClass(); final Method method = findMethod(source.getClass(), name, argTypes); if (method == null) { throw new IllegalArgumentException(String.format("Unable to find '%s' with '%s' on '%s'!", name, Arrays.toString(argTypes), source.getClass())); ... |
RT | invoke(boolean makeAccessible, T object, Class super T> methodClass, String methodName, Class>[] paramTypes, Object... params) Invokes a method by means of reflection. try { if (object == null) throw new NullPointerException("Cannot invoke the method '" + methodName + "' over a null object"); Method setPropM = methodClass.getDeclaredMethod(methodName, paramTypes); if (makeAccessible) setPropM.setAccessible(true); return (RT) setPropM.invoke(object, params); } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException ... |
Object | invoke(Class c, Object obj, String method, Class[] paramClasses, Object[] params) Invokes accessible method of an object. Method m = c.getMethod(method, paramClasses);
return m.invoke(obj, params);
|
Object | invoke(Class targetClass, Object target, String methodName, Class paramType0, Object paramValue0) call method by reflection this method allows to call protected and private methods; use it with care... Class[] paramTypes = new Class[1]; Object[] paramValues = new Object[1]; paramTypes[0] = paramType0; paramValues[0] = paramValue0; return invoke(targetClass, target, methodName, paramTypes, paramValues); |
void | invoke(Class extends Annotation> annotationClass, Object instance, Object[] parameters) Invokes the method of the object instance annotated with the specified annotation. Class<?> instanceClass = instance.getClass(); Method[] methods = instanceClass.getMethods(); for (Method method : methods) { if (method.isAnnotationPresent(annotationClass)) { method.setAccessible(true); try { method.invoke(instance, parameters); } catch (Exception e) { ... |
Object | invoke(Class> clazz, Object obj, String methodName, Object... args) invoke try { Method method = findMethod(clazz, methodName); method.setAccessible(true); Object result = method.invoke(obj, args); return result; } catch (Throwable e) { throw new RuntimeException("invoke", e); |
T | invoke(Class> clazz, String methodName, Class>[] parameterTypes, Object instance, Object[] args, Class invoke try { Method method = clazz.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); return returnType.cast(method.invoke(instance, args)); } catch (Exception e) { throw new IllegalStateException(e); |
Object | invoke(Class> clazz, String methodName, Object instance, Class>[] signature, Object... args) Reflectively invokes a method boolean mod = false; Method m = null; try { try { m = clazz.getDeclaredMethod(methodName, signature == null ? NO_ARG_SIG : signature); } catch (NoSuchMethodException e) { m = clazz.getMethod(methodName, signature == null ? NO_ARG_SIG : signature); if (!m.isAccessible()) { m.setAccessible(true); mod = true; return m.invoke(instance, args); } catch (Exception ex) { throw new RuntimeException(ex); } finally { if (mod && m != null) { m.setAccessible(false); |
Object | invoke(Class> cls, String methodName, Object... parameter) invoke Class<?>[] parameterTypes = new Class<?>[parameter.length]; for (int i = 0; i < parameterTypes.length; i++) { parameterTypes[i] = parameter[i].getClass(); try { return getMethod(cls, methodName, parameterTypes).invoke(null, parameter); } catch (Exception e) { e.printStackTrace(); ... |
T | invoke(Class Invokes the given method on the given object with the given arguments. try { return (T) m.invoke(obj, args); } catch (IllegalAccessException e) { throw new RuntimeException(e); |