Package org.springframework.data.util
Class ReflectionUtils
java.lang.Object
org.springframework.data.util.ReflectionUtils
Spring Data specific reflection utility methods and classes.
- Since:
- 1.5
- Author:
- Oliver Gierke, Thomas Darimont, Christoph Strobl, Mark Paluch, Johannes Englmeier
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
AReflectionUtils.FieldFilter
for a given annotation.static interface
AReflectionUtils.FieldFilter
that has a description. -
Method Summary
Modifier and TypeMethodDescriptionstatic Optional<Constructor<?>>
findConstructor
(Class<?> type, Object... constructorArguments) Deprecated.since 3.5, return type will change to nullable instead of Optional.static Field
findField
(Class<?> type, ReflectionUtils.DescribedFieldFilter filter) Finds the field matching the givenReflectionUtils.DescribedFieldFilter
.static Field
findField
(Class<?> type, ReflectionUtils.DescribedFieldFilter filter, boolean enforceUniqueness) Finds the field matching the givenReflectionUtils.DescribedFieldFilter
.static Field
findField
(Class<?> type, ReflectionUtils.FieldFilter filter) Finds the first field on the given class matching the givenReflectionUtils.FieldFilter
.static Method
findMethod
(Class<?> type, String name, ResolvableType... parameterTypes) Returns theMethod
with the given name and parameters declared on the given type, if available.static Method
findRequiredMethod
(Class<?> type, String name, Class<?>... parameterTypes) Deprecated.since 3.5, usegetRequiredMethod(Class, String, Class[])
instead.static int
getParameterCount
(Method method, Predicate<Class<?>> predicate) static Object
getPrimitiveDefault
(Class<?> type) Get default value for a primitive type.static Field
getRequiredField
(Class<?> type, String name) Obtains the required field of the given name on the given type or throwsIllegalArgumentException
if the found could not be found.static Method
getRequiredMethod
(Class<?> type, String name, Class<?>... parameterTypes) Returns the method with the given name of the given class and parameter types.static boolean
hasParameterAssignableToType
(Method method, Class<?> type) Returns whether the givenMethod
has a parameter that is assignable to the given type.static boolean
hasParameterOfType
(Method method, Class<?> type) Returns whether the givenMethod
has a parameter of the given type.static boolean
isNullable
(MethodParameter parameter) Returns whether the givenMethodParameter
is nullable.static boolean
returnTypeAndParameters
(Method method) static void
Sets the given field on the given object to the given value.
-
Method Details
-
hasParameterOfType
Returns whether the givenMethod
has a parameter of the given type.- Parameters:
method
- the method to check, must not be null.type
- parameter type to query for, must not be null.- Returns:
- true the given
Method
has a parameter of the given type. - Since:
- 3.5
-
hasParameterAssignableToType
Returns whether the givenMethod
has a parameter that is assignable to the given type.- Parameters:
method
- the method to check, must not be null.type
- parameter type to query for, must not be null.- Returns:
- true the given
Method
has a parameter that is assignable to the given type. - Since:
- 3.5
-
getParameterCount
-
isVoid
- Parameters:
type
- must not be null.- Returns:
- whether the given the type is a void type.
- Since:
- 2.4
-
findField
Finds the first field on the given class matching the givenReflectionUtils.FieldFilter
.- Parameters:
type
- must not be null.filter
- must not be null.- Returns:
- the field matching the filter or null in case no field could be found.
-
findField
Finds the field matching the givenReflectionUtils.DescribedFieldFilter
. Will make sure there's only one field matching the filter.- Parameters:
type
- must not be null.filter
- must not be null.- Returns:
- the field matching the given
ReflectionUtils.DescribedFieldFilter
or null if none found. - Throws:
IllegalStateException
- in case more than one matching field is found- See Also:
-
findField
@Nullable public static Field findField(Class<?> type, ReflectionUtils.DescribedFieldFilter filter, boolean enforceUniqueness) Finds the field matching the givenReflectionUtils.DescribedFieldFilter
. Will make sure there's only one field matching the filter in caseenforceUniqueness
is true.- Parameters:
type
- must not be null.filter
- must not be null.enforceUniqueness
- whether to enforce uniqueness of the field- Returns:
- the field matching the given
ReflectionUtils.DescribedFieldFilter
or null if none found. - Throws:
IllegalStateException
- if enforceUniqueness is true and more than one matching field is found
-
getRequiredField
Obtains the required field of the given name on the given type or throwsIllegalArgumentException
if the found could not be found.- Parameters:
type
- must not be null.name
- must not be null or empty.- Returns:
- the required field.
- Throws:
IllegalArgumentException
- in case the field can't be found.
-
setField
Sets the given field on the given object to the given value. Will make sure the given field is accessible.- Parameters:
field
- must not be null.target
- must not be null.value
-
-
findConstructor
@Deprecated public static Optional<Constructor<?>> findConstructor(Class<?> type, Object... constructorArguments) Deprecated.since 3.5, return type will change to nullable instead of Optional.Finds a constructor on the given type that matches the given constructor arguments.- Parameters:
type
- must not be null.constructorArguments
- must not be null.- Returns:
- a
Constructor
that is compatible with the given arguments.
-
findRequiredMethod
@Deprecated public static Method findRequiredMethod(Class<?> type, String name, Class<?>... parameterTypes) Deprecated.since 3.5, usegetRequiredMethod(Class, String, Class[])
instead.Returns the method with the given name of the given class and parameter types. Prefers regular methods overbridge
andsynthetic
ones.- Parameters:
type
- must not be null.name
- must not be null.parameterTypes
- must not be null.- Returns:
- the method object.
- Throws:
IllegalArgumentException
- in case the method cannot be resolved.
-
getRequiredMethod
Returns the method with the given name of the given class and parameter types. Prefers regular methods overbridge
andsynthetic
ones.- Parameters:
type
- must not be null.name
- must not be null.parameterTypes
- must not be null.- Returns:
- the method object.
- Throws:
IllegalArgumentException
- in case the method cannot be resolved.- Since:
- 3.5
-
returnTypeAndParameters
- Parameters:
method
- must not be null.- Returns:
- stream of return and parameter types.
- Since:
- 2.0
-
findMethod
@Nullable public static Method findMethod(Class<?> type, String name, ResolvableType... parameterTypes) Returns theMethod
with the given name and parameters declared on the given type, if available.- Parameters:
type
- must not be null.name
- must not be null or empty.parameterTypes
- must not be null.- Returns:
- the required method.
- Since:
- 3.5
-
isNullable
Returns whether the givenMethodParameter
is nullable. Nullable parameters are reference types and ones that are defined in Kotlin as such.- Returns:
- true if
MethodParameter
is nullable. - Since:
- 2.0
-
getPrimitiveDefault
Get default value for a primitive type.- Parameters:
type
- must not be null.- Returns:
- boxed primitive default value.
- Since:
- 2.1
-