Description
Convenience wrapper to reflection method invoke API.
License
Open Source License
Parameter
Parameter | Description |
---|
target | object to invoke the method on (or null for static methods) |
clazz | class name |
methodName | method name |
parameterTypes | parameter types to resolve method name |
args | actual arguments |
Exception
Parameter | Description |
---|
IllegalArgumentException | if method not found |
IllegalStateException | for InvocationTargetException (exception in invoked method) |
Return
invocation result or null
Declaration
public static Object invoke(Object target, Class<?> clazz, String methodName, Class[] parameterTypes,
Object... args)
Method Source Code
//package com.java2s;
//License from project: Open Source License
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
public class Main {
/**/* w w w. j a va 2s.c o m*/
* Convenience wrapper to reflection method invoke API. Invoke the method and hide checked exceptions.
*
* @param target object to invoke the method on (or null for static methods)
* @param clazz class name
* @param methodName method name
* @param parameterTypes parameter types to resolve method name
* @param args actual arguments
* @return invocation result or null
* @throws IllegalArgumentException if method not found
* @throws IllegalStateException for InvocationTargetException (exception in invoked method)
*/
public static Object invoke(Object target, Class<?> clazz, String methodName, Class[] parameterTypes,
Object... args) {
try {
Method method = null;
try {
method = clazz.getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
method = clazz.getDeclaredMethod(methodName, parameterTypes);
}
method.setAccessible(true);
return method.invoke(target, args);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Illegal arguments method %s.%s(%s) on %s, params %s",
clazz.getName(), methodName, Arrays.toString(parameterTypes), target, Arrays.toString(args)),
e);
} catch (InvocationTargetException e) {
throw new IllegalStateException(String.format("Error invoking method %s.%s(%s) on %s, params %s",
clazz.getName(), methodName, Arrays.toString(parameterTypes), target, Arrays.toString(args)),
e);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(String.format("No such method %s.%s(%s) on %s, params %s",
clazz.getName(), methodName, Arrays.toString(parameterTypes), target, Arrays.toString(args)),
e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(String.format("No such method %s.%s(%s) on %s, params %s",
clazz.getName(), methodName, Arrays.toString(parameterTypes), target, Arrays.toString(args)),
e);
}
}
}
Related
- invoke(Object object, String methodName, Object[] args)
- invoke(Object objToInvoke, Class> classToInvoke, String method, Class[] argumentClasses, Object[] arguments)
- invoke(Object owner, String methodName)
- invoke(Object proxy, java.lang.reflect.Method method, Object[] args)
- invoke(Object source, String key)
- invoke(Object target, Class clazz, String method)
- invoke(Object target, Method method)
- invoke(Object target, String methodName)
- invoke(Object target, String methodName, Object... args)