Description
get Method Return
License
Open Source License
Parameter
Parameter | Description |
---|
className | a parameter |
methodName | a parameter |
params | a parameter |
args | a parameter |
isStatic | a parameter |
Exception
Parameter | Description |
---|
ClassNotFoundException | an exception |
NoSuchMethodException | an exception |
SecurityException | an exception |
InstantiationException | an exception |
IllegalAccessException | an exception |
IllegalArgumentException | an exception |
InvocationTargetException | an exception |
Return
Returns the Method object from the specified class name, method name, parameters of this method and if this method is static.
Declaration
public static Object getMethodReturn(String className, String methodName, Class<?>[] params, Object[] args,
boolean isStatic) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
Method Source Code
//package com.java2s;
//License from project: Open Source License
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
/**//from ww w .j a va 2 s . c o m
*
* @param className
* @param methodName
* @param params
* @param args
* @param isStatic
*
* @return Returns the Method object from the specified class name, method name, parameters of this method and if this method is static.
*
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static Object getMethodReturn(String className, String methodName, Class<?>[] params, Object[] args,
boolean isStatic) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> clazz = Class.forName(className);
Object instance = clazz.newInstance();
Method method = clazz.getMethod(methodName, params);
if (isStatic) {
return method.invoke(null, args);
} else {
return method.invoke(instance, args);
}
}
/**
*
* @param className
* @param methodName
* @param params
*
* @return Returns the Method object from the specified class name, method name and parameters of this method.
*
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static Method getMethod(String className, String methodName, Class<?>[] params)
throws ClassNotFoundException, NoSuchMethodException, SecurityException {
Class<?> clazz = Class.forName(className);
return clazz.getMethod(methodName, params);
}
}
Related
- getMethodNames(Object obj, boolean hasParent)
- getMethodNames(Object obj, boolean includeInheritedMethods)
- getMethodObject(Class extends T> type, Class> clazz, String method, Class>[] args, Object object, Object[] objects)
- getMethodObject(Object object, String method, Object[] parametre)
- getMethodResult(final Object element, final String methodName)
- getMethodReturnTypeGeneric(Object source, Method method)
- getMethods(Class> objectClass, Class extends Annotation> annotationClass)
- getMethods(Object instance, String name, Class>[] arguments, boolean isPrefix)
- getMethods(Object obj, boolean hasParent)