Top Description Inners Constructors Methods
org.springframework.expression.spel.support

public abstract Class ReflectionHelper

extends Object
Class Inheritance
Imports
java.lang.invoke.MethodHandle, .MethodType, java.lang.reflect.Array, .Executable, .Method, java.util.List, .Optional, org.springframework.core.MethodParameter, .ResolvableType, org.springframework.core.convert.TypeDescriptor, org.springframework.expression.EvaluationException, .TypeConverter, org.springframework.expression.spel.SpelEvaluationException, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .CollectionUtils, .MethodInvoker

Utility methods used by the reflection resolver code to discover the appropriate methods/constructors and fields that should be used in expressions.
Authors
Andy Clement, Juergen Hoeller, Sam Brannen
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static record
ReflectionHelper.ArgumentsMatchInfo

An instance of ArgumentsMatchInfo describes what kind of match was achieved between two sets of arguments - the set that a method/constructor is expecting and the set that are being supplied at the point of invocation.

pack-priv static enum

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
pack-priv static ReflectionHelper.ArgumentsMatchInfo

Returns:

a MatchInfo object indicating what kind of match it was, or null if it was not a match
compareArguments
(List<TypeDescriptor>
the types the method/constructor is expecting
expectedArgTypes
,
List<TypeDescriptor>
the types that are being supplied at the point of invocation
suppliedArgTypes
,
TypeConverter
a registered type converter
typeConverter
)

Compare argument arrays and return information about whether they match.

pack-priv static ReflectionHelper.ArgumentsMatchInfo

Returns:

a MatchInfo object indicating what kind of match it was, or null if it was not a match
compareArgumentsVarargs
(List<TypeDescriptor>
the types the method/constructor is expecting
expectedArgTypes
,
List<TypeDescriptor>
the types that are being supplied at the point of invocation
suppliedArgTypes
,
TypeConverter
a registered type converter
typeConverter
)

Compare argument arrays and return information about whether they match.

public static boolean

Returns:

true if some kind of conversion occurred on the argument
convertAllArguments
(TypeConverter
the converter to use for type conversions
converter
,
Object[]
the arguments to convert to the requested parameter types
arguments
,
Method
the target Method
method
)

Convert a supplied set of arguments into the requested types.

public static boolean

Returns:

true if some kind of conversion occurred on an argument
convertAllMethodHandleArguments
(TypeConverter
the type converter to use for attempting conversions
converter
,
Object[]
the actual arguments that need conversion
arguments
,
MethodHandle
the target MethodHandle
methodHandle
,
Integer
the known position of the varargs argument, if any (null if not varargs)
varargsPosition
)

Takes an input set of argument values and converts them to the types specified as the required parameter types.

pack-priv static boolean

Returns:

true if some kind of conversion occurred on an argument
convertArguments
(TypeConverter
the type converter to use for attempting conversions
converter
,
Object[]
the actual arguments that need conversion
arguments
,
Executable
the target Method or Constructor
executable
,
Integer
the known position of the varargs argument, if any (null if not varargs)
varargsPosition
)

Takes an input set of argument values and converts them to the types specified as the required parameter types.

public static int
private static boolean

Returns:

true if the supplied value is the first entry in the array
isFirstEntryInArray
(Object
the value to check for in the array
value
,
Object
an array object that may have the supplied value as the first element
possibleArray
)

Check if the supplied value is the first entry in the array represented by the possibleArray value.

public static Object[]

Returns:

a repackaged array of arguments where any varargs setup has been done
setupArgumentsForVarargsInvocation
(Class<?>[]
the types of the parameters for the invocation
requiredParameterTypes
,
Object...
the arguments to be setup ready for the invocation
args
)

Package up the arguments so that they correctly match what is expected in requiredParameterTypes.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ReflectionHelperback to summary
public ReflectionHelper()

Method Detail

compareArgumentsback to summary
pack-priv static ReflectionHelper.ArgumentsMatchInfo compareArguments(List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter)

Compare argument arrays and return information about whether they match. A supplied type converter and conversionAllowed flag allow for matches to take into account that a type may be transformed into a different type by the converter.

Parameters
expectedArgTypes:List<TypeDescriptor>

the types the method/constructor is expecting

suppliedArgTypes:List<TypeDescriptor>

the types that are being supplied at the point of invocation

typeConverter:TypeConverter

a registered type converter

Returns:ReflectionHelper.ArgumentsMatchInfo

a MatchInfo object indicating what kind of match it was, or null if it was not a match

Annotations
@Nullable
compareArgumentsVarargsback to summary
pack-priv static ReflectionHelper.ArgumentsMatchInfo compareArgumentsVarargs(List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter)

Compare argument arrays and return information about whether they match. A supplied type converter and conversionAllowed flag allow for matches to take into account that a type may be transformed into a different type by the converter. This variant of compareArguments also allows for a varargs match.

Parameters
expectedArgTypes:List<TypeDescriptor>

the types the method/constructor is expecting

suppliedArgTypes:List<TypeDescriptor>

the types that are being supplied at the point of invocation

typeConverter:TypeConverter

a registered type converter

Returns:ReflectionHelper.ArgumentsMatchInfo

a MatchInfo object indicating what kind of match it was, or null if it was not a match

Annotations
@Nullable
convertAllArgumentsback to summary
public static boolean convertAllArguments(TypeConverter converter, Object[] arguments, Method method) throws SpelEvaluationException

Convert a supplied set of arguments into the requested types. If the parameterTypes are related to a varargs method then the final entry in the parameterTypes array is going to be an array itself whose component type should be used as the conversion target for extraneous 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 handles that.

Parameters
converter:TypeConverter

the converter to use for type conversions

arguments:Object[]

the arguments to convert to the requested parameter types

method:Method

the target Method

Returns:boolean

true if some kind of conversion occurred on the argument

Exceptions
SpelEvaluationException:
if there is a problem with conversion
convertAllMethodHandleArgumentsback to summary
public static boolean convertAllMethodHandleArguments(TypeConverter converter, Object[] arguments, MethodHandle methodHandle, Integer varargsPosition) throws EvaluationException

Takes an input set of argument values and converts them to the types specified as the required parameter types. The arguments are converted 'in-place' in the input array.

Parameters
converter:TypeConverter

the type converter to use for attempting conversions

arguments:Object[]

the actual arguments that need conversion

methodHandle:MethodHandle

the target MethodHandle

varargsPosition:Integer

the known position of the varargs argument, if any (null if not varargs)

Returns:boolean

true if some kind of conversion occurred on an argument

Exceptions
EvaluationException:
if a problem occurs during conversion
Since
6.1
convertArgumentsback to summary
pack-priv static boolean convertArguments(TypeConverter converter, Object[] arguments, Executable executable, Integer varargsPosition) throws EvaluationException

Takes an input set of argument values and converts them to the types specified as the required parameter types. The arguments are converted 'in-place' in the input array.

Parameters
converter:TypeConverter

the type converter to use for attempting conversions

arguments:Object[]

the actual arguments that need conversion

executable:Executable

the target Method or Constructor

varargsPosition:Integer

the known position of the varargs argument, if any (null if not varargs)

Returns:boolean

true if some kind of conversion occurred on an argument

Exceptions
EvaluationException:
if a problem occurs during conversion
getTypeDifferenceWeightback to summary
public static int getTypeDifferenceWeight(List<TypeDescriptor> paramTypes, List<TypeDescriptor> argTypes)

Based on MethodInvoker#getTypeDifferenceWeight(Class[], Object[]) but operates on TypeDescriptors.

isFirstEntryInArrayback to summary
private static boolean isFirstEntryInArray(Object value, Object possibleArray)

Check if the supplied value is the first entry in the array represented by the possibleArray value.

Parameters
value:Object

the value to check for in the array

possibleArray:Object

an array object that may have the supplied value as the first element

Returns:boolean

true if the supplied value is the first entry in the array

setupArgumentsForVarargsInvocationback to summary
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 declared String..., 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:Class<?>[]

the types of the parameters for the invocation

args:Object[]

the arguments to be setup ready for the invocation

Returns:Object[]

a repackaged array of arguments where any varargs setup has been done

org.springframework.expression.spel.support back to summary

pack-priv final Record ReflectionHelper.ArgumentsMatchInfo

extends Record
Class Inheritance
Record Components
kind:ReflectionHelper.ArgumentsMatchKind
the kind of match that was achieved

An instance of ArgumentsMatchInfo describes what kind of match was achieved between two sets of arguments - the set that a method/constructor is expecting and the set that are being supplied at the point of invocation. If the kind indicates that conversion is required for some of the arguments then the arguments that require conversion are listed in the argsRequiringConversion array.

Field Summary

Modifier and TypeField and Description
private final ReflectionHelper.ArgumentsMatchKind
kind

Record Component accessed by kind().

the kind of match that was achieved

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object
the reference object with which to compare.
o
)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.
public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.
public boolean
public boolean
public boolean
public ReflectionHelper.ArgumentsMatchKind
kind()

Record Component getter of kind.

Record Component the kind of match that was achieved

public String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

kindback to summary
private final ReflectionHelper.ArgumentsMatchKind kind

Record Component accessed by kind().

the kind of match that was achieved

Constructor Detail

ArgumentsMatchInfoback to summary
pack-priv ArgumentsMatchInfo(ReflectionHelper.ArgumentsMatchKind kind)

Method Detail

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this record is equal to the argument; false otherwise.

hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

Returns:int

a hash code value for this record.

isCloseMatchback to summary
public boolean isCloseMatch()
isExactMatchback to summary
public boolean isExactMatch()
isMatchRequiringConversionback to summary
public boolean isMatchRequiringConversion()
kindback to summary
public ReflectionHelper.ArgumentsMatchKind kind()

Record Component getter of kind.

Record Component
the kind of match that was achieved
toStringback to summary
public String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

Returns:String

a string representation of the object.

Annotations
@Override
org.springframework.expression.spel.support back to summary

pack-priv final Enum ReflectionHelper.ArgumentsMatchKind

extends Enum<ReflectionHelper.ArgumentsMatchKind>
Class Inheritance

Arguments match kinds.

Field Summary

Modifier and TypeField and Description
public static final ReflectionHelper.ArgumentsMatchKind
CLOSE

A close match is where the parameter types either exactly match or are assignment-compatible.

public static final ReflectionHelper.ArgumentsMatchKind
EXACT

An exact match is where the parameter types exactly match what the method/constructor is expecting.

public static final ReflectionHelper.ArgumentsMatchKind
REQUIRES_CONVERSION

A conversion match is where the type converter must be used to transform some of the parameter types.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ReflectionHelper.ArgumentsMatchKind
public static ReflectionHelper.ArgumentsMatchKind[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CLOSEback to summary
public static final ReflectionHelper.ArgumentsMatchKind CLOSE

A close match is where the parameter types either exactly match or are assignment-compatible.

EXACTback to summary
public static final ReflectionHelper.ArgumentsMatchKind EXACT

An exact match is where the parameter types exactly match what the method/constructor is expecting.

REQUIRES_CONVERSIONback to summary
public static final ReflectionHelper.ArgumentsMatchKind REQUIRES_CONVERSION

A conversion match is where the type converter must be used to transform some of the parameter types.

Constructor Detail

ArgumentsMatchKindback to summary
private ArgumentsMatchKind()

Method Detail

valueOfback to summary
public static ReflectionHelper.ArgumentsMatchKind valueOf(String name)
valuesback to summary
public static ReflectionHelper.ArgumentsMatchKind[] values()