Package com.gengoai.reflection
Class Reflect
- java.lang.Object
-
- com.gengoai.reflection.Reflect
-
- All Implemented Interfaces:
Serializable
public final class Reflect extends Object
Wrapper around an object or class allowing easy access to reflection operations.- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Reflect
allowPrivilegedAccess()
Allow privileged access during reflective calls.boolean
containsField(String name)
Determines if a field with the given name is associated with the classboolean
containsMethod(String name)
Determines if a method with the given name is associated with the classboolean
containsMethod(String name, Type... parameters)
Determines if a method with the given name is associated with the classReflect
create()
Creates an instance of the class being reflected using the no-argument constructor.Reflect
create(@NonNull Class[] types, @NonNull Object... args)
Creates an instance of the class being reflected using the best constructor that matches the given types.Reflect
create(Object... args)
Creates an instance of the class being reflected using the most specific constructor available.<T> T
get()
Gets the underlying object.<T> T
get(String name)
Convenience method for getting the value of a field or getter methodIterable<Reflect>
getAncestors(boolean reverseOrder)
Gets an iterable of the ancestors of this class including super classes and interfaces.<A extends Annotation>
AgetAnnotation(@NonNull Class<A> annotationClass, boolean onlyDirect)
Gets the annotation of the given class on this object.<A extends Annotation>
AgetAnnotation(Class<A> annotationClass)
Gets the annotation of the given class present on this object.Annotation[]
getAnnotations()
Gets all annotations present on this object.Annotation[]
getAnnotations(boolean onlyDirect)
Gets all annotations on this object.<A extends Annotation>
A[]getAnnotations(@NonNull Class<A> annotationClass)
Gets all associated annotations of given type on this object.<A extends Annotation>
A[]getAnnotations(@NonNull Class<A> annotationClass, boolean onlyDirect)
Gets all annotations on this object.static Class<?>
getClassForName(String name)
Attempts to determine the Class for a given name in string form.static Class<?>
getClassForNameQuietly(String name)
CallsgetClassForName(String)
, but suppresses exception to a log warning and returns null instead.RConstructor
getConstructor(@NonNull Type... types)
Gets the best constructor for the class matching the given typesList<RConstructor>
getConstructorsWhere(@NonNull SerializablePredicate<? super RConstructor> predicate)
Gets the constructors for the class matching the given predicateList<RConstructor>
getConstructorsWithAnnotation(@NonNull Class<? extends Annotation>... annotationClasses)
Gets the constructors for the class with at least of the given annotations.Reflect
getDeclaringClass()
Gets the class that declares the reflected objectClass<?>
getElement()
Gets the AnnotatedElementRField
getField(String name)
Gets the field with the given name.List<RField>
getFields()
Gets all fields.List<RField>
getFieldsWhere(@NonNull SerializablePredicate<RField> predicate)
Gets all fields that match the given predicateList<RField>
getFieldsWithAnnotation(@NonNull Class<? extends Annotation>... annotationClasses)
Gets the fields for the class with at least of the given annotations.RMethod
getMethod(String name)
Gets the method with the given name.RMethod
getMethod(String name, Type... types)
Gets the method with the given name and with types compatible with the given types.List<RMethod>
getMethods()
Gets all methods.List<RMethod>
getMethods(String name)
Gets all methods with the given name.List<RMethod>
getMethodsWhere(@NonNull SerializablePredicate<? super RMethod> predicate)
Gets the methods for the class matching the given predicateList<RMethod>
getMethodsWhere(String name, @NonNull SerializablePredicate<? super RMethod> predicate)
Gets the methods with the given name for the class matching the given predicateList<RMethod>
getMethodsWithAnnotation(@NonNull Class<? extends Annotation>... annotationClasses)
Gets the methods for the class with at least of the given annotations.int
getModifiers()
Gets the modifiers on this field.String
getName()
Gets the name of the elementRMethod
getSingletonMethod()
Gets the singleton method of the object.Reflect
getSuperClass()
Gets the super-class of the class being reflectedClass<?>
getType()
Gets the type of the elementboolean
isAnnotationDeclared(@NonNull Class<? extends Annotation>... annotationClasses)
Determines if any of the given annotations are declared on this objectboolean
isAnnotationPresent(@NonNull Class<? extends Annotation>... annotationClasses)
Determines if any of the given annotations are present on this objectboolean
isPrivileged()
is privileged access allowed on this object?boolean
isSingleton()
Does the reflected class have a singleton creation method (getInstance, getSingleton, or createInstance)static Reflect
onClass(@NonNull Class<?> clazz)
Creates a Reflect object for the given class.static Reflect
onClass(@NonNull Type clazz)
Creates a Reflect object for the given type.static Reflect
onClass(String className)
Creates a Reflect object for the class represented by the given class name.static Reflect
onObject(Object object)
Creates a Reflect object on the given object<A extends Annotation,O>
OprocessAnnotation(@NonNull Class<A> annotationClass, @NonNull CheckedFunction<? super A,? extends O> function)
Applies the givenCheckedBiFunction
to this object with the given annotation if it is present on the object.<A extends Annotation,O>
List<O>processAnnotations(@NonNull Class<A> annotationClass, @NonNull CheckedFunction<? super A,? extends O> function)
Applies the givenCheckedBiFunction
to this object and all instances of the given annotation present on the object.Reflect
set(String name, Object value)
Convenience method for setting the value of a field or setterReflect
setIsPrivileged(boolean allowPrivilegedAccess)
Sets whether or not privileged access is allowed on this objectString
toString()
<A extends Annotation>
VwithAnnotation(@NonNull Class<A> annotationClass, @NonNull CheckedConsumer<? super A> consumer)
Applies the givenCheckedBiConsumer
to this object with the given annotation if it is present on the object.<A extends Annotation>
VwithAnnotations(@NonNull Class<A> annotationClass, @NonNull CheckedConsumer<? super A> consumer)
Applies the givenCheckedBiConsumer
to this object and all instances of the given annotation present on this object.
-
-
-
Method Detail
-
getClassForName
public static Class<?> getClassForName(String name) throws Exception
Attempts to determine the Class for a given name in string form. For convenience the String, primitives and Object versions of the primitives can be called with just their name. The following types also have a short hand
- List - java.util.List
- ArrayList - java.util.ArrayList
- Set - java.util.Set
- HashSet - java.util.HashSet
- Map - java.util.Map
- HashMap - java.util.HashMap
Array versions can be created by appending [] to the end. For example, String[] refers to an array of java.lang.String
- Parameters:
name
- The class name- Returns:
- The represented by the name
- Throws:
Exception
- the exception
-
getClassForNameQuietly
public static Class<?> getClassForNameQuietly(String name)
Calls
getClassForName(String)
, but suppresses exception to a log warning and returns null instead. Any exceptions are logged to the default logger.(at DEBUG level) and a null is returned.- Parameters:
name
- Name of class- Returns:
- The Class information or null
-
onClass
public static Reflect onClass(@NonNull @NonNull Class<?> clazz)
Creates a Reflect object for the given class.- Parameters:
clazz
- the class we want reflective access to- Returns:
- the reflect object
-
onClass
public static Reflect onClass(@NonNull @NonNull Type clazz)
Creates a Reflect object for the given type.- Parameters:
clazz
- the type we want reflective access to- Returns:
- the reflect object
-
onClass
public static Reflect onClass(String className) throws ReflectionException
Creates a Reflect object for the class represented by the given class name.- Parameters:
className
- the name of the class we want reflective access to- Returns:
- the reflect object
- Throws:
ReflectionException
- the reflection exception
-
onObject
public static Reflect onObject(Object object)
Creates a Reflect object on the given object- Parameters:
object
- the object we want reflective access to- Returns:
- the reflect object
-
allowPrivilegedAccess
public Reflect allowPrivilegedAccess()
Allow privileged access during reflective calls.- Returns:
- this reflect object
-
containsField
public boolean containsField(String name)
Determines if a field with the given name is associated with the class- Parameters:
name
- The field name- Returns:
- True if there is a field with the given name
-
containsMethod
public boolean containsMethod(String name)
Determines if a method with the given name is associated with the class- Parameters:
name
- The method name- Returns:
- True if there is a method with the given name
-
containsMethod
public boolean containsMethod(String name, Type... parameters)
Determines if a method with the given name is associated with the class- Parameters:
name
- The method name- Returns:
- True if there is a method with the given name
-
create
public Reflect create() throws ReflectionException
Creates an instance of the class being reflected using the no-argument constructor.- Returns:
- A
Reflect
object to do further reflection - Throws:
ReflectionException
- Something went wrong constructing the object
-
create
public Reflect create(Object... args) throws ReflectionException
Creates an instance of the class being reflected using the most specific constructor available.- Parameters:
args
- The arguments to the constructor.- Returns:
- A
Reflect
object to do further reflection - Throws:
ReflectionException
- Something went wrong constructing the object
-
create
public Reflect create(@NonNull @NonNull Class[] types, @NonNull @NonNull Object... args) throws ReflectionException
Creates an instance of the class being reflected using the best constructor that matches the given types.- Parameters:
types
- The type of the given arguments.args
- The arguments to the constructor.- Returns:
- A
Reflect
object to do further reflection - Throws:
ReflectionException
- Something went wrong constructing the object
-
get
public <T> T get()
Gets the underlying object.- Type Parameters:
T
- the type parameter- Returns:
- the underlying object or null if we are reflecting a class
-
get
public <T> T get(String name) throws ReflectionException
Convenience method for getting the value of a field or getter method- Type Parameters:
T
- the value type- Parameters:
name
- the name of the field- Returns:
- the value of the field
- Throws:
ReflectionException
- Something went wrong accessing the field
-
getAncestors
public Iterable<Reflect> getAncestors(boolean reverseOrder)
Gets an iterable of the ancestors of this class including super classes and interfaces.- Parameters:
reverseOrder
- True - order starting at Object, False order starting at superclass.- Returns:
- the iterable of Reflect objects representing super-classes and interfaces
-
getConstructor
public RConstructor getConstructor(@NonNull @NonNull Type... types) throws ReflectionException
Gets the best constructor for the class matching the given types- Parameters:
types
- The types (possibly empty) of the constructor parameters- Returns:
- the best constructor constructor for the class
- Throws:
ReflectionException
- Either could not find an appropriate constructor or security did not allow reflective access.
-
getConstructorsWhere
public final List<RConstructor> getConstructorsWhere(@NonNull @NonNull SerializablePredicate<? super RConstructor> predicate)
Gets the constructors for the class matching the given predicate- Parameters:
predicate
- The predicate to use for filtering the constructors- Returns:
- the constructors for the class matching the given predicate
-
getConstructorsWithAnnotation
@SafeVarargs public final List<RConstructor> getConstructorsWithAnnotation(@NonNull @NonNull Class<? extends Annotation>... annotationClasses)
Gets the constructors for the class with at least of the given annotations.- Parameters:
annotationClasses
- The annotation classes to search for- Returns:
- the constructors for the class with at least of the given annotations.
-
getDeclaringClass
public Reflect getDeclaringClass()
Gets the class that declares the reflected object- Returns:
- the declaring class
-
getElement
public Class<?> getElement()
Gets the AnnotatedElement- Returns:
- the AnnotatedElement object
-
getField
public RField getField(String name) throws ReflectionException
Gets the field with the given name.- Parameters:
name
- the name of the field- Returns:
- the reflected field
- Throws:
ReflectionException
- No field found
-
getFieldsWhere
public List<RField> getFieldsWhere(@NonNull @NonNull SerializablePredicate<RField> predicate)
Gets all fields that match the given predicate- Parameters:
predicate
- the predicate to use for filtering the fields- Returns:
- the fields matching the given predicate
-
getFieldsWithAnnotation
@SafeVarargs public final List<RField> getFieldsWithAnnotation(@NonNull @NonNull Class<? extends Annotation>... annotationClasses)
Gets the fields for the class with at least of the given annotations.- Parameters:
annotationClasses
- The annotation classes to search for- Returns:
- the fields for the class with at least of the given annotations.
-
getMethod
public RMethod getMethod(String name) throws ReflectionException
Gets the method with the given name.- Parameters:
name
- the name of the method- Returns:
- the reflected method
- Throws:
ReflectionException
- No such method
-
getMethod
public RMethod getMethod(String name, Type... types) throws ReflectionException
Gets the method with the given name and with types compatible with the given types.- Parameters:
name
- the name of the methodtypes
- the types of the method parameters- Returns:
- the reflected method
- Throws:
ReflectionException
- No such method
-
getMethods
public List<RMethod> getMethods(String name)
Gets all methods with the given name.- Parameters:
name
- the name of the method- Returns:
- the list of reflected methods
-
getMethods
public List<RMethod> getMethods()
Gets all methods.- Returns:
- the list of reflected methods
-
getMethodsWhere
public List<RMethod> getMethodsWhere(String name, @NonNull @NonNull SerializablePredicate<? super RMethod> predicate)
Gets the methods with the given name for the class matching the given predicate- Parameters:
name
- the name of the methodpredicate
- The predicate to use for filtering the constructors- Returns:
- the methods for the class matching the given predicate
-
getMethodsWhere
public List<RMethod> getMethodsWhere(@NonNull @NonNull SerializablePredicate<? super RMethod> predicate)
Gets the methods for the class matching the given predicate- Parameters:
predicate
- The predicate to use for filtering the constructors- Returns:
- the methods for the class matching the given predicate
-
getMethodsWithAnnotation
@SafeVarargs public final List<RMethod> getMethodsWithAnnotation(@NonNull @NonNull Class<? extends Annotation>... annotationClasses)
Gets the methods for the class with at least of the given annotations.- Parameters:
annotationClasses
- The annotation classes to search for- Returns:
- the methods for the class with at least of the given annotations.
-
getModifiers
public int getModifiers()
Gets the modifiers on this field.- Returns:
- the modifiers
-
getName
public String getName()
Gets the name of the element- Returns:
- the name of the element
-
getSingletonMethod
public RMethod getSingletonMethod() throws ReflectionException
Gets the singleton method of the object.- Returns:
- the reflected singleton method
- Throws:
ReflectionException
- No Such Method
-
getSuperClass
public Reflect getSuperClass()
Gets the super-class of the class being reflected- Returns:
- the reflected super class
-
getType
public Class<?> getType()
Gets the type of the element- Returns:
- the type of the element
-
isPrivileged
public boolean isPrivileged()
is privileged access allowed on this object?- Returns:
- True - privileged access is allowed, False - no privileged access is allowed
-
isSingleton
public boolean isSingleton()
Does the reflected class have a singleton creation method (getInstance, getSingleton, or createInstance)- Returns:
- True - if the class being reflected is a singleton, False otherwise
-
set
public Reflect set(String name, Object value) throws ReflectionException
Convenience method for setting the value of a field or setter- Parameters:
name
- the name of the fieldvalue
- the value to set the field to- Returns:
- this Reflect object
- Throws:
ReflectionException
- Something went wrong accessing the field
-
setIsPrivileged
public Reflect setIsPrivileged(boolean allowPrivilegedAccess)
Sets whether or not privileged access is allowed on this object- Parameters:
allowPrivilegedAccess
- True - privileged access is allowed, False - no privileged access is allowed- Returns:
- this object
-
getAnnotation
public final <A extends Annotation> A getAnnotation(Class<A> annotationClass)
Gets the annotation of the given class present on this object.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation class- Returns:
- the annotation (null if does not exist)
-
getAnnotation
public final <A extends Annotation> A getAnnotation(@NonNull @NonNull Class<A> annotationClass, boolean onlyDirect)
Gets the annotation of the given class on this object. WhenonlyDirect
is true it will only return annotations directly present viagetDeclaredAnnotation
otherwise will return any instance present viagetAnnotation
.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation classonlyDirect
- True - only declared annotations, False present annotations- Returns:
- the annotation (null if does not exist)
-
getAnnotations
public final Annotation[] getAnnotations(boolean onlyDirect)
Gets all annotations on this object. WhenonlyDirect
is true it will only return annotations directly present viagetDeclaredAnnotations
otherwise will return any instance present viagetAnnotations
.- Parameters:
onlyDirect
- True - only declared annotations, False present annotations- Returns:
- the array of Annotation
-
getAnnotations
public final Annotation[] getAnnotations()
Gets all annotations present on this object.- Returns:
- the array of Annotation
-
getAnnotations
public final <A extends Annotation> A[] getAnnotations(@NonNull @NonNull Class<A> annotationClass, boolean onlyDirect)
Gets all annotations on this object. WhenonlyDirect
is true it will only return annotations directly present viagetDeclaredAnnotationsByType
otherwise will return any instance present viagetAnnotationsByType
.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation classonlyDirect
- True - only declared annotations, False present annotations- Returns:
- the array of Annotation
-
getAnnotations
public final <A extends Annotation> A[] getAnnotations(@NonNull @NonNull Class<A> annotationClass)
Gets all associated annotations of given type on this object.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation class- Returns:
- the array of Annotation
-
isAnnotationDeclared
@SafeVarargs public final boolean isAnnotationDeclared(@NonNull @NonNull Class<? extends Annotation>... annotationClasses)
Determines if any of the given annotations are declared on this object- Parameters:
annotationClasses
- the annotation classes- Returns:
- True - if any of the given annotations are declared on this object
-
isAnnotationPresent
@SafeVarargs public final boolean isAnnotationPresent(@NonNull @NonNull Class<? extends Annotation>... annotationClasses)
Determines if any of the given annotations are present on this object- Parameters:
annotationClasses
- the annotation classes- Returns:
- True - if any of the given annotations are present on this object
-
processAnnotation
public final <A extends Annotation,O> O processAnnotation(@NonNull @NonNull Class<A> annotationClass, @NonNull @NonNull CheckedFunction<? super A,? extends O> function) throws ReflectionException
Applies the givenCheckedBiFunction
to this object with the given annotation if it is present on the object.- Type Parameters:
A
- the annotation type parameterO
- the return type parameter- Parameters:
annotationClass
- the annotation classfunction
- the function to apply- Returns:
- the return value of the function or null if no annotation was present
- Throws:
ReflectionException
- Something went wrong during reflection
-
processAnnotations
public final <A extends Annotation,O> List<O> processAnnotations(@NonNull @NonNull Class<A> annotationClass, @NonNull @NonNull CheckedFunction<? super A,? extends O> function) throws ReflectionException
Applies the givenCheckedBiFunction
to this object and all instances of the given annotation present on the object.- Type Parameters:
A
- the annotation type parameterO
- the return type parameter- Parameters:
annotationClass
- the annotation classfunction
- the function to apply- Returns:
- A list of the return values of the function or null if no annotation was present
- Throws:
ReflectionException
- Something went wrong during reflection
-
withAnnotation
public final <A extends Annotation> V withAnnotation(@NonNull @NonNull Class<A> annotationClass, @NonNull @NonNull CheckedConsumer<? super A> consumer) throws ReflectionException
Applies the givenCheckedBiConsumer
to this object with the given annotation if it is present on the object.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation classconsumer
- the consumer to apply- Returns:
- This object
- Throws:
ReflectionException
- Something went wrong during reflection
-
withAnnotations
public final <A extends Annotation> V withAnnotations(@NonNull @NonNull Class<A> annotationClass, @NonNull @NonNull CheckedConsumer<? super A> consumer) throws ReflectionException
Applies the givenCheckedBiConsumer
to this object and all instances of the given annotation present on this object.- Type Parameters:
A
- the annotation type parameter- Parameters:
annotationClass
- the annotation classconsumer
- the consumer to apply- Returns:
- This object
- Throws:
ReflectionException
- Something went wrong during reflection
-
-