List of utility methods to do Reflection Method Invoke
Object | invokeMethod(Object source, String method, Collection invoke Method Collection<Class<?>> argumentClasses = new ArrayList<Class<?>>(); for (Object argument : arguments) { argumentClasses.add(argument.getClass()); if (source == null) return null; for (Method m : source.getClass().getMethods()) { if (m.getName().equalsIgnoreCase(method)) { ... |
Object | invokeMethod(Object sourceObject, String methodName, Object... arguments) invoke Method Object returnValue; Method[] methodArray = sourceObject.getClass().getDeclaredMethods(); Method metodo = null; for (Method m : methodArray) { if (m.getName().equals(methodName)) { metodo = m; if (metodo == null) { throw new RuntimeException("Couldn't find methos."); metodo.setAccessible(true); try { returnValue = metodo.invoke(sourceObject, arguments); } catch (Exception e) { throw new RuntimeException("Couldn't invoke method: " + methodName, e); return returnValue; |
Object | invokeMethod(Object target, Class> clazz, String methodName, Class[] types, Object[] args) Invoke method. final Method m = findMethod(clazz, methodName, types); try { return m.invoke(target, args); } catch (Exception e) { throw new RuntimeException(e); |
Object | invokeMethod(Object target, Method method) invoke Method if (method.isAccessible()) { return method.invoke(target); } else { method.setAccessible(true); Object rtn = method.invoke(target); method.setAccessible(false); return rtn; |
Object | invokeMethod(Object target, Method method) invoke Method return invokeMethod(target, method, null);
|
Object | invokeMethod(Object target, Method method, Object... args) invoke Method try { return method.invoke(target, args); } catch (Exception e) { throw new RuntimeException( String.format("target: %s method: %s args: %s", target, method, Arrays.toString(args)), e); |
void | invokeMethod(Object target, Method method, Object[] args) invoke Method try { method.setAccessible(true); method.invoke(target, args); } catch (IllegalAccessException ex) { throw new RuntimeException( "Unable to invoke method '" + method.getName() + "' for class " + target.getClass().getName(), ex); } catch (IllegalArgumentException ex) { ... |
Object | invokeMethod(Object target, String method) invokes the given method to the given target using reflection. return getMethodInvoker(method).apply(target);
|
Object | invokeMethod(Object target, String methodName, Object... parameters) invoke Method try { final Method method = extractMethod(target, methodName); method.setAccessible(true); return method.invoke(target, parameters); } catch (Exception e) { throw new RuntimeException("Could not invoke method.", e); |
Object | invokeMethod(Object target, String methodName, Object[] arguments) invoke Method Class targetClass = target.getClass(); Class[] argumentTypes = new Class[arguments.length]; for (int i = 0; i < argumentTypes.length; i++) { argumentTypes[i] = arguments[i].getClass(); try { Method method = targetClass.getMethod(methodName, argumentTypes); return method.invoke(target, arguments); ... |