Package org.jeasy.random.util
Class ReflectionUtils
- java.lang.Object
-
- org.jeasy.random.util.ReflectionUtils
-
public final class ReflectionUtils extends java.lang.ObjectReflection utility methods. This class is intended for internal use only. All public methods (exceptasRandomizer(java.util.function.Supplier)might change between minor versions without notice.- Author:
- Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Randomizer<T>asRandomizer(java.util.function.Supplier<T> supplier)Create a dynamic proxy that adapts the givenSupplierto aRandomizer.static java.util.Collection<?>createEmptyCollectionForType(java.lang.Class<?> fieldType, int initialSize)Create an empty collection for the given type.static java.util.List<java.lang.Class<?>>filterSameParameterizedTypes(java.util.List<java.lang.Class<?>> types, java.lang.reflect.Type type)Filters a list of types to keep only elements having the same parameterized types as the given type.static <T extends java.lang.annotation.Annotation>
TgetAnnotation(java.lang.reflect.Field field, java.lang.Class<T> annotationType)Looks for given annotationType on given field or read method for field.static <T> java.util.List<java.lang.reflect.Field>getDeclaredFields(T type)Get declared fields of a given type.static java.util.Collection<?>getEmptyImplementationForCollectionInterface(java.lang.Class<?> collectionInterface)Return an empty implementation for aCollectiontype.static java.util.Map<?,?>getEmptyImplementationForMapInterface(java.lang.Class<?> mapInterface)Return an empty implementation for the givenMapinterface.static java.lang.ObjectgetFieldValue(java.lang.Object object, java.lang.reflect.Field field)Get the value (accessible or not accessible) of a field of a target object.static java.util.List<java.lang.reflect.Field>getInheritedFields(java.lang.Class<?> type)Get inherited fields of a given type.static <T> java.util.List<java.lang.Class<?>>getPublicConcreteSubTypesOf(java.lang.Class<T> type)Searches the classpath for all public concrete subtypes of the given interface or abstract class.static java.util.Optional<java.lang.reflect.Method>getReadMethod(java.lang.reflect.Field field)Get the read method for given field.static java.lang.Class<?>getWrapperType(java.lang.Class<?> primitiveType)Get wrapper type of a primitive type.static java.util.Optional<java.lang.reflect.Method>getWriteMethod(java.lang.reflect.Field field)Get the write method for given field.static <T> booleanisAbstract(java.lang.Class<T> type)Check if the type is abstract (either an interface or an abstract class).static booleanisAnnotationPresent(java.lang.reflect.Field field, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)Checks if field or corresponding read method is annotated with given annotationType.static booleanisArrayType(java.lang.Class<?> type)Check if a type is an array type.static booleanisCollectionType(java.lang.Class<?> type)Check if a type is a collection type.static booleanisCollectionType(java.lang.reflect.Type type)Check if a type is a collection type.static booleanisEnumType(java.lang.Class<?> type)Check if a type is an enum type.static booleanisInterface(java.lang.Class<?> type)Check if a type is an interface.static booleanisIntrospectable(java.lang.Class<?> type)Check if a type should be introspected for internal fields.static booleanisJdkBuiltIn(java.lang.Class<?> type)Check if a type is a JDK built-in collection/map.static booleanisMapType(java.lang.Class<?> type)Check if a type is a map type.static booleanisOptionalType(java.lang.Class<?> type)Check if a type isOptional.static booleanisParameterizedType(java.lang.reflect.Type type)Check if a type is a parameterized typestatic booleanisPopulatable(java.lang.reflect.Type type)Check if a type is populatable.static booleanisPrimitiveFieldWithDefaultValue(java.lang.Object object, java.lang.reflect.Field field)Check if a field has a primitive type and matching default value which is set by the compiler.static <T> booleanisPublic(java.lang.Class<T> type)Check if the type is public.static booleanisStatic(java.lang.reflect.Field field)Check if a field is static.static booleanisTypeVariable(java.lang.reflect.Type type)Check if a type is a type variablestatic booleanisWildcardType(java.lang.reflect.Type type)Check if a type is a wildcard typestatic <T> Randomizer<T>newInstance(java.lang.Class<T> type, RandomizerArgument[] randomizerArguments)static voidsetFieldValue(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value)Set a value (accessible or not accessible) in a field of a target object.static voidsetProperty(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value)Set a value in a field of a target object.
-
-
-
Method Detail
-
asRandomizer
public static <T> Randomizer<T> asRandomizer(java.util.function.Supplier<T> supplier)
Create a dynamic proxy that adapts the givenSupplierto aRandomizer.- Type Parameters:
T- target type- Parameters:
supplier- to adapt- Returns:
- the proxy randomizer
-
getDeclaredFields
public static <T> java.util.List<java.lang.reflect.Field> getDeclaredFields(T type)
Get declared fields of a given type.- Type Parameters:
T- the actual type to introspect- Parameters:
type- the type to introspect- Returns:
- list of declared fields
-
getInheritedFields
public static java.util.List<java.lang.reflect.Field> getInheritedFields(java.lang.Class<?> type)
Get inherited fields of a given type.- Parameters:
type- the type to introspect- Returns:
- list of inherited fields
-
setProperty
public static void setProperty(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionSet 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:
object- instance to set the property onfield- field to set the property onvalue- value to set- Throws:
java.lang.IllegalAccessException- if the property cannot be setjava.lang.reflect.InvocationTargetException
-
setFieldValue
public static void setFieldValue(java.lang.Object object, java.lang.reflect.Field field, java.lang.Object value) throws java.lang.IllegalAccessExceptionSet a value (accessible or not accessible) in a field of a target object.- Parameters:
object- instance to set the property onfield- field to set the property onvalue- value to set- Throws:
java.lang.IllegalAccessException- if the property cannot be set
-
getFieldValue
public static java.lang.Object getFieldValue(java.lang.Object object, java.lang.reflect.Field field) throws java.lang.IllegalAccessExceptionGet the value (accessible or not accessible) of a field of a target object.- Parameters:
object- instance to get the field offield- field to get the value of- Returns:
- the value of the field
- Throws:
java.lang.IllegalAccessException- if field cannot be accessed
-
getWrapperType
public static java.lang.Class<?> getWrapperType(java.lang.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
public static boolean isPrimitiveFieldWithDefaultValue(java.lang.Object object, java.lang.reflect.Field field) throws java.lang.IllegalAccessExceptionCheck if a field has a primitive type and matching default value which is set by the compiler.- Parameters:
object- 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
- Throws:
java.lang.IllegalAccessException- if field cannot be accessed
-
isStatic
public static boolean isStatic(java.lang.reflect.Field field)
Check if a field is static.- Parameters:
field- the field to check- Returns:
- true if the field is static, false otherwise
-
isInterface
public static boolean isInterface(java.lang.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
public static <T> boolean isAbstract(java.lang.Class<T> type)
Check if the type is abstract (either an interface or an abstract class).- Type Parameters:
T- the actual type to check- Parameters:
type- the type to check- Returns:
- true if the type is abstract, false otherwise
-
isPublic
public static <T> boolean isPublic(java.lang.Class<T> type)
Check if the type is public.- Type Parameters:
T- the actual type to check- Parameters:
type- the type to check- Returns:
- true if the type is public, false otherwise
-
isArrayType
public static boolean isArrayType(java.lang.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
public static boolean isEnumType(java.lang.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
public static boolean isCollectionType(java.lang.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
public static boolean isCollectionType(java.lang.reflect.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
public static boolean isPopulatable(java.lang.reflect.Type type)
Check if a type is populatable.- Parameters:
type- the type to check- Returns:
- true if the type is populatable, false otherwise
-
isIntrospectable
public static boolean isIntrospectable(java.lang.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
public static boolean isMapType(java.lang.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
public static boolean isOptionalType(java.lang.Class<?> type)
Check if a type isOptional.- Parameters:
type- the type to check- Returns:
- true if the type is
Optional, false otherwise.
-
isJdkBuiltIn
public static boolean isJdkBuiltIn(java.lang.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
public static boolean isParameterizedType(java.lang.reflect.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
public static boolean isWildcardType(java.lang.reflect.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
public static boolean isTypeVariable(java.lang.reflect.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
public static <T> java.util.List<java.lang.Class<?>> getPublicConcreteSubTypesOf(java.lang.Class<T> type)
Searches the classpath for all public concrete subtypes of the given interface or abstract class.- Type Parameters:
T- the actual type to introspect- Parameters:
type- to search concrete subtypes of- Returns:
- a list of all concrete subtypes found
-
filterSameParameterizedTypes
public static java.util.List<java.lang.Class<?>> filterSameParameterizedTypes(java.util.List<java.lang.Class<?>> types, java.lang.reflect.Type type)Filters a list of types to keep only elements having the same parameterized types as the given type.- Parameters:
type- the type to use for the searchtypes- a list of types to filter- Returns:
- a list of types having the same parameterized types as the given type
-
getAnnotation
public static <T extends java.lang.annotation.Annotation> T getAnnotation(java.lang.reflect.Field field, java.lang.Class<T> annotationType)Looks for given annotationType on given field or read method for field.- Type Parameters:
T- the actual type of annotation- 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.
-
isAnnotationPresent
public static boolean isAnnotationPresent(java.lang.reflect.Field field, java.lang.Class<? extends java.lang.annotation.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
public static java.util.Collection<?> getEmptyImplementationForCollectionInterface(java.lang.Class<?> collectionInterface)
Return an empty implementation for aCollectiontype.- Parameters:
collectionInterface- for which an empty implementation should be returned- Returns:
- empty implementation for the collection interface
-
createEmptyCollectionForType
public static java.util.Collection<?> createEmptyCollectionForType(java.lang.Class<?> fieldType, int 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
public static java.util.Map<?,?> getEmptyImplementationForMapInterface(java.lang.Class<?> mapInterface)
Return an empty implementation for the givenMapinterface.- Parameters:
mapInterface- for which an empty implementation should be returned- Returns:
- empty implementation for the given
Mapinterface.
-
getWriteMethod
public static java.util.Optional<java.lang.reflect.Method> getWriteMethod(java.lang.reflect.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
public static java.util.Optional<java.lang.reflect.Method> getReadMethod(java.lang.reflect.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
-
newInstance
public static <T> Randomizer<T> newInstance(java.lang.Class<T> type, RandomizerArgument[] randomizerArguments)
-
-