Package com.aspectran.utils
Class ReflectionUtils
java.lang.Object
com.aspectran.utils.ReflectionUtils
Simple utility class for working with the reflection API.
- Since:
- 2.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
Get the field represented by the suppliedfield object
on the specifiedtarget object
.static float
getTypeDifferenceWeight
(Class<?>[] srcArgs, Class<?>[] destArgs) Returns the sum of the object transformation cost for each class in the source argument list.static float
getTypeDifferenceWeight
(Class<?>[] paramTypes, 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
(Class<?> srcClass, Class<?> destClass) Gets the number of steps required needed to turn the source class into the destination class.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.static Object
invokeMethod
(Method method, Object target) Invoke the specifiedMethod
against the supplied target object with no arguments.static Object
invokeMethod
(Method method, Object target, Object... args) Invoke the specifiedMethod
against the supplied target object with the supplied arguments.static void
Set the field represented by the suppliedfield object
on the specifiedtarget object
to the specifiedvalue
.static Object
toComponentTypeArray
(Object val, Class<?> componentType) Converts an array of objects to an array of the specified component type.static Object
toPrimitiveArray
(Object val) Converts an array of objects to an array of their primitive types.
-
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 matchdestArgs
- the arguments to match- Returns:
- the accumulated weight for all arguments
-
getTypeDifferenceWeight
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(@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 argumentsdestArgs
- the destination arguments- Returns:
- the accumulated weight for all arguments
-
getTypeDifferenceWeight
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
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
Converts an array of objects to an array of the specified component type.- Parameters:
val
- an array of objects to be convertedcomponentType
- theClass
object representing the component type of the new array- Returns:
- an array of the objects with the specified component type
-
getField
Get the field represented by the suppliedfield object
on 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
Set the field represented by the suppliedfield object
on the specifiedtarget object
to 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
Invoke the specifiedMethod
against the supplied target object with no arguments. The target object can benull
when invoking a staticMethod
.- Parameters:
method
- the method to invoketarget
- 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 specifiedMethod
against the supplied target object with the supplied arguments. The target object can benull
when 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
-