Class ReflectionUtils


  • public class ReflectionUtils
    extends java.lang.Object
    Simple utility class for working with the reflection API.
    Since:
    2.0.0
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object getField​(java.lang.reflect.Field field, java.lang.Object target)
      Get the field represented by the supplied field object on the specified target object.
      static float getTypeDifferenceWeight​(java.lang.Class<?>[] srcArgs, java.lang.Class<?>[] destArgs)
      Returns the sum of the object transformation cost for each class in the source argument list.
      static float getTypeDifferenceWeight​(java.lang.Class<?>[] paramTypes, java.lang.Object[] destArgs)
      Algorithm that judges the match between the declared parameter types of a candidate method and a specific list of arguments that this method is supposed to be invoked with.
      static float getTypeDifferenceWeight​(java.lang.Class<?> srcClass, java.lang.Class<?> destClass)
      Gets the number of steps required needed to turn the source class into the destination class.
      static float getTypeDifferenceWeight​(java.lang.Class<?> paramType, java.lang.Object destArg)
      Algorithm that judges the match between the declared parameter types of a candidate method and a specific list of arguments that this method is supposed to be invoked with.
      static java.lang.Object invokeMethod​(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)
      Invoke the specified Method against the supplied target object with the supplied arguments.
      static void setField​(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value)
      Set the field represented by the supplied field object on the specified target object to the specified value.
      static java.lang.Object toComponentTypeArray​(java.lang.Object val, java.lang.Class<?> componentType)
      Converts an array of objects to an array of the specified component type.
      static java.lang.Object toPrimitiveArray​(java.lang.Object val)
      Converts an array of objects to an array of their primitive types.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • getField

        public static java.lang.Object getField​(java.lang.reflect.Field field,
                                                java.lang.Object target)
        Get the field represented by the supplied field object on the specified target object. In accordance with Field.get(Object) semantics, the returned value is automatically wrapped if the underlying field has a primitive type.
        Parameters:
        field - the field to get
        target - the target object from which to get the field
        Returns:
        the field's current value
      • setField

        public static void setField​(java.lang.reflect.Field field,
                                    java.lang.Object target,
                                    java.lang.Object value)
        Set the field represented by the supplied field object on the specified target object to the specified value. In accordance with Field.set(Object, Object) semantics, the new value is automatically unwrapped if the underlying field has a primitive type.
        Parameters:
        field - the field to set
        target - the target object on which to set the field
        value - the value to set (may be null)
      • invokeMethod

        public static java.lang.Object invokeMethod​(java.lang.reflect.Method method,
                                                    java.lang.Object target,
                                                    java.lang.Object... args)
        Invoke the specified Method against the supplied target object with the supplied arguments. The target object can be null when invoking a static Method.
        Parameters:
        method - the method to invoke
        target - the target object to invoke the method on
        args - the invocation arguments (may be null)
        Returns:
        the invocation result, if any
      • getTypeDifferenceWeight

        public static float getTypeDifferenceWeight​(java.lang.Class<?>[] paramTypes,
                                                    java.lang.Object[] destArgs)
        Algorithm that judges the match between the declared parameter types of a candidate method and a specific list of arguments that this method is supposed to be invoked with.
        Parameters:
        paramTypes - the parameter types to match
        destArgs - the arguments to match
        Returns:
        the accumulated weight for all arguments
      • getTypeDifferenceWeight

        public static float getTypeDifferenceWeight​(java.lang.Class<?> paramType,
                                                    java.lang.Object destArg)
        Algorithm that judges the match between the declared parameter types of a candidate method and a specific list of arguments that this method is supposed to be invoked with.
        Parameters:
        paramType - the parameter type to match
        destArg - the argument to match
        Returns:
        the type difference weight
      • getTypeDifferenceWeight

        public static float getTypeDifferenceWeight​(java.lang.Class<?>[] srcArgs,
                                                    java.lang.Class<?>[] destArgs)
        Returns the sum of the object transformation cost for each class in the source argument list.
        Parameters:
        srcArgs - the source arguments
        destArgs - the destination arguments
        Returns:
        the accumulated weight for all arguments
      • getTypeDifferenceWeight

        public static float getTypeDifferenceWeight​(java.lang.Class<?> srcClass,
                                                    java.lang.Class<?> destClass)
        Gets the number of steps required needed to turn the source class into the destination class. This represents the number of steps in the object hierarchy graph.
        Parameters:
        srcClass - the source class
        destClass - the destination class
        Returns:
        the cost of transforming an object
      • toPrimitiveArray

        public static java.lang.Object toPrimitiveArray​(java.lang.Object val)
        Converts an array of objects to an array of their primitive types.
        Parameters:
        val - an array of objects to be converted, may be null
        Returns:
        an array of their primitive types
      • toComponentTypeArray

        public static java.lang.Object toComponentTypeArray​(java.lang.Object val,
                                                            java.lang.Class<?> componentType)
        Converts an array of objects to an array of the specified component type.
        Parameters:
        val - an array of objects to be converted
        componentType - the Class object representing the component type of the new array
        Returns:
        an array of the objects with the specified component type