Class ReflectionHelper

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

public final class ReflectionHelper extends Object
Collection of utility methods to facilitate the use of reflection.
Author:
Laurent Cohen
  • 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 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, 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 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.
    • findMethod

      public static Method findMethod(Class<?> clazz, String methodName, int nbArgs) throws Exception
      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

      public static Method findMethod(Class<?> clazz, String methodName) throws Exception
      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

      public static Method findMethodAnyArgs(Class<?> clazz, String methodName) throws Exception
      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

      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.
    • newInstance

      public static Object newInstance(Class<?> clazz)
      Invoke a default constructor using reflection.
      Parameters:
      clazz - 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.
    • getField

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

      public static Object getField(String className, Object instance, String fieldName)
      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 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:
      a Class, or null if the classe 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.
    • 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

      public static Method findMethod(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.
      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 null or null if no method matches.
    • findConstructor

      public static Constructor<?> findConstructor(Class<?> clazz, Class<?>... paramTypes)
      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 null or 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 null or 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 null or 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.