Package org.jppf.utils
Class ReflectionHelper
java.lang.Object
org.jppf.utils.ReflectionHelper
Collection of utility methods to facilitate the use of reflection.
- Author:
- Laurent Cohen
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Constructor<?>findConstructor(Class<?> clazz, Class<?>... paramTypes) Find the specified public constructor in the specified class.static Constructor<?>findConstructor(String className, String... paramTypeNames) Find the specified public constructor in the specified class.static MethodfindMethod(Class<?> clazz, String methodName) Find a method without having to specify the parameters types.static MethodfindMethod(Class<?> clazz, String methodName, int nbArgs) Find a method without having to specify the parameters types.static MethodfindMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) Find the specified public method in the specified class or one of its implemented interfaces or in its super class hierarchy.static MethodfindMethod(String className, String methodName, String... paramTypeNames) Find the specified public method in the specified class or one of its implemented interfaces or in its super class hierarchy.static MethodfindMethodAnyArgs(Class<?> clazz, String methodName) Find a method without having to specify the parameters types.static MethodfindMethodFromConcreteArgs(Class<?> clazz, String methodName, Object... params) static Class<?>Transform a class name into aClassobject.static Class<?>[]getClasses(String... classNames) Transform an array of class names into an array ofClassobjects.static ClassLoaderReturns the current thread's context class loader, or this class's class loader if it is null.static ObjectGet the value of the field of a specified class.static ObjectGet the value of the specified field for a specified class and specified instance.static ObjectGet the value of the specified static field for a specified class.static ObjectinvokeConstructor(Class<?> clazz, Class<?>[] paramTypes, Object... values) Invoke a constructor using reflection.static <T> TinvokeDefaultOrStringArrayConstructor(Class<T> inf, String prop, String[] value) Instantiate an object that implements the specified interface, whose class name is given by the specified property, by calling either its default constructor or a oonstrcutor that take a String[].static ObjectinvokeMethod(Class<?> clazz, Object instance, String methodName) Convenience method to invoke a method with no parameter.static ObjectinvokeMethod(Class<?> clazz, Object instance, String methodName, Class<?>[] paramTypes, Object... values) Invoke a method using reflection.static ObjectinvokeMethod(Class<?> clazz, Object instance, String methodName, Object... values) Invoke a method using reflection, without having to specify the parameters types.static ObjectnewInstance(Class<?> clazz) Invoke a default constructor using reflection.static ObjectnewInstance(String className) Invoke a default constructor using reflection.
-
Constructor Details
-
ReflectionHelper
public ReflectionHelper()
-
-
Method Details
-
invokeMethod
public static Object invokeMethod(Class<?> clazz, Object instance, String methodName, Class<?>[] paramTypes, Object... values) Invoke a method using reflection.- Parameters:
clazz- the class on which to invoke the method.instance- the object on which to invoke the method, may be null if the method is static.methodName- the name of the method to invoke.paramTypes- the types of the method's parameters, may be null if no parameters.values- the values of the method's parameters, may be null if no parameters.- Returns:
- the result of the method's invocation, or null if the method's return type is void,
or a
JPPFExceptionif the invocation failed.
-
invokeMethod
public static Object invokeMethod(Class<?> clazz, Object instance, String methodName, Object... values) Invoke a method using reflection, without having to specify the parameters types. In this case, we assume the first method found with the specified name, and whose number of parameter is the same as the number of values, is the one we use.- Parameters:
clazz- the class on which to invoke the method.instance- the object on which to invoke the method, may be null if the method is static.methodName- the name of the method to invoke.values- the values of the method's parameters, may be null if no parameters.- Returns:
- the result of the method's invocation, or null if the method's return type is void,
or a
JPPFExceptionif the invocation failed.
-
invokeMethod
Convenience method to invoke a method with no parameter.- Parameters:
clazz- the class on which to invoke the method.instance- the object on which to invoke the method, may be null if the method is static.methodName- the name of the method to invoke.- Returns:
- the result of the method's invocation, or null if the method's return type is void,
or a
JPPFExceptionif the invocation failed.
-
findMethod
Find a method without having to specify the parameters types. In this case, we assume the first method found with the specified name, and whose number of parameter is the same as the number of values, is the one we use.- Parameters:
clazz- the class on which to invoke the method.methodName- the name of the method to find.nbArgs- the of parameters in the method, may be zero if no parameters.- Returns:
- the method with the specified name and number of parameters.
- Throws:
Exception- if any erorr occurs.
-
findMethod
Find a method without having to specify the parameters types. In this case, we assume the first method found with the specified name and zero arugments is the one we use.- Parameters:
clazz- the class on which to invoke the method.methodName- the name of the method to find.- Returns:
- the method with the specified name and no parameters.
- Throws:
Exception- if any erorr occurs.
-
findMethodAnyArgs
Find a method without having to specify the parameters types. In this case, we assume the first method found with the specified name is the one we use.- Parameters:
clazz- the class on which to invoke the method.methodName- the name of the method to find.- Returns:
- the method with the specified name and number of parameters.
- Throws:
Exception- if any erorr occurs.
-
newInstance
Invoke a default constructor using reflection.- Parameters:
className- the name of the class to instantiate.- Returns:
- an instance of the class whose name is specified, or a
JPPFExceptionif the instantiation failed.
-
newInstance
Invoke a default constructor using reflection.- Parameters:
clazz- the class to instantiate.- Returns:
- an instance of the class whose name is specified, or a
JPPFExceptionif the instantiation failed.
-
invokeConstructor
Invoke a constructor using reflection.- Parameters:
clazz- the class on which to invoke the constructor.paramTypes- the types of the constructor's parameters, may be null if no parameters.values- the values of the constructor's parameters, may be null if no parameters.- Returns:
- the result of the constructor's invocation, or a
JPPFExceptionif the invocation failed.
-
getField
Get the value of the field of a specified class.- Parameters:
clazz- the class declaring the field.instance- the class instance for which to get the field's value, may be null if the field is static.fieldName- the name of the field to get the value of.- Returns:
- the value of the field, or a
JPPFExceptionif the invocation failed.
-
getField
Get the value of the specified static field for a specified class.- Parameters:
className- the class declaring the field.fieldName- the name of the field to get the value of.- Returns:
- the value of the field, or a
JPPFExceptionif the invocation failed.
-
getField
Get the value of the specified field for a specified class and specified instance.- Parameters:
className- the class declaring the field.instance- the class instance for which to get the field's value, may be null if the field is static.fieldName- the name of the field to get the value of.- Returns:
- the value of the field, or a
JPPFExceptionif the invocation failed.
-
getClasses
Transform an array of class names into an array ofClassobjects.- Parameters:
classNames- the names of the classes to find.- Returns:
- n array of
Classobjects, or null if one of the classes could not be found.
-
getClass0
Transform a class name into aClassobject.- Parameters:
className- the name of the class to find.- Returns:
- a
Class, or null if the classe could not be found.
-
getCurrentClassLoader
Returns the current thread's context class loader, or this class's class loader if it is null.- Returns:
- a
ClassLoaderinstance.
-
findMethodFromConcreteArgs
public static Method findMethodFromConcreteArgs(Class<?> clazz, String methodName, Object... params) throws Exception - Parameters:
clazz- the class on which to find the method.methodName- the name of the method to find.params- the concrete parameters. A best effort is made to try and match the specified parameters to the formal parameters of an existing method.- Returns:
- a method that matches the name and parameters.
- Throws:
Exception- if any erorr occurs.
-
findMethod
Find the specified public method in the specified class or one of its implemented interfaces or in its super class hierarchy.- Parameters:
clazz- the class for which to find the method.methodName- the name of the method to find.paramTypes- the types of the parameters of the method, if any.- Returns:
- the matching method, or
nullor null if no method matches.
-
findConstructor
Find the specified public constructor in the specified class.- Parameters:
clazz- the class for which to find the constructor.paramTypes- the types of the parameters of the constructor, if any.- Returns:
- the matching method, or
nullor null if no method matches.
-
findMethod
public static Method findMethod(String className, String methodName, String... paramTypeNames) throws Exception Find the specified public method in the specified class or one of its implemented interfaces or in its super class hierarchy.- Parameters:
className- the class for which to find the method.methodName- the name of the method to find.paramTypeNames- the types of the parameters of the method, if any.- Returns:
- the matching method, or
nullor null if no method matches. - Throws:
Exception- if any error occurs.
-
findConstructor
public static Constructor<?> findConstructor(String className, String... paramTypeNames) throws Exception Find the specified public constructor in the specified class.- Parameters:
className- the class for which to find the constructor.paramTypeNames- the types of the parameters of the constructor, if any.- Returns:
- the matching method, or
nullor null if no method matches. - Throws:
Exception- if any error occurs.
-
invokeDefaultOrStringArrayConstructor
public static <T> T invokeDefaultOrStringArrayConstructor(Class<T> inf, String prop, String[] value) Instantiate an object that implements the specified interface, whose class name is given by the specified property, by calling either its default constructor or a oonstrcutor that take a String[].- Type Parameters:
T- the type of the interface.- Parameters:
inf- the interface that must be implemented by the class.prop- the name of the property that provides the class name and optional parameters for its String[] constructor, along with an optional default implementation of the interface.value- the value of the property.- Returns:
- an instance of the specified interface.
- Throws:
Exception- if any error occurs.
-