org.springframework.data.util

## Class ReflectionUtils

- java.lang.Object
- - org.springframework.data.util.ReflectionUtils

    public final class ReflectionUtils
    extends Object

Spring Data specific reflection utility methods and classes.

Since:
  1.5
Author:
  Oliver Gierke, Thomas Darimont, Christoph Strobl, Mark Paluch

### Nested Class Summary

Nested Classes
`static class` `ReflectionUtils.AnnotationFieldFilter` A `ReflectionUtils.FieldFilter` for a given annotation.
`static interface` `ReflectionUtils.DescribedFieldFilter` A `ReflectionUtils.FieldFilter` that has a description.

### Method Summary

All Methods Static Methods Concrete Methods Deprecated Methods
`static <T> T` `createInstanceIfPresent(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 Field` `findField(Class<?> type, ReflectionUtils.DescribedFieldFilter filter)` Finds the field matching the given `ReflectionUtils.DescribedFieldFilter`.
`static Field` `findField(Class<?> type, ReflectionUtils.DescribedFieldFilter filter, boolean enforceUniqueness)` Finds the field matching the given `ReflectionUtils.DescribedFieldFilter`.
`static Field` `findField(Class<?> type, ReflectionUtils.FieldFilter filter)` Finds the first field on the given class matching the given `ReflectionUtils.FieldFilter`.
`static Field` `findRequiredField(Class<?> type, String name)` Finds the field of the given name on the given type.
`static Method` `findRequiredMethod(Class<?> type, String name, Class<?>... parameterTypes)` Returns the method with the given name of the given class and parameter types.
`static Optional<Method>` `getMethod(Class<?> type, String name, ResolvableType... parameterTypes)` Returns the `Method` with the given name and parameters declared on the given type, if available.
`static Object` `getPrimitiveDefault(Class<?> type)` Get default value for a primitive type.
`static boolean` `isKotlinClass(Class<?> type)` Deprecated. since 2.3, use `KotlinDetector.isKotlinType(Class)` instead.
`static boolean` `isNullable(MethodParameter parameter)` Returns whether the given `MethodParameter` is nullable.
`static boolean` `isSupportedKotlinClass(Class<?> type)` Deprecated. since 2.3, use `KotlinReflectionUtils.isSupportedKotlinClass(Class)` instead.
`static boolean` `isVoid(Class<?> type)` Check whether the given `type` represents a void type such as `void`, `Void` or Kotlin `Unit`.
`static Stream<Class<?>>` `returnTypeAndParameters(Method method)` Returns a `Stream` of the return and parameters types of the given `Method`.
`static void` `setField(Field field, Object target, Object value)` Sets the given field on the given object to the given value.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### createInstanceIfPresent

    public static <T> T createInstanceIfPresent(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.

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

    public static boolean isVoid(Class<?> type)

Check whether the given `type` represents a void type such as `void`, `Void` or Kotlin `Unit`.

Parameters:
  `type` - must not be null.
Returns:
  whether the given the type is a void type.
Since:
  2.4

#### findField

    @Nullable
    public static Field findField(Class<?> type,
                                            ReflectionUtils.FieldFilter filter)

Finds the first field on the given class matching the given `ReflectionUtils.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

    @Nullable
    public static Field findField(Class<?> type,
                                            ReflectionUtils.DescribedFieldFilter filter)

Finds the field matching the given `ReflectionUtils.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(Class, DescribedFieldFilter, boolean)`

#### findField

    @Nullable
    public static Field findField(Class<?> type,
                                            ReflectionUtils.DescribedFieldFilter filter,
                                            boolean enforceUniqueness)

Finds the field matching the given `ReflectionUtils.DescribedFieldFilter`. Will make sure there's only one field matching the filter in case `enforceUniqueness` 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

#### findRequiredField

    public static Field findRequiredField(Class<?> type,
                                          String name)

Finds 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.

#### setField

    public static void setField(Field field,
                                Object target,
                                @Nullable
                                Object value)

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

    public 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 `Constructor` that is compatible with the given arguments.

#### findRequiredMethod

    public static Method findRequiredMethod(Class<?> type,
                                            String name,
                                            Class<?>... parameterTypes)

Returns the method with the given name of the given class and parameter types. Prefers regular methods over `bridge` and `synthetic` 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.

#### returnTypeAndParameters

    public static Stream<Class<?>> returnTypeAndParameters(Method method)

Returns a `Stream` of the return and parameters types of the given `Method`.

Parameters:
  `method` - must not be null.
Returns:
Since:
  2.0

#### getMethod

    public static Optional<Method> getMethod(Class<?> type,
                                             String name,
                                             ResolvableType... parameterTypes)

Returns the `Method` 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:
Since:
  2.0

#### isKotlinClass

    @Deprecated
    public static boolean isKotlinClass(Class<?> type)

Deprecated. since 2.3, use `KotlinDetector.isKotlinType(Class)` instead.
Return true if the specified class is a Kotlin one.

Returns:
  true if `type` is a Kotlin class.
Since:
  2.0

#### isSupportedKotlinClass

    @Deprecated
    public static boolean isSupportedKotlinClass(Class<?> type)

Deprecated. since 2.3, use `KotlinReflectionUtils.isSupportedKotlinClass(Class)` instead.
Return true if the specified class is a supported Kotlin class. Currently supported are only regular Kotlin classes. Other class types (synthetic, SAM, lambdas) are not supported via reflection.

Returns:
  true if `type` is a supported Kotlin class.
Since:
  2.0

#### isNullable

    public static boolean isNullable(MethodParameter parameter)

Returns whether the given `MethodParameter` 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

    public static Object getPrimitiveDefault(Class<?> type)

Get default value for a primitive type.

Parameters:
  `type` - must not be null.
Returns:
  boxed primitive default value.
Since:
  2.1