Object ReflectionUtils
-
- All Implemented Interfaces:
public class ReflectionUtils
Reflection utility methods.
This class is intended for internal use only. All public methods (except might change between minor versions without notice.
Mahmoud Ben Hassine ([email protected])
-
-
Field Summary
Fields Modifier and Type Field Description public final static ReflectionUtils
INSTANCE
-
Method Summary
Modifier and Type Method Description final static <T extends Any> Randomizer<T>
asRandomizer(Supplier<T> supplier)
Create a dynamic proxy that adapts the given Supplier to a Randomizer. final static <T extends Any> List<Field>
getDeclaredFields(T type)
Get declared fields of a given type. final static List<Field>
getInheritedFields(Class<?> type)
Get inherited fields of a given type. final static Unit
setProperty(Object any, Field field, Object value)
Set a value in a field of a target object. final static Unit
setFieldValue(Object any, Field field, Object value)
Set a value (accessible or not accessible) in a field of a target object. final static Object
getFieldValue(Object any, Field field)
Get the value (accessible or not accessible) of a field of a target object. final static Class<out Object>
getWrapperType(Class<?> primitiveType)
Get wrapper type of a primitive type. final static Boolean
isPrimitiveFieldWithDefaultValue(Object any, Field field)
Check if a field has a primitive type and matching default value which is set by the compiler. final static Boolean
isStatic(Field field)
Check if a field is static. final static Boolean
isInterface(Class<?> type)
Check if a type is an interface. final static <T extends Any> Boolean
isAbstract(Class<T> type)
Check if the type is abstract (either an interface or an abstract class). final static <T extends Any> Boolean
isPublic(Class<T> type)
Check if the type is public. final static Boolean
isArrayType(Class<?> type)
Check if a type is an array type. final static Boolean
isEnumType(Class<?> type)
Check if a type is an enum type. final static Boolean
isCollectionType(Class<?> type)
Check if a type is a collection type. final static Boolean
isCollectionType(Type type)
Check if a type is a collection type. final static Boolean
isPopulatable(Type type)
Check if a type is populatable. final static Boolean
isIntrospectable(Class<?> type)
Check if a type should be introspected for internal fields. final static Boolean
isMapType(Class<?> type)
Check if a type is a map type. final static Boolean
isOptionalType(Class<?> type)
Check if a type is Optional. final static Boolean
isJdkBuiltIn(Class<?> type)
Check if a type is a JDK built-in collection/map. final static Boolean
isParameterizedType(Type type)
Check if a type is a parameterized type final static Boolean
isWildcardType(Type type)
Check if a type is a wildcard type final static Boolean
isTypeVariable(Type type)
Check if a type is a type variable final static <T extends Any> List<Class<?>>
getPublicConcreteSubTypesOf(Class<T> type)
Searches the classpath for all public concrete subtypes of the given interface or abstract class. final static List<Class<?>>
filterSameParameterizedTypes(List<Class<?>> types, Type type)
Filters a list of types to keep only elements having the same parameterized types as the given type. final static <T extends Annotation> T
getAnnotation(Field field, Class<T> annotationType)
Looks for given annotationType on given field or read method for field. final static Boolean
isAnnotationPresent(Field field, Class<out Annotation> annotationType)
Checks if field or corresponding read method is annotated with given annotationType. final static Collection<?>
getEmptyImplementationForCollectionInterface(Class<?> collectionInterface)
Return an empty implementation for a Collection type. final static Collection<?>
createEmptyCollectionForType(Class<?> fieldType, Integer initialSize)
Create an empty collection for the given type. final static Map<?, ?>
getEmptyImplementationForMapInterface(Class<?> mapInterface)
Returns an empty, mutable map implementation for the given map interface. final static Optional<Method>
getWriteMethod(Field field)
Get the write method for given field. final static Optional<Method>
getReadMethod(Field field)
Get the read method for given field. final static <T extends Any> Constructor<T>
getCanonicalConstructor(Class<T> recordType)
Gets the canonical constructor of a record. final static <T extends Any> Randomizer<T>
newInstance(Class<T> type, Array<RandomizerArgument> randomizerArguments)
Creates a new instance of the specified type using the provided constructor arguments, if any. -
-
Method Detail
-
asRandomizer
final static <T extends Any> Randomizer<T> asRandomizer(Supplier<T> supplier)
Create a dynamic proxy that adapts the given Supplier to a Randomizer.
- Parameters:
supplier
- to adapt- Returns:
the proxy randomizer </T>
-
getDeclaredFields
final static <T extends Any> List<Field> getDeclaredFields(T type)
Get declared fields of a given type.
- Parameters:
type
- the type to introspect- Returns:
list of declared fields </T>
-
getInheritedFields
final static List<Field> getInheritedFields(Class<?> type)
Get inherited fields of a given type.
- Parameters:
type
- the type to introspect- Returns:
list of inherited fields
-
setProperty
final static Unit setProperty(Object any, Field field, Object value)
Set a value in a field of a target object. If the target object provides a setter for the field, this setter will be used. Otherwise, the field will be set using reflection.
- Parameters:
any
- instance to set the property onfield
- field to set the property onvalue
- value to set
-
setFieldValue
final static Unit setFieldValue(Object any, Field field, Object value)
Set a value (accessible or not accessible) in a field of a target object.
- Parameters:
any
- instance to set the property onfield
- field to set the property onvalue
- value to set
-
getFieldValue
final static Object getFieldValue(Object any, Field field)
Get the value (accessible or not accessible) of a field of a target object.
- Parameters:
any
- instance to get the field offield
- field to get the value of- Returns:
the value of the field
-
getWrapperType
final static Class<out Object> getWrapperType(Class<?> primitiveType)
Get wrapper type of a primitive type.
- Parameters:
primitiveType
- to get its wrapper type- Returns:
the wrapper type of the given primitive type
-
isPrimitiveFieldWithDefaultValue
final static Boolean isPrimitiveFieldWithDefaultValue(Object any, Field field)
Check if a field has a primitive type and matching default value which is set by the compiler.
- Parameters:
any
- instance to get the field value offield
- field to check- Returns:
true if the field is primitive and is set to the default value, false otherwise
-
isStatic
final static Boolean isStatic(Field field)
Check if a field is static.
- Parameters:
field
- the field to check- Returns:
true if the field is static, false otherwise
-
isInterface
final static Boolean isInterface(Class<?> type)
Check if a type is an interface.
- Parameters:
type
- the type to check- Returns:
true if the type is an interface, false otherwise
-
isAbstract
final static <T extends Any> Boolean isAbstract(Class<T> type)
Check if the type is abstract (either an interface or an abstract class).
- Parameters:
type
- the type to check- Returns:
true if the type is abstract, false otherwise </T>
-
isPublic
final static <T extends Any> Boolean isPublic(Class<T> type)
Check if the type is public.
- Parameters:
type
- the type to check- Returns:
true if the type is public, false otherwise </T>
-
isArrayType
final static Boolean isArrayType(Class<?> type)
Check if a type is an array type.
- Parameters:
type
- the type to check.- Returns:
true if the type is an array type, false otherwise.
-
isEnumType
final static Boolean isEnumType(Class<?> type)
Check if a type is an enum type.
- Parameters:
type
- the type to check.- Returns:
true if the type is an enum type, false otherwise.
-
isCollectionType
final static Boolean isCollectionType(Class<?> type)
Check if a type is a collection type.
- Parameters:
type
- the type to check.- Returns:
true if the type is a collection type, false otherwise
-
isCollectionType
final static Boolean isCollectionType(Type type)
Check if a type is a collection type.
- Parameters:
type
- the type to check.- Returns:
true if the type is a collection type, false otherwise
-
isPopulatable
final static Boolean isPopulatable(Type type)
Check if a type is populatable.
- Parameters:
type
- the type to check- Returns:
true if the type is populatable, false otherwise
-
isIntrospectable
final static Boolean isIntrospectable(Class<?> type)
Check if a type should be introspected for internal fields.
- Parameters:
type
- the type to check- Returns:
true if the type should be introspected, false otherwise
-
isMapType
final static Boolean isMapType(Class<?> type)
Check if a type is a map type.
- Parameters:
type
- the type to check- Returns:
true if the type is a map type, false otherwise.
-
isOptionalType
final static Boolean isOptionalType(Class<?> type)
Check if a type is Optional.
- Parameters:
type
- the type to check- Returns:
true if the type is Optional, false otherwise.
-
isJdkBuiltIn
final static Boolean isJdkBuiltIn(Class<?> type)
Check if a type is a JDK built-in collection/map.
- Parameters:
type
- the type to check- Returns:
true if the type is a built-in collection/map type, false otherwise.
-
isParameterizedType
final static Boolean isParameterizedType(Type type)
Check if a type is a parameterized type
- Parameters:
type
- the type to check- Returns:
true if the type is parameterized, false otherwise
-
isWildcardType
final static Boolean isWildcardType(Type type)
Check if a type is a wildcard type
- Parameters:
type
- the type to check- Returns:
true if the type is a wildcard type, false otherwise
-
isTypeVariable
final static Boolean isTypeVariable(Type type)
Check if a type is a type variable
- Parameters:
type
- the type to check- Returns:
true if the type is a type variable, false otherwise
-
getPublicConcreteSubTypesOf
final static <T extends Any> List<Class<?>> getPublicConcreteSubTypesOf(Class<T> type)
Searches the classpath for all public concrete subtypes of the given interface or abstract class.
- Parameters:
type
- to search concrete subtypes of- Returns:
a list of all concrete subtypes found </T>
-
filterSameParameterizedTypes
final static List<Class<?>> filterSameParameterizedTypes(List<Class<?>> types, Type type)
Filters a list of types to keep only elements having the same parameterized types as the given type.
- Parameters:
types
- a list of types to filtertype
- the type to use for the search- Returns:
a list of types having the same parameterized types as the given type
-
getAnnotation
final static <T extends Annotation> T getAnnotation(Field field, Class<T> annotationType)
Looks for given annotationType on given field or read method for field.
- Parameters:
field
- field to checkannotationType
- Type of annotation you're looking for.- Returns:
given annotation if field or read method has this annotation or null. </T>
-
isAnnotationPresent
final static Boolean isAnnotationPresent(Field field, Class<out Annotation> annotationType)
Checks if field or corresponding read method is annotated with given annotationType.
- Parameters:
field
- Field to checkannotationType
- Annotation you're looking for.- Returns:
true if field or read method it annotated with given annotationType or false.
-
getEmptyImplementationForCollectionInterface
final static Collection<?> getEmptyImplementationForCollectionInterface(Class<?> collectionInterface)
Return an empty implementation for a Collection type.
- Parameters:
collectionInterface
- for which an empty implementation should be returned- Returns:
empty implementation for the collection interface
-
createEmptyCollectionForType
final static Collection<?> createEmptyCollectionForType(Class<?> fieldType, Integer initialSize)
Create an empty collection for the given type.
- Parameters:
fieldType
- for which an empty collection should we createdinitialSize
- initial size of the collection- Returns:
empty collection
-
getEmptyImplementationForMapInterface
final static Map<?, ?> getEmptyImplementationForMapInterface(Class<?> mapInterface)
Returns an empty, mutable map implementation for the given map interface.
- Parameters:
mapInterface
- The class of the map interface for which to create an empty implementation.- Returns:
A new empty MutableMap instance compatible with the given interface.
-
getWriteMethod
final static Optional<Method> getWriteMethod(Field field)
Get the write method for given field.
- Parameters:
field
- field to get the write method for- Returns:
Optional of write method or empty if field has no write method
-
getReadMethod
final static Optional<Method> getReadMethod(Field field)
Get the read method for given field.
- Parameters:
field
- field to get the read method for.- Returns:
Optional of read method or empty if field has no read method
-
getCanonicalConstructor
final static <T extends Any> Constructor<T> getCanonicalConstructor(Class<T> recordType)
Gets the canonical constructor of a record.
- Parameters:
recordType
- the class of the record.- Returns:
the canonical constructor of the record.
-
newInstance
final static <T extends Any> Randomizer<T> newInstance(Class<T> type, Array<RandomizerArgument> randomizerArguments)
Creates a new instance of the specified type using the provided constructor arguments, if any. If no arguments are provided or if no matching constructor is found, the default constructor of the type is used to create the instance.
- Parameters:
type
- the class of the type to instantiaterandomizerArguments
- an array of arguments to pass to the constructor of the specified type- Returns:
a new instance of the specified type wrapped in a Randomizer
-
-
-
-