Class ReflectionUtils

java.lang.Object
com.aspectran.utils.ReflectionUtils

public abstract class ReflectionUtils extends Object
Simple utility class for working with the reflection API.
Since:
2.0.0
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • getTypeDifferenceWeight

      public static float getTypeDifferenceWeight(@NonNull Class<?>[] paramTypes, @NonNull 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(Class<?> paramType, 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(@NonNull Class<?>[] srcArgs, @NonNull 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(Class<?> srcClass, 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 Object toPrimitiveArray(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 Object toComponentTypeArray(Object val, 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
    • getField

      public static Object getField(@NonNull Field field, 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(@NonNull Field field, Object target, 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

      @Nullable public static Object invokeMethod(Method method, @Nullable Object target)
      Invoke the specified Method against the supplied target object with no 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
      Returns:
      the invocation result, if any
      See Also:
    • invokeMethod

      @Nullable public static Object invokeMethod(@NonNull Method method, @Nullable Object target, 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