Package com.aspectran.core.util
Class ReflectionUtils
- java.lang.Object
-
- com.aspectran.core.util.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 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 specifiedMethod
against the supplied target object with the supplied arguments.static boolean
makeAccessible(java.lang.reflect.Field field)
Make the given field accessible, explicitly setting it accessible if necessary.static boolean
makeAccessible(java.lang.reflect.Method method)
Make the given method accessible, explicitly setting it accessible if necessary.static void
setField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value)
Set the field represented by the suppliedfield object
on the specifiedtarget object
to the specifiedvalue
.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.
-
-
-
Method Detail
-
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 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
public static java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.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
-
makeAccessible
public static boolean makeAccessible(java.lang.reflect.Field field)
Make the given field accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)
method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).- Parameters:
field
- the field to make accessible- Returns:
- true, if successful
- See Also:
Field.setAccessible(boolean)
-
makeAccessible
public static boolean makeAccessible(java.lang.reflect.Method method)
Make the given method accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)
method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).- Parameters:
method
- the method to make accessible- Returns:
- true, if successful
- See Also:
Method.setAccessible(boolean)
-
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
- theClass
object representing the component type of the new array- Returns:
- an array of the objects with the specified component type
-
-