public final class ReflectionUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ReflectionUtils.AnnotationFieldFilter
A
ReflectionUtils.FieldFilter for a given annotation. |
static interface |
ReflectionUtils.DescribedFieldFilter
A
ReflectionUtils.FieldFilter that has a description. |
Modifier and Type | Method and Description |
---|---|
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)
Return true if the specified class is a Kotlin one.
|
static boolean |
isNullable(MethodParameter parameter)
Returns whether the given
MethodParameter is nullable. |
static boolean |
isSupportedKotlinClass(Class<?> type)
Return true if the specified class is a supported Kotlin class.
|
static Stream<Class<?>> |
returnTypeAndParameters(Method method)
|
static void |
setField(Field field,
Object target,
Object value)
Sets the given field on the given object to the given value.
|
public static <T> T createInstanceIfPresent(String classname, T defaultInstance)
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.@Nullable public static Field findField(Class<?> type, ReflectionUtils.FieldFilter filter)
ReflectionUtils.FieldFilter
.type
- must not be null.filter
- must not be null.@Nullable public static Field findField(Class<?> type, ReflectionUtils.DescribedFieldFilter filter)
ReflectionUtils.DescribedFieldFilter
. Will make sure there's only one field matching the
filter.type
- must not be null.filter
- must not be null.ReflectionUtils.DescribedFieldFilter
or null if none found.IllegalStateException
- in case more than one matching field is foundfindField(Class, DescribedFieldFilter, boolean)
@Nullable public static Field findField(Class<?> type, ReflectionUtils.DescribedFieldFilter filter, boolean enforceUniqueness)
ReflectionUtils.DescribedFieldFilter
. Will make sure there's only one field matching the
filter in case enforceUniqueness
is true.type
- must not be null.filter
- must not be null.enforceUniqueness
- whether to enforce uniqueness of the fieldReflectionUtils.DescribedFieldFilter
or null if none found.IllegalStateException
- if enforceUniqueness is true and more than one matching field is foundpublic static Field findRequiredField(Class<?> type, String name)
type
- must not be null.name
- must not be null or empty.IllegalArgumentException
- in case the field can't be found.public static void setField(Field field, Object target, @Nullable Object value)
field
- must not be null.target
- must not be null.value
- public static Optional<Constructor<?>> findConstructor(Class<?> type, Object... constructorArguments)
type
- must not be null.constructorArguments
- must not be null.Constructor
that is compatible with the given arguments.public static Method findRequiredMethod(Class<?> type, String name, Class<?>... parameterTypes)
type
- must not be null.name
- must not be null.parameterTypes
- must not be null.IllegalArgumentException
- in case the method cannot be resolved.public static Stream<Class<?>> returnTypeAndParameters(Method method)
method
- must not be null.public static Optional<Method> getMethod(Class<?> type, String name, ResolvableType... parameterTypes)
Method
with the given name and parameters declared on the given type, if available.type
- must not be null.name
- must not be null or empty.parameterTypes
- must not be null.public static boolean isKotlinClass(Class<?> type)
type
is a Kotlin class.public static boolean isSupportedKotlinClass(Class<?> type)
type
is a supported Kotlin class.public static boolean isNullable(MethodParameter parameter)
MethodParameter
is nullable. Nullable parameters are reference types
and ones that are defined in Kotlin as such.MethodParameter
is nullable.Copyright © 2011–2019 Pivotal Software, Inc.. All rights reserved.