org.jppf.utils
Class ReflectionHelper

java.lang.Object
  extended by org.jppf.utils.ReflectionHelper

public final class ReflectionHelper
extends Object

Collection of utility methods to facilitate the use of reflection.

Author:
Laurent Cohen

Constructor Summary
ReflectionHelper()
           
 
Method Summary
static Class getClass0(String className)
          Transform a class name into a Class object.
static Class[] getClasses(String... classNames)
          Transform an array of class names into an array of Class objects.
static ClassLoader getCurrentClassLoader()
          Returns the current thread's context class loader, or this class's class loader if it is null.
static Object getField(Class clazz, Object instance, String fieldName)
          Get the value of the field of a specified class.
static Object invokeConstructor(Class<?> clazz, Class<?>[] paramTypes, Object... values)
          Invoke a constructor using reflection.
static Object invokeMethod(Class<?> clazz, Object instance, String methodName, Class<?>[] paramTypes, Object... values)
          Invoke a method using reflection.
static Object invokeMethod(Class clazz, Object instance, String methodName)
          Convenience method to invoke a method with no parameter.
static Object invokeMethod(Class clazz, Object instance, String methodName, Object... values)
          Invoke a method using reflection, without having to specify the parameters types.
static Object newInstance(String className)
          Invoke a default constructor using reflection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionHelper

public ReflectionHelper()
Method Detail

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 JPPFException if 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 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 JPPFException if the invocation failed.

invokeMethod

public static Object invokeMethod(Class clazz,
                                  Object instance,
                                  String methodName)
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 JPPFException if the invocation failed.

newInstance

public static Object newInstance(String className)
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 JPPFException if the instantiation failed.

invokeConstructor

public static Object invokeConstructor(Class<?> clazz,
                                       Class<?>[] paramTypes,
                                       Object... values)
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 JPPFException if the invocation failed.

getField

public static Object getField(Class clazz,
                              Object instance,
                              String fieldName)
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 JPPFException if the invocation failed.

getClasses

public static Class[] getClasses(String... classNames)
Transform an array of class names into an array of Class objects.

Parameters:
classNames - the names of the classes to find.
Returns:
n array of Class objects, or null if one of the classes could not be found.

getClass0

public static Class getClass0(String className)
Transform a class name into a Class object.

Parameters:
className - the name of the class to find.
Returns:
n array of Class objects, or null if one of the classes could not be found.

getCurrentClassLoader

public static ClassLoader getCurrentClassLoader()
Returns the current thread's context class loader, or this class's class loader if it is null.

Returns:
a ClassLoader instance.


Copyright © 2005-2010 JPPF Team.