Package com.aspectran.core.util
Class ReflectionUtils
- java.lang.Object
-
- com.aspectran.core.util.ReflectionUtils
-
public class ReflectionUtils extends java.lang.ObjectSimple 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.ObjectgetField(java.lang.reflect.Field field, java.lang.Object target)Get the field represented by the suppliedfield objecton the specifiedtarget object.static floatgetTypeDifferenceWeight(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 floatgetTypeDifferenceWeight(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 floatgetTypeDifferenceWeight(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 floatgetTypeDifferenceWeight(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.ObjectinvokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)Invoke the specifiedMethodagainst the supplied target object with the supplied arguments.static voidsetField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value)Set the field represented by the suppliedfield objecton the specifiedtarget objectto the specifiedvalue.static java.lang.ObjecttoComponentTypeArray(java.lang.Object val, java.lang.Class<?> componentType)Converts an array of objects to an array of the specified component type.static java.lang.ObjecttoPrimitiveArray(java.lang.Object val)Converts an array of objects to an array of their primitive types.
-
-
-
Method Detail
-
getField
public static java.lang.Object getField(java.lang.reflect.Field field, java.lang.Object target)Get the field represented by the suppliedfield objecton the specifiedtarget object. In accordance withField.get(Object)semantics, the returned value is automatically wrapped if the underlying field has a primitive type.- Parameters:
field- the field to gettarget- 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 suppliedfield objecton the specifiedtarget objectto the specifiedvalue. In accordance withField.set(Object, Object)semantics, the new value is automatically unwrapped if the underlying field has a primitive type.- Parameters:
field- the field to settarget- the target object on which to set the fieldvalue- the value to set (may benull)
-
invokeMethod
public static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... args)Invoke the specifiedMethodagainst the supplied target object with the supplied arguments. The target object can benullwhen invoking a staticMethod.- Parameters:
method- the method to invoketarget- the target object to invoke the method onargs- the invocation arguments (may benull)- 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 matchdestArgs- 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 matchdestArg- 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 argumentsdestArgs- 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 classdestClass- 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 benull- 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 convertedcomponentType- theClassobject representing the component type of the new array- Returns:
- an array of the objects with the specified component type
-
-