Class ReflectionUtils

java.lang.Object
org.jppf.utils.ReflectionUtils

public class ReflectionUtils extends Object
Collection of static utility methods for dealing with reflection-based APIs.
Author:
Laurent Cohen
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • dumpObject

      public static String dumpObject(Object o, String... names)
      Generates a string that displays all return values for all getters of an object. This method uses a new line character a field separator.
      Parameters:
      o - the object whose getter return values are to be dumped into a string.
      names - the names of the attributes to dump. If null or empty, then all attributes that have a getter, except "class", are dumped. This parameter is used as a filter on the attributes to dump and defines the order in which the fields are dumped.
      Returns:
      a string with the classname, hashcode, and the value of each attribute that has a corresponding getter.
    • dumpObject

      public static String dumpObject(Object o, String separator, boolean displaySimpleClassName, boolean displayHashCode, String... names)
      Generates a string that displays all return values for all getters of an object.
      Parameters:
      o - the object whose getter return values are to be dumped into a string.
      separator - separator between field values, like a comma or a new line.
      displaySimpleClassName - true to use the object's class simple name, false for the fully qualified name.
      displayHashCode - whether to append '@hashcode_value' to the name of the class.
      names - the names of the attributes to dump. If null or empty, then all attributes that have a getter, except "class", are dumped. This parameter is used as a filter on the attributes to dump and defines the order in which the fields are dumped.
      Returns:
      a string with the classname, hashcode, and the value of each attribute that has a corresponding getter.
    • getMBeanAttributeName

      public static String getMBeanAttributeName(Method meth)
      Get the name of an MBean attribute based on a getter or setter method.
      Parameters:
      meth - the method to analyse.
      Returns:
      the name of an MBean attribute following the javax.management coventions, or null if the input method is not a setter or getter.
    • getMBeanAttributeName

      public static String getMBeanAttributeName(String name)
      Get the name of an MBean attribute based on the name of a getter or setter method.
      Parameters:
      name - the name of the method to analyse.
      Returns:
      the name of an MBean attribute following the javax.management coventions, or null if the input method is not a setter or getter.
    • isGetter

      public static boolean isGetter(Method meth)
      Determines whether a method is a getter (accessor), according to Sun's naming conventions.
      Parameters:
      meth - the method to analyse.
      Returns:
      true if the method is a getter, false otherwise.
    • isSetter

      public static boolean isSetter(Method meth)
      Determines whether a method is a setter (mutator), according to Sun's naming conventions.
      Parameters:
      meth - the method to analyse.
      Returns:
      true if the method is a setter, false otherwise.
    • getGetter

      public static Method getGetter(Class<?> clazz, String name)
      Get a getter with a given name from a class.
      Parameters:
      clazz - the class enclosing the getter.
      name - the name of the getter to look for.
      Returns:
      a Method object, or null if the class has no getter with the specified name.
    • getSetter

      public static Method getSetter(Class<?> clazz, String name)
      Get a setter with a given name from a class.
      Parameters:
      clazz - the class enclosing the setter.
      name - the name of the setter to look for.
      Returns:
      a Method object, or null if the class has no setter with the specified name.
    • getGetterForAttribute

      public static Method getGetterForAttribute(Class<?> clazz, String attrName)
      Get a getter corresponding to a specified instance variable name from a class.
      Parameters:
      clazz - the class enclosing the instance variable.
      attrName - the name of the instance variable to look for.
      Returns:
      a Method object, or null if the class has no getter for the specified instance variable name.
    • getSetterForAttribute

      public static Method getSetterForAttribute(Class<?> clazz, String attrName)
      Get a setter corresponding to a specified instance variable name from a class.
      Parameters:
      clazz - the class enclosing the instance variable.
      attrName - the name of the instance variable to look for.
      Returns:
      a Method object, or null if the class has no setter for the specified instance variable name.
    • getAllBeanMethods

      public static Method[] getAllBeanMethods(Class<?> clazz, boolean getters)
      Obtain all the getters or setters of a specified class.
      Parameters:
      clazz - the class to get the methods from.
      getters - if true, indicates that the getters should be looked up, otherwise it should be the setters.
      Returns:
      an array of Method instances.
    • deepCopy

      public static Object deepCopy(Object o)
      Perform a deep copy of an object. This method uses reflection to perform the copy through public accessors and mutators (getters and setters).
      Parameters:
      o - the object to copy.
      Returns:
      an object whose state is a copy of that of the input object.
    • getMatchingMethod

      public static Method getMatchingMethod(Class<?> clazz, String name, Object[] args)
      Get the method with the specified name, in the specified declaring class, that matches the number, order and types of the specified arguments.
      Parameters:
      clazz - the class in which to look for the method.
      name - the name of the method to look for.
      args - the arguments of the method.
      Returns:
      a matching Method instance, or null if no match could be found.
    • getMatchingConstructor

      public static Constructor<?> getMatchingConstructor(Class<?> clazz, Object[] args)
      Get the constructor with in the specified declaring class, that matches the number, order and types of the specified arguments.
      Parameters:
      clazz - the class in which to look for the constructor.
      args - the arguments of the method.
      Returns:
      a matching Constructor instance, or null if no match could be found.
    • matchingTypes

      public static boolean matchingTypes(Class<?>[] argTypes, Class<?>[] types)
      Determine whether a set of (possibly null) types loosely matches another set of types.
      Parameters:
      argTypes - the types to match, null values are considered wildcards (matching any type).
      types - the set of types to match.
      Returns:
      true if the methods match, false otherwise.
    • mapPrimitveType

      public static Class<?> mapPrimitveType(Class<?> type)
      Map a primitive type to its corresponding wrapper type.
      Parameters:
      type - a primitive type.
      Returns:
      a Class instance.
    • createTypeArray

      public static Class<?>[] createTypeArray(Object[] args)
      Generate an array of the types of the specified arguments. If the array of arguments is null this method will return an empty array.
      Parameters:
      args - the arguments to get the types from.
      Returns:
      an array of Class instances.
    • getCurrentMethodName

      public static String getCurrentMethodName()
      Get the name of the method that called this one.
      Returns:
      the name of the invoking method as a string.
    • getCurrentClassAndMethod

      public static String getCurrentClassAndMethod()
      Get the name of the method that called this one.
      Returns:
      the name of the invoking method as a string.
    • getCurrentClassName

      public static String getCurrentClassName()
      Get the name of the current class in the call stack.
      Returns:
      the name of the invoking method as a string.
    • simpleDump

      public static String simpleDump(Object o)
      Returns a string in format simple_class_name@identity_hashcode from the specified object.
      Parameters:
      o - the object to dump.
      Returns:
      a string representing the specified object, or "null" if the object is null.
    • sameSignature

      public static boolean sameSignature(Method method1, Method method2)
      Determine whether two methids have the same signature.
      Parameters:
      method1 - the first method to compare.
      method2 - the second method to compare.
      Returns:
      true if the methods have the same signature, false otherwise.