Reflector
Reflector is tiny java reflections library
Install / Use
/learn @alxkm/ReflectorREADME
Reflector <img src="https://www.svgrepo.com/show/144446/mirror-horizontally.svg" height="32px" alt="Reflector" />
<!--[](https://app.travis-ci.com/alxkm/reflector)-->Reflector is library, which offers a comprehensive suite of utilities for harnessing the power of the Java Reflection API. With a focus on simplicity and efficiency, it empowers developers to interact with class metadata, methods, fields, constructors, and annotations effortlessly.
Library main usage is:
- get class methods private or not
- get class fields
- select annotated fields
- select and clear some fields
- invoke new instance of class
- invoke object method
- read object field
- read object field as map
- other (get class name, package, super)
- get method
- get declared methods
- get default interfaces methods
Required java version is java 8
Java Reflection Utils
Features
- AnnotationUtils: Simplifies the handling of annotations on classes, methods, and fields.
- ClassBasicUtils: Provides fundamental utilities for working with class metadata.
- ConstructorUtils: Aids in accessing constructor-related information with ease.
- FieldsExtraUtils: Extends field-related utilities, offering additional functionalities like retrieving private fields and annotated fields.
- FieldUtils: Offers a wide range of utilities for field manipulation and access.
- GeneralUtils: Contains miscellaneous utility methods for various common tasks.
- InvokeUtils: Facilitates method invocation on objects and classes.
- MethodEnhancementsUtils: Enhances method-related utilities with additional functionalities.
- MethodUtils: Simplifies method-related tasks, such as method invocation and retrieval.
- MiscellaneousUtils: Houses miscellaneous utility methods for various purposes.
- ObjectUtils: Provides utilities for working with objects, including copying and comparison.
- PackageUtils: Facilitates working with packages, including class retrieval and scanning.
- ReflectionUtils: The core of the library, offering a wide range of reflection-related utilities.
- ReflectionUtilsLegacy: Contains legacy reflection utilities for compatibility purposes.
- SecurityUtils: Offers utilities for handling security-related tasks, ensuring safe reflection operations.
ReflectionUtils Documentation
Example Usage Quick start
Annotations
getClassAnnotations
Description: Retrieves all annotations present on the given class.
Parameters:
clazz(Class<?>): The class whose annotations are to be retrieved.
Returns:
Annotation[]: An array of annotations present on the given class.
Throws:
IllegalArgumentException: If the provided class is null.
Example Usage:
Annotation[] annotations = ReflectionUtils.getClassAnnotations(MyClass.class);
getAnnotationsByType
Description: Retrieves annotations by type from a class or element.
Parameters:
<T>: The type of the annotation to query for and return if present.element(AnnotatedElement): The element from which to get the annotations.annotationClass(Class<T>): The Class object corresponding to the annotation type.
Returns:
T[]: An array of all annotations of the specified annotation type if present on this element, else an empty array.
Throws:
NullPointerException: If the element or annotationClass is null.
Example Usage:
MyAnnotation[] annotations = ReflectionUtils.getAnnotationsByType(myElement, MyAnnotation.class);
getDeclaredAnnotations
Description: Gets annotations declared directly on a class, method, or field.
Parameters:
element(AnnotatedElement): The element from which to get the annotations.
Returns:
Annotation[]: An array of annotations directly declared on the element.
Throws:
NullPointerException: If the element is null.
Example Usage:
Annotation[] annotations = ReflectionUtils.getDeclaredAnnotations(myElement);
getMethodDeclaredAnnotations
Description: Retrieves the annotations declared on a method.
Parameters:
method(Method): The method to retrieve annotations from.
Returns:
Annotation[]: An array of annotations declared on the method.
Throws:
NullPointerException: If the method is null.
Example Usage:
Annotation[] annotations = ReflectionUtils.getMethodDeclaredAnnotations(myMethod);
getMethodsDeclaredAnnotations
Description: Retrieves a map of methods to their declared annotations for the given array of methods.
Parameters:
methods(Method[]): The array of methods whose declared annotations are to be retrieved.
Returns:
Map<Method, Annotation[]>: A map where the keys are the methods and the values are arrays of their declared annotations.
Throws:
NullPointerException: If the methods array is null.
Example Usage:
Map<Method, Annotation[]> methodAnnotations = ReflectionUtils.getMethodsDeclaredAnnotations(myMethodsArray);
isAnnotationOnClassPresent
Description: Checks if a specific annotation is present on the given class.
Parameters:
clazz(Class<?>): The class to check for the presence of the annotation.annotationClass(Class<T>): The annotation class to look for.
Returns:
boolean: True if the specified annotation is present on the class, false otherwise.
Throws:
NullPointerException: If the provided class or annotation class is null.
Example Usage:
boolean isPresent = ReflectionUtils.isAnnotationOnClassPresent(MyClass.class, MyAnnotation.class);
isMethodParameterAnnotated
Description: Checks if any parameter of the given method is annotated with the specified annotation class.
Parameters:
method(Method): The method whose parameters are to be checked.clazz(Class<T>): The annotation class to look for on the method parameters.
Returns:
boolean: True if any parameter of the method is annotated with the specified annotation, false otherwise.
Throws:
NullPointerException: If the provided method or annotation class is null.
Example Usage:
boolean isAnnotated = ReflectionUtils.isMethodParameterAnnotated(myMethod, MyAnnotation.class);
getFieldAnnotations
Description: Gets all annotations present on a given field.
Parameters:
field(Field): The field whose annotations are to be retrieved.
Returns:
Annotation[]: An array of annotations present on the field.
Throws:
NullPointerException: If the provided field is null.
Example Usage:
Annotation[] annotations = ReflectionUtils.getFieldAnnotations(myField);
isMethodAnnotated
Description: Checks if the given method is annotated with the specified annotation class.
Parameters:
method(Method): The method to check for the annotation.clazz(Class<T>): The annotation class to look for on the method.
Returns:
boolean: True if the method is annotated with the specified annotation, false otherwise.
Throws:
NullPointerException: If the provided method or annotation class is null.
Example Usage:
boolean isAnnotated = ReflectionUtils.isMethodAnnotated(myMethod, MyAnnotation.class);
Constructors
getConstructorParameters
Description: Retrieves the parameters of a constructor.
Parameters:
constructor(Constructor<?>): The constructor from which to retrieve parameters.
Returns:
Parameter[]: An array of parameters of the constructor.
Throws:
NullPointerException: If the constructor is null.
Example Usage:
Parameter[] parameters = ReflectionUtils.getConstructorParameters(myConstructor);
getConstructorModifiers
Description: Retrieves the modifiers of a constructor.
Parameters:
constructor(Constructor<?>): The constructor from which to retrieve modifiers.
Returns:
int: An integer representing the modifiers of the constructor.
Throws:
NullPointerException: If the constructor is null.
Example Usage:
int modifiers = ReflectionUtils.getConstructorModifiers(myConstructor);
getConstructors
Description: Retrieves all public constructors of the specified class.
Parameters:
clazz(Class<?>): The class from which to retrieve constructors.
Returns:
Constructor<?>[]: An array of public constructors of the specified class.
Throws:
NullPointerException: If the class is null.
Example Usage:
Constructor<?>[] constructors = ReflectionUtils.getConstructors(MyClass.class);
getDeclaredConstructors
Description: Retrieves all declared constructors of the specified class, including public, protected, default (package), and private constructors.
Parameters:
clazz(Class<?>): The class from which to retrieve declared constructors.
Returns:
Constructor<?>[]: An array of declared constructors of the specified class.
Throws:
NullPointerException: If the class is null.
Example Usage:
Constructor<?>[] constructors = ReflectionUtils.getDeclaredConstructors(MyClass.class);
Field Utils
Methods
getAllPrivateFields
Description: Retrieves all private fields of a given class, including fields declared in its superclasses.
Parameters:
clazz(Class<?>): The class from which to retrieve private fields.
Returns:
List<Field>: A list of all private fields of the specified class.
Throws:
- `NullPointerExcept
