Package org.reflector
Class AnnotationUtils
java.lang.Object
org.reflector.AnnotationUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Annotation>
T[]getAnnotationsByType(AnnotatedElement element, Class<T> annotationClass) Retrieves annotations by type from a class or element.static Annotation[]getClassAnnotations(Class<?> clazz) Retrieves all annotations present on the given class.static Annotation[]getDeclaredAnnotations(AnnotatedElement element) Gets annotations declared directly on a class, method, or field.static Annotation[]getFieldAnnotations(Field field) Gets all annotations present on a given field.static Annotation[]getMethodDeclaredAnnotations(Method method) Retrieves the annotations declared on a method.static Map<Method,Annotation[]> getMethodsDeclaredAnnotations(Method[] methods) Retrieves a map of methods to their declared annotations for the given array of methods.static <T extends Annotation>
booleanisAnnotationOnClassPresent(Class<?> clazz, Class<T> annotationClass) Checks if a specific annotation is present on the given class.static <T extends Annotation>
booleanisMethodAnnotated(Method method, Class<T> clazz) Checks if the given method is annotated with the specified annotation class.static <T extends Annotation>
booleanisMethodParameterAnnotated(Method method, Class<T> clazz) Checks if any parameter of the given method is annotated with the specified annotation class.
-
Method Details
-
getClassAnnotations
Retrieves all annotations present on the given class.- Parameters:
clazz- the class whose annotations are to be retrieved- Returns:
- an array of annotations present on the given class
- Throws:
NullPointerException- if the provided class is null
-
getAnnotationsByType
public static <T extends Annotation> T[] getAnnotationsByType(AnnotatedElement element, Class<T> annotationClass) Retrieves annotations by type from a class or element.- Type Parameters:
T- the type of the annotation to query for and return if present- Parameters:
element- the element from which to get the annotationsannotationClass- the Class object corresponding to the annotation type- Returns:
- 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
-
getDeclaredAnnotations
Gets annotations declared directly on a class, method, or field.- Parameters:
element- the element from which to get the annotations- Returns:
- an array of annotations directly declared on the element
- Throws:
NullPointerException- if the element is null
-
getMethodDeclaredAnnotations
Retrieves the annotations declared on a method.- Parameters:
method- the method to retrieve annotations from- Returns:
- an array of annotations declared on the method
- Throws:
NullPointerException- if the method is null
-
getMethodsDeclaredAnnotations
Retrieves a map of methods to their declared annotations for the given array of methods.Null entries in the array are skipped. If the same method appears more than once the first entry wins, rather than the call failing.
- Parameters:
methods- the array of methods whose declared annotations are to be retrieved- Returns:
- 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
-
isAnnotationOnClassPresent
public static <T extends Annotation> boolean isAnnotationOnClassPresent(Class<?> clazz, Class<T> annotationClass) Checks if a specific annotation is present on the given class.- Type Parameters:
T- the type of the annotation- Parameters:
clazz- the class to check for the presence of the annotationannotationClass- the annotation class to look for- Returns:
- true if the specified annotation is present on the class, false otherwise
- Throws:
NullPointerException- if the provided class or annotation class is null
-
isMethodParameterAnnotated
public static <T extends Annotation> boolean isMethodParameterAnnotated(Method method, Class<T> clazz) Checks if any parameter of the given method is annotated with the specified annotation class.- Type Parameters:
T- the type of the annotation- Parameters:
method- the method whose parameters are to be checkedclazz- the annotation class to look for on the method parameters- Returns:
- 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
-
getFieldAnnotations
Gets all annotations present on a given field.- Parameters:
field- the field whose annotations are to be retrieved- Returns:
- an array of annotations present on the field
- Throws:
NullPointerException- if the provided method or annotation class is null
-
isMethodAnnotated
Checks if the given method is annotated with the specified annotation class.- Type Parameters:
T- the type of the annotation- Parameters:
method- the method to check for the annotationclazz- the annotation class to look for on the method- Returns:
- true if the method is annotated with the specified annotation, false otherwise
- Throws:
NullPointerException- if the provided method or annotation class is null
-