Class ReflectionHelper
- Since:
- 3.0
- Author:
- Andy Clement, Juergen Hoeller, Sam Brannen
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
convertAllArguments
(TypeConverter converter, Object[] arguments, Method method) Convert the supplied set of arguments into the parameter types specified by the suppliedMethod
.static boolean
convertAllMethodHandleArguments
(TypeConverter converter, Object[] arguments, MethodHandle methodHandle, Integer varargsPosition) Takes an input set of argument values and converts them to the parameter types of the suppliedMethodHandle
.static int
getTypeDifferenceWeight
(List<TypeDescriptor> paramTypes, List<TypeDescriptor> argTypes) Based onMethodInvoker.getTypeDifferenceWeight(Class[], Object[])
but operates on TypeDescriptors.static Object[]
setupArgumentsForVarargsInvocation
(Class<?>[] requiredParameterTypes, Object... args) Package up the arguments so that they correctly match what is expected in requiredParameterTypes.
-
Constructor Details
-
ReflectionHelper
public ReflectionHelper()
-
-
Method Details
-
getTypeDifferenceWeight
public static int getTypeDifferenceWeight(List<TypeDescriptor> paramTypes, List<TypeDescriptor> argTypes) Based onMethodInvoker.getTypeDifferenceWeight(Class[], Object[])
but operates on TypeDescriptors. -
convertAllArguments
public static boolean convertAllArguments(TypeConverter converter, Object[] arguments, Method method) throws SpelEvaluationException Convert the supplied set of arguments into the parameter types specified by the suppliedMethod
.The arguments are converted 'in-place' in the input array.
If the method accepts varargs, the final entry in its parameterTypes array is going to be an array itself whose component type will be used as the conversion target for any additional arguments. For example, if the parameterTypes are {Integer, String[]} and the input arguments are {Integer, boolean, float}, then both the boolean and float must be converted to strings.
This method does not repackage the arguments into a form suitable for the varargs invocation. A subsequent call to
setupArgumentsForVarargsInvocation(Class[], Object...)
must be used for that.- Parameters:
converter
- the converter to use for type conversionsarguments
- the arguments to convert to the parameter types of the target methodmethod
- the target method- Returns:
- true if some kind of conversion occurred on an argument
- Throws:
SpelEvaluationException
- if there is a problem with conversion
-
convertAllMethodHandleArguments
public static boolean convertAllMethodHandleArguments(TypeConverter converter, Object[] arguments, MethodHandle methodHandle, @Nullable Integer varargsPosition) throws EvaluationException Takes an input set of argument values and converts them to the parameter types of the suppliedMethodHandle
.The arguments are converted 'in-place' in the input array.
- Parameters:
converter
- the type converter to use for attempting conversionsarguments
- the actual arguments that need conversionmethodHandle
- the target MethodHandlevarargsPosition
- the known position of the varargs argument, if any (null
if not varargs)- Returns:
true
if some kind of conversion occurred on an argument- Throws:
EvaluationException
- if a problem occurs during conversion- Since:
- 6.1
-
setupArgumentsForVarargsInvocation
public static Object[] setupArgumentsForVarargsInvocation(Class<?>[] requiredParameterTypes, Object... args) Package up the arguments so that they correctly match what is expected in requiredParameterTypes.For example, if requiredParameterTypes is
(int, String[])
because the second parameter was declaredString...
, then if arguments is[1,"a","b"]
then it must be repackaged as[1,new String[]{"a","b"}]
in order to match the expected types.- Parameters:
requiredParameterTypes
- the types of the parameters for the invocationargs
- the arguments to be setup ready for the invocation- Returns:
- a repackaged array of arguments where any varargs setup has been done
-