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 SummaryNested ClassesModifier and TypeClassDescriptionstatic classAReflectionUtils.FieldFilterfor a given annotation.static interfaceAReflectionUtils.FieldFilterthat has a description.
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T> TcreateInstanceIfPresent(String classname, T defaultInstance) Creates an instance of the class with the given fully qualified name or returns the given default instance if the class cannot be loaded or instantiated.static Optional<Constructor<?>>findConstructor(Class<?> type, Object... constructorArguments) Finds a constructor on the given type that matches the given constructor arguments.static FieldfindField(Class<?> type, ReflectionUtils.DescribedFieldFilter filter) Finds the field matching the givenReflectionUtils.DescribedFieldFilter.static FieldfindField(Class<?> type, ReflectionUtils.DescribedFieldFilter filter, boolean enforceUniqueness) Finds the field matching the givenReflectionUtils.DescribedFieldFilter.static FieldfindField(Class<?> type, ReflectionUtils.FieldFilter filter) Finds the first field on the given class matching the givenReflectionUtils.FieldFilter.static FieldfindRequiredField(Class<?> type, String name) Finds the field of the given name on the given type.static MethodfindRequiredMethod(Class<?> type, String name, Class<?>... parameterTypes) Returns the method with the given name of the given class and parameter types.getMethod(Class<?> type, String name, ResolvableType... parameterTypes) Returns theMethodwith the given name and parameters declared on the given type, if available.static ObjectgetPrimitiveDefault(Class<?> type) Get default value for a primitive type.static booleanisNullable(MethodParameter parameter) Returns whether the givenMethodParameteris nullable.static booleanstatic Class<?>loadIfPresent(String name, ClassLoader classLoader) Loads the class with the given name using the givenClassLoader.returnTypeAndParameters(Method method) static voidSets the given field on the given object to the given value.
- 
Method Details- 
createInstanceIfPresentCreates an instance of the class with the given fully qualified name or returns the given default instance if the class cannot be loaded or instantiated.- Parameters:
- classname- the fully qualified class name to create an instance for.
- defaultInstance- the instance to fall back to in case the given class cannot be loaded or instantiated.
- Returns:
 
- 
isVoid- Parameters:
- type- must not be null.
- Returns:
- whether the given the type is a void type.
- Since:
- 2.4
 
- 
findFieldFinds 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.
 
- 
findFieldFinds 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.DescribedFieldFilteror 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 caseenforceUniquenessis 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.DescribedFieldFilteror null if none found.
- Throws:
- IllegalStateException- if enforceUniqueness is true and more than one matching field is found
 
- 
findRequiredFieldFinds the field of the given name on the given type.- Parameters:
- type- must not be null.
- name- must not be null or empty.
- Returns:
- Throws:
- IllegalArgumentException- in case the field can't be found.
 
- 
setFieldSets 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-
 
- 
findConstructorpublic static Optional<Constructor<?>> findConstructor(Class<?> type, Object... constructorArguments) 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 Constructorthat is compatible with the given arguments.
 
- 
findRequiredMethodReturns the method with the given name of the given class and parameter types. Prefers regular methods overbridgeandsyntheticones.- 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.
 
- 
returnTypeAndParameters- Parameters:
- method- must not be null.
- Returns:
- Since:
- 2.0
 
- 
getMethodpublic static Optional<Method> getMethod(Class<?> type, String name, ResolvableType... parameterTypes) Returns theMethodwith 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:
- Since:
- 2.0
 
- 
isNullableReturns whether the givenMethodParameteris nullable. Nullable parameters are reference types and ones that are defined in Kotlin as such.- Returns:
- true if MethodParameteris nullable.
- Since:
- 2.0
 
- 
getPrimitiveDefaultGet default value for a primitive type.- Parameters:
- type- must not be null.
- Returns:
- boxed primitive default value.
- Since:
- 2.1
 
- 
loadIfPresentLoads the class with the given name using the givenClassLoader.- Parameters:
- name- the name of the class to be loaded.
- classLoader- the- ClassLoaderto use to load the class.
- Returns:
- the Classor null in case the class can't be loaded for any reason.
- Since:
- 2.5
 
 
-