Interface AnnotationUtils
-
- All Superinterfaces:
io.microsphere.util.Utils
public interface AnnotationUtils extends io.microsphere.util.Utils
A utility interface for working with annotations in thejavax.lang.model.*
package.This interface provides methods to retrieve, filter, and check annotations and their attributes from various constructs such as
Element
,TypeMirror
, and others commonly used in annotation processing.Example Usage
// Retrieve a specific annotation from an element AnnotationMirror annotation = AnnotationUtils.getAnnotation(element, MyAnnotation.class); // Retrieve all annotations from a type List<AnnotationMirror> annotations = AnnotationUtils.getAnnotations(typeMirror); // Check if an annotation is present on an element boolean present = AnnotationUtils.isAnnotationPresent(element, MyAnnotation.class); // Find meta-annotations on an annotation AnnotationMirror metaAnnotation = AnnotationUtils.findMetaAnnotation(annotation, MyMetaAnnotation.class);
This interface is intended to be implemented by classes that provide static utility methods for handling annotations during annotation processing. It helps in organizing and reusing common operations related to annotations.
- Since:
- 1.0.0
- Author:
- Mercy
-
-
Field Summary
Fields Modifier and Type Field Description static javax.lang.model.element.AnnotationValueVisitor<java.lang.Object,javax.lang.model.element.ExecutableElement>
DEFAULT_ANNOTATION_VALUE_VISITOR
The defaultAnnotationValueVisitor
static java.lang.annotation.ElementType[]
EMPTY_ELEMENT_TYPE_ARRAY
The emptyElementType
arraystatic java.lang.String
VALUE_ATTRIBUTE_NAME
The name of the attribute method : value()static boolean
WITH_DEFAULT
-
Method Summary
Static Methods Modifier and Type Method Description static java.util.List<javax.lang.model.element.AnnotationMirror>
findAllAnnotations(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.CharSequence annotatedTypeName, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances of the specified annotated type name from the givenProcessingEnvironment
.static java.util.List<javax.lang.model.element.AnnotationMirror>
findAllAnnotations(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.reflect.Type annotatedType, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances of the specified annotated type from the givenProcessingEnvironment
.static java.util.List<javax.lang.model.element.AnnotationMirror>
findAllAnnotations(javax.lang.model.element.Element element, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances from the givenElement
, applying the specified annotation filters to narrow down the results.static java.util.List<javax.lang.model.element.AnnotationMirror>
findAllAnnotations(javax.lang.model.element.TypeElement element, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
static java.util.List<javax.lang.model.element.AnnotationMirror>
findAllAnnotations(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances from the givenTypeMirror
, applying the specified annotation filters to narrow down the results.static javax.lang.model.element.AnnotationMirror
findAnnotation(javax.lang.model.element.Element element, java.lang.CharSequence annotationClassName)
Retrieves the firstAnnotationMirror
of the specified annotation class name from the givenElement
.static javax.lang.model.element.AnnotationMirror
findAnnotation(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves the firstAnnotationMirror
of the specified annotation class from the givenElement
.static javax.lang.model.element.AnnotationMirror
findAnnotation(javax.lang.model.type.TypeMirror type, java.lang.CharSequence annotationClassName)
Retrieves the firstAnnotationMirror
of the specified annotation class name from the givenTypeMirror
.static javax.lang.model.element.AnnotationMirror
findAnnotation(javax.lang.model.type.TypeMirror type, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves the firstAnnotationMirror
of the specified annotation class from the givenTypeMirror
.static java.util.List<javax.lang.model.element.AnnotationMirror>
findAnnotations(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances from the givenAnnotatedConstruct
that match the provided annotation filters.static javax.lang.model.element.AnnotationMirror
findMetaAnnotation(javax.lang.model.element.Element annotatedConstruct, java.lang.CharSequence metaAnnotationClassName)
Retrieves the first meta-annotation of the specified meta-annotation class name from the given annotated element.static javax.lang.model.element.AnnotationMirror
findMetaAnnotation(javax.lang.model.element.Element annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> metaAnnotationClass)
Retrieves the first meta-annotation of the specified meta-annotation class from the given annotated element.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAllAnnotations(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.reflect.Type annotatedType)
Retrieves allAnnotationMirror
instances of the specified annotated type from the givenProcessingEnvironment
.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAllAnnotations(javax.lang.model.element.Element element)
Retrieves allAnnotationMirror
instances from the givenElement
.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAllAnnotations(javax.lang.model.element.Element element, java.lang.CharSequence annotationClassName)
Retrieves allAnnotationMirror
instances of the specified annotation class name from the givenElement
.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAllAnnotations(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves allAnnotationMirror
instances of the specified annotation class from the givenElement
.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAllAnnotations(javax.lang.model.type.TypeMirror type)
Retrieves allAnnotationMirror
instances from the givenTypeMirror
.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAllAnnotations(javax.lang.model.type.TypeMirror type, java.lang.CharSequence annotationClassName)
Retrieves allAnnotationMirror
instances of the specified annotation class name from the givenTypeMirror
.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAllAnnotations(javax.lang.model.type.TypeMirror type, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves allAnnotationMirror
instances of the specified annotation class from the givenTypeMirror
.static javax.lang.model.element.AnnotationMirror
getAnnotation(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.CharSequence annotationClassName)
Retrieves the firstAnnotationMirror
of the specified annotation class name from the givenAnnotatedConstruct
.static javax.lang.model.element.AnnotationMirror
getAnnotation(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves the firstAnnotationMirror
of the specified annotation class from the givenAnnotatedConstruct
.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAnnotations(javax.lang.model.AnnotatedConstruct annotatedConstruct)
Retrieves allAnnotationMirror
instances from the givenAnnotatedConstruct
.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAnnotations(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.CharSequence annotationClassName)
Retrieves allAnnotationMirror
instances of the specified annotation class name from the givenAnnotatedConstruct
.static java.util.List<javax.lang.model.element.AnnotationMirror>
getAnnotations(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves allAnnotationMirror
instances of the specified annotation class from the givenAnnotatedConstruct
.static <T> T
getAttribute(java.util.Map.Entry<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> elementValue)
Retrieves the value of the specified attribute from the provided entry containing the attribute method and its corresponding annotation value.static <T> T
getAttribute(javax.lang.model.element.AnnotationMirror annotation, java.lang.String attributeName)
Retrieves the value of the specified attribute from the given annotation.static <T> T
getAttribute(javax.lang.model.element.AnnotationMirror annotation, java.lang.String attributeName, boolean withDefault)
Retrieves the value of the specified attribute from the given annotation, optionally including the default value.static java.lang.String
getAttributeName(javax.lang.model.element.ExecutableElement attributeMethod)
Retrieves the name of the attribute method from the givenExecutableElement
.static java.util.Map<java.lang.String,java.lang.Object>
getAttributesMap(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves a map of attribute names to their corresponding values from the first matching annotation of the specified class on the given annotated construct.static java.util.Map<java.lang.String,java.lang.Object>
getAttributesMap(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass, boolean withDefault)
Retrieves a map of attribute names to their corresponding values from the first matching annotation of the specified class on the given annotated construct.static java.util.Map<java.lang.String,java.lang.Object>
getAttributesMap(javax.lang.model.element.AnnotationMirror annotation)
Retrieves a map of attribute names to their corresponding values from the specified annotation.static java.util.Map<java.lang.String,java.lang.Object>
getAttributesMap(javax.lang.model.element.AnnotationMirror annotation, boolean withDefault)
Retrieves a map of attribute names to their corresponding values from the specified annotation.static java.lang.annotation.ElementType[]
getElementTypes(javax.lang.model.element.AnnotationMirror annotation)
Retrieves theElementType
array from the specified annotation.static java.lang.annotation.ElementType[]
getElementTypes(javax.lang.model.type.DeclaredType annotationType)
Retrieves theElementType
array from the specified annotation type by checking theTarget
annotation associated with it.static java.util.Map.Entry<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue>
getElementValue(java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> elementValues, java.lang.String attributeName)
Retrieves the attribute method and its corresponding annotation value from the specified element values map based on the given attribute name.static java.util.Map.Entry<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue>
getElementValue(javax.lang.model.element.AnnotationMirror annotation, java.lang.String attributeName, boolean withDefault)
Retrieves the attribute method and its corresponding annotation value from the specified annotation based on the given attribute name.static java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue>
getElementValues(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves the map of annotation attribute methods to their corresponding values from the specifiedAnnotatedConstruct
and annotation class.static java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue>
getElementValues(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass, boolean withDefault)
Retrieves the map of annotation attribute methods to their corresponding values from the specifiedAnnotatedConstruct
and annotation class.static java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue>
getElementValues(javax.lang.model.element.AnnotationMirror annotation)
Retrieves a map of annotation attribute methods to their corresponding values from the specifiedAnnotationMirror
.static java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue>
getElementValues(javax.lang.model.element.AnnotationMirror annotation, boolean withDefault)
Retrieves a map of annotation attribute methods to their corresponding values from the specifiedAnnotationMirror
.static <T> T
getValue(javax.lang.model.element.AnnotationMirror annotation)
Retrieves the value of the default attribute method named"value"
from the specified annotation.static boolean
isAnnotationPresent(javax.lang.model.element.Element element, java.lang.CharSequence annotationClassName)
Checks if the specified annotation (by class name) is present on the given element, either directly or as a meta-annotation.static boolean
isAnnotationPresent(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Checks if the specified annotation is present on the given element, either directly or as a meta-annotation.static boolean
matchesAnnotationType(javax.lang.model.element.AnnotationMirror annotationMirror, java.lang.reflect.Type annotationType)
Checks if the given annotation mirror has the same type as the specifiedType
.static boolean
matchesAnnotationTypeName(javax.lang.model.element.AnnotationMirror annotationMirror, java.lang.CharSequence annotationTypeName)
Checks if the given annotation mirror has the same type as the specified annotation class name.static boolean
matchesAttributeMethod(javax.lang.model.element.ExecutableElement attributeMethod, java.lang.String attributeName)
Checks if the provided executable element represents an annotation attribute method with the specified name.static boolean
matchesAttributeValue(javax.lang.model.element.AnnotationValue annotationValue, java.lang.Object attributeValue)
Checks if the value of the givenAnnotationValue
matches the specified attribute value.static boolean
matchesAttributeValue(javax.lang.model.element.AnnotationValue one, javax.lang.model.element.AnnotationValue another)
Checks if two givenAnnotationValue
instances are equal by comparing their values.static boolean
matchesDefaultAttributeValue(javax.lang.model.element.ExecutableElement attributeMethod, javax.lang.model.element.AnnotationValue annotationValue)
Checks if the provided annotation value matches the default value of the specified attribute method.
-
-
-
Field Detail
-
VALUE_ATTRIBUTE_NAME
static final java.lang.String VALUE_ATTRIBUTE_NAME
The name of the attribute method : value()- See Also:
- Constant Field Values
-
DEFAULT_ANNOTATION_VALUE_VISITOR
static final javax.lang.model.element.AnnotationValueVisitor<java.lang.Object,javax.lang.model.element.ExecutableElement> DEFAULT_ANNOTATION_VALUE_VISITOR
The defaultAnnotationValueVisitor
-
EMPTY_ELEMENT_TYPE_ARRAY
@Immutable static final java.lang.annotation.ElementType[] EMPTY_ELEMENT_TYPE_ARRAY
The emptyElementType
array
-
WITH_DEFAULT
static final boolean WITH_DEFAULT
- See Also:
- Constant Field Values
-
-
Method Detail
-
getAnnotation
static javax.lang.model.element.AnnotationMirror getAnnotation(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves the firstAnnotationMirror
of the specified annotation class from the givenAnnotatedConstruct
. If either the construct or the annotation class isnull
, this method returnsnull
.Example Usage
// Get an annotation directly present on a class TypeElement typeElement = ...; // obtain a TypeElement AnnotationMirror annotation = AnnotationUtils.getAnnotation(typeElement, MyAnnotation.class); // Get an annotation from an element that may inherit annotations from its parent Element element = ...; // obtain an Element annotation = AnnotationUtils.getAnnotation(element, MyAnnotation.class);
- Parameters:
annotatedConstruct
- the annotated construct to search for annotations, may benull
annotationClass
- the annotation class to look for, may benull
- Returns:
- the first matching
AnnotationMirror
, ornull
if none found
-
getAnnotation
static javax.lang.model.element.AnnotationMirror getAnnotation(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.CharSequence annotationClassName)
Retrieves the firstAnnotationMirror
of the specified annotation class name from the givenAnnotatedConstruct
. If either the construct or the annotation class name isnull
, this method returnsnull
.Example Usage
// Get an annotation by its class name from an element TypeElement typeElement = ...; // obtain a TypeElement String annotationClassName = "com.example.MyAnnotation"; AnnotationMirror annotation = AnnotationUtils.getAnnotation(typeElement, annotationClassName); // Get an annotation from a method element Element methodElement = ...; // obtain a method Element annotation = AnnotationUtils.getAnnotation(methodElement, "com.example.AnotherAnnotation");
- Parameters:
annotatedConstruct
- the annotated construct to search for annotations, may benull
annotationClassName
- the fully qualified class name of the annotation to look for, may benull
- Returns:
- the first matching
AnnotationMirror
, ornull
if none found
-
getAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAnnotations(javax.lang.model.AnnotatedConstruct annotatedConstruct)
Retrieves allAnnotationMirror
instances from the givenAnnotatedConstruct
. If the annotated construct isnull
, this method returns an empty list.Example Usage
// Retrieve all annotations from a class element TypeElement typeElement = ...; // obtain a TypeElement List<AnnotationMirror> annotations = AnnotationUtils.getAnnotations(typeElement); // Retrieve annotations from a method element Element methodElement = ...; // obtain a method Element annotations = AnnotationUtils.getAnnotations(methodElement);
- Parameters:
annotatedConstruct
- the annotated construct to search for annotations, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
getAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAnnotations(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves allAnnotationMirror
instances of the specified annotation class from the givenAnnotatedConstruct
. If either the construct or the annotation class isnull
, this method returns an empty list.Example Usage
// Retrieve all annotations of a specific type from a class element TypeElement typeElement = ...; // obtain a TypeElement List<AnnotationMirror> annotations = AnnotationUtils.getAnnotations(typeElement, MyAnnotation.class); // Retrieve annotations from a method element Element methodElement = ...; // obtain a method Element annotations = AnnotationUtils.getAnnotations(methodElement, AnotherAnnotation.class);
- Parameters:
annotatedConstruct
- the annotated construct to search for annotations, may benull
annotationClass
- the annotation class to look for, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
getAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAnnotations(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.CharSequence annotationClassName)
Retrieves allAnnotationMirror
instances of the specified annotation class name from the givenAnnotatedConstruct
. If either the construct or the annotation class name isnull
, this method returns an empty list.Example Usage
// Retrieve annotations by their class name from a class element TypeElement typeElement = ...; // obtain a TypeElement String annotationClassName = "com.example.MyAnnotation"; List<AnnotationMirror> annotations = AnnotationUtils.getAnnotations(typeElement, annotationClassName); // Retrieve annotations from a method element Element methodElement = ...; // obtain a method Element annotations = AnnotationUtils.getAnnotations(methodElement, "com.example.AnotherAnnotation");
- Parameters:
annotatedConstruct
- the annotated construct to search for annotations, may benull
annotationClassName
- the fully qualified class name of the annotation to look for, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
getAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAllAnnotations(javax.lang.model.type.TypeMirror type)
Retrieves allAnnotationMirror
instances from the givenTypeMirror
. If the type mirror isnull
, this method returns an empty list.Example Usage
// Retrieve all annotations from a type mirror TypeMirror type = ...; // obtain a TypeMirror List<AnnotationMirror> annotations = AnnotationUtils.getAllAnnotations(type); // Handle cases where the type might be null annotations = AnnotationUtils.getAllAnnotations(null); // returns empty list
- Parameters:
type
- the type mirror to search for annotations, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
getAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAllAnnotations(javax.lang.model.element.Element element)
Retrieves allAnnotationMirror
instances from the givenElement
. If the element isnull
, this method returns an empty list.This method is designed to provide a consistent way of retrieving annotations across different constructs in the annotation processing framework.
Example Usage
// Retrieve all annotations from a class element TypeElement typeElement = ...; // obtain a TypeElement List<AnnotationMirror> annotations = AnnotationUtils.getAllAnnotations(typeElement); // Retrieve annotations from a method element Element methodElement = ...; // obtain a method Element annotations = AnnotationUtils.getAllAnnotations(methodElement); // Handle cases where the element might be null annotations = AnnotationUtils.getAllAnnotations(null); // returns empty list
- Parameters:
element
- the annotated element to search for annotations, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
getAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAllAnnotations(javax.lang.model.type.TypeMirror type, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves allAnnotationMirror
instances of the specified annotation class from the givenTypeMirror
. If either the type or the annotation class isnull
, this method returns an empty list.Example Usage
// Retrieve annotations from a type mirror TypeMirror type = ...; // obtain a TypeMirror List<AnnotationMirror> annotations = AnnotationUtils.getAllAnnotations(type, MyAnnotation.class); // Handle cases where the type might be null annotations = AnnotationUtils.getAllAnnotations(null, MyAnnotation.class); // returns empty list // Handle cases where the annotation class might be null annotations = AnnotationUtils.getAllAnnotations(type, null); // returns empty list
- Parameters:
type
- the type mirror to search for annotations, may benull
annotationClass
- the annotation class to look for, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
getAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAllAnnotations(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves allAnnotationMirror
instances of the specified annotation class from the givenElement
. If either the element or the annotation class isnull
, this method returns an empty list.Example Usage
// Retrieve annotations from a class element TypeElement typeElement = ...; // obtain a TypeElement List<AnnotationMirror> annotations = AnnotationUtils.getAllAnnotations(typeElement, MyAnnotation.class); // Retrieve annotations from a method element Element methodElement = ...; // obtain a method Element annotations = AnnotationUtils.getAllAnnotations(methodElement, AnotherAnnotation.class); // Handle cases where the element might be null annotations = AnnotationUtils.getAllAnnotations(null, MyAnnotation.class); // returns empty list // Handle cases where the annotation class might be null annotations = AnnotationUtils.getAllAnnotations(typeElement, null); // returns empty list
- Parameters:
element
- the annotated element to search for annotations, may benull
annotationClass
- the annotation class to look for, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
getAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAllAnnotations(javax.lang.model.type.TypeMirror type, java.lang.CharSequence annotationClassName)
Retrieves allAnnotationMirror
instances of the specified annotation class name from the givenTypeMirror
. If either the type or the annotation class name isnull
, this method returns an empty list.Example Usage
// Retrieve annotations from a type mirror TypeMirror type = ...; // obtain a TypeMirror String annotationClassName = "com.example.MyAnnotation"; List<AnnotationMirror> annotations = AnnotationUtils.getAllAnnotations(type, annotationClassName); // Handle cases where the type might be null annotations = AnnotationUtils.getAllAnnotations(null, annotationClassName); // returns empty list // Handle cases where the annotation class name might be null annotations = AnnotationUtils.getAllAnnotations(type, null); // returns empty list
- Parameters:
type
- the type mirror to search for annotations, may benull
annotationClassName
- the fully qualified class name of the annotation to look for, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
getAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAllAnnotations(javax.lang.model.element.Element element, java.lang.CharSequence annotationClassName)
Retrieves allAnnotationMirror
instances of the specified annotation class name from the givenElement
. If either the element or the annotation class name isnull
, this method returns an empty list.Example Usage
// Retrieve annotations by their class name from a class element TypeElement typeElement = ...; // obtain a TypeElement String annotationClassName = "com.example.MyAnnotation"; List<AnnotationMirror> annotations = AnnotationUtils.getAllAnnotations(typeElement, annotationClassName); // Retrieve annotations from a method element Element methodElement = ...; // obtain a method Element annotations = AnnotationUtils.getAllAnnotations(methodElement, "com.example.AnotherAnnotation"); // Handle cases where the element might be null annotations = AnnotationUtils.getAllAnnotations(null, annotationClassName); // returns empty list // Handle cases where the annotation class name might be null annotations = AnnotationUtils.getAllAnnotations(typeElement, null); // returns empty list
- Parameters:
element
- the annotated element to search for annotations, may benull
annotationClassName
- the fully qualified class name of the annotation to look for, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
getAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> getAllAnnotations(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.reflect.Type annotatedType)
Retrieves allAnnotationMirror
instances of the specified annotated type from the givenProcessingEnvironment
. If either the processing environment or the annotated type isnull
, this method returns an empty list.Example Usage
// Retrieve annotations for a specific type from the processing environment ProcessingEnvironment processingEnv = ...; // obtain a ProcessingEnvironment instance Type annotatedType = MyAnnotation.class; // specify the annotation type List<AnnotationMirror> annotations = AnnotationUtils.getAllAnnotations(processingEnv, annotatedType); // Handle cases where the processing environment might be null annotations = AnnotationUtils.getAllAnnotations(null, annotatedType); // returns empty list // Handle cases where the annotated type might be null annotations = AnnotationUtils.getAllAnnotations(processingEnv, null); // returns empty list
- Parameters:
processingEnv
- the processing environment used to retrieve annotations, may benull
annotatedType
- the annotated type to search for annotations, may benull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
findAnnotation
static javax.lang.model.element.AnnotationMirror findAnnotation(javax.lang.model.type.TypeMirror type, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves the firstAnnotationMirror
of the specified annotation class from the givenTypeMirror
. If either the type or the annotation class isnull
, this method returnsnull
.Example Usage
// Retrieve an annotation from a type mirror TypeMirror type = ...; // obtain a TypeMirror instance AnnotationMirror annotation = AnnotationUtils.findAnnotation(type, MyAnnotation.class); // Handle cases where the type might be null annotation = AnnotationUtils.findAnnotation(null, MyAnnotation.class); // returns null // Handle cases where the annotation class might be null annotation = AnnotationUtils.findAnnotation(type, null); // returns null
- Parameters:
type
- the type mirror to search for annotations, may benull
annotationClass
- the annotation class to look for, may benull
- Returns:
- the first matching
AnnotationMirror
, ornull
if none found
-
findAnnotation
static javax.lang.model.element.AnnotationMirror findAnnotation(javax.lang.model.type.TypeMirror type, java.lang.CharSequence annotationClassName)
Retrieves the firstAnnotationMirror
of the specified annotation class name from the givenTypeMirror
. If either the type or the annotation class name isnull
, this method returnsnull
.Example Usage
// Retrieve an annotation by its class name from a type mirror TypeMirror type = ...; // obtain a TypeMirror instance String annotationClassName = "com.example.MyAnnotation"; AnnotationMirror annotation = AnnotationUtils.findAnnotation(type, annotationClassName); // Handle cases where the type might be null annotation = AnnotationUtils.findAnnotation(null, annotationClassName); // returns null // Handle cases where the annotation class name might be null annotation = AnnotationUtils.findAnnotation(type, null); // returns null
- Parameters:
type
- the type mirror to search for annotations, may benull
annotationClassName
- the fully qualified class name of the annotation to look for, may benull
- Returns:
- the first matching
AnnotationMirror
, ornull
if none found
-
findAnnotation
static javax.lang.model.element.AnnotationMirror findAnnotation(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves the firstAnnotationMirror
of the specified annotation class from the givenElement
. If either the element or the annotation class isnull
, this method returnsnull
.Example Usage
// Retrieve an annotation from a class element TypeElement typeElement = ...; // obtain a TypeElement AnnotationMirror annotation = AnnotationUtils.findAnnotation(typeElement, MyAnnotation.class); // Retrieve an annotation from a method element Element methodElement = ...; // obtain a method Element annotation = AnnotationUtils.findAnnotation(methodElement, AnotherAnnotation.class); // Handle cases where the element might be null annotation = AnnotationUtils.findAnnotation(null, MyAnnotation.class); // returns null // Handle cases where the annotation class might be null annotation = AnnotationUtils.findAnnotation(typeElement, null); // returns null
- Parameters:
element
- the annotated element to search for annotations, may benull
annotationClass
- the annotation class to look for, may benull
- Returns:
- the first matching
AnnotationMirror
, ornull
if none found
-
findAnnotation
static javax.lang.model.element.AnnotationMirror findAnnotation(javax.lang.model.element.Element element, java.lang.CharSequence annotationClassName)
Retrieves the firstAnnotationMirror
of the specified annotation class name from the givenElement
. If either the element or the annotation class name isnull
, this method returnsnull
.Example Usage
// Retrieve an annotation by its class name from a class element TypeElement typeElement = ...; // obtain a TypeElement String annotationClassName = "com.example.MyAnnotation"; AnnotationMirror annotation = AnnotationUtils.findAnnotation(typeElement, annotationClassName); // Retrieve an annotation from a method element Element methodElement = ...; // obtain a method Element annotation = AnnotationUtils.findAnnotation(methodElement, "com.example.AnotherAnnotation"); // Handle cases where the element might be null annotation = AnnotationUtils.findAnnotation(null, annotationClassName); // returns null // Handle cases where the annotation class name might be null annotation = AnnotationUtils.findAnnotation(typeElement, null); // returns null
- Parameters:
element
- the annotated element to search for annotations, may benull
annotationClassName
- the fully qualified class name of the annotation to look for, may benull
- Returns:
- the first matching
AnnotationMirror
, ornull
if none found
-
findMetaAnnotation
static javax.lang.model.element.AnnotationMirror findMetaAnnotation(javax.lang.model.element.Element annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> metaAnnotationClass)
Retrieves the first meta-annotation of the specified meta-annotation class from the given annotated element. A meta-annotation is an annotation that is present on another annotation. If either the annotated element or the meta-annotation class isnull
, this method returnsnull
.Example Usage
// Retrieve a meta-annotation from an annotation on a class element TypeElement typeElement = ...; // obtain a TypeElement AnnotationMirror metaAnnotation = AnnotationUtils.findMetaAnnotation(typeElement, MyMetaAnnotation.class); // Retrieve a meta-annotation from an annotation on a method element Element methodElement = ...; // obtain a method Element metaAnnotation = AnnotationUtils.findMetaAnnotation(methodElement, AnotherMetaAnnotation.class); // Handle cases where the element might be null metaAnnotation = AnnotationUtils.findMetaAnnotation(null, MyMetaAnnotation.class); // returns null // Handle cases where the meta-annotation class might be null metaAnnotation = AnnotationUtils.findMetaAnnotation(typeElement, null); // returns null
- Parameters:
annotatedConstruct
- the annotated element to search for meta-annotations, may benull
metaAnnotationClass
- the annotation class to look for as a meta-annotation, may benull
- Returns:
- the first matching meta-
AnnotationMirror
, ornull
if none found
-
findMetaAnnotation
static javax.lang.model.element.AnnotationMirror findMetaAnnotation(javax.lang.model.element.Element annotatedConstruct, java.lang.CharSequence metaAnnotationClassName)
Retrieves the first meta-annotation of the specified meta-annotation class name from the given annotated element. A meta-annotation is an annotation that is present on another annotation. If either the annotated element or the meta-annotation class name isnull
, this method returnsnull
.Example Usage
// Retrieve a meta-annotation by its class name from a class element TypeElement typeElement = ...; // obtain a TypeElement String metaAnnotationClassName = "com.example.MyMetaAnnotation"; AnnotationMirror metaAnnotation = AnnotationUtils.findMetaAnnotation(typeElement, metaAnnotationClassName); // Retrieve a meta-annotation from a method element Element methodElement = ...; // obtain a method Element metaAnnotation = AnnotationUtils.findMetaAnnotation(methodElement, "com.example.AnotherMetaAnnotation"); // Handle cases where the element might be null metaAnnotation = AnnotationUtils.findMetaAnnotation(null, metaAnnotationClassName); // returns null // Handle cases where the meta-annotation class name might be null metaAnnotation = AnnotationUtils.findMetaAnnotation(typeElement, null); // returns null
- Parameters:
annotatedConstruct
- the annotated element to search for meta-annotations, may benull
metaAnnotationClassName
- the fully qualified class name of the meta-annotation to look for, may benull
- Returns:
- the first matching meta-
AnnotationMirror
, ornull
if none found
-
isAnnotationPresent
static boolean isAnnotationPresent(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Checks if the specified annotation is present on the given element, either directly or as a meta-annotation.Example Usage
// Check if an annotation is present on a class element TypeElement typeElement = ...; // obtain a TypeElement boolean present = AnnotationUtils.isAnnotationPresent(typeElement, MyAnnotation.class); // Check if an annotation is present on a method element Element methodElement = ...; // obtain a method Element present = AnnotationUtils.isAnnotationPresent(methodElement, AnotherAnnotation.class); // Handle cases where the element might be null present = AnnotationUtils.isAnnotationPresent(null, MyAnnotation.class); // returns false // Handle cases where the annotation class might be null present = AnnotationUtils.isAnnotationPresent(typeElement, null); // returns false
- Parameters:
element
- the element to check for the presence of the annotation; may benull
annotationClass
- the annotation class to look for; may benull
- Returns:
true
if the annotation is present (either directly or as a meta-annotation),false
otherwise or if either parameter isnull
-
isAnnotationPresent
static boolean isAnnotationPresent(javax.lang.model.element.Element element, java.lang.CharSequence annotationClassName)
Checks if the specified annotation (by class name) is present on the given element, either directly or as a meta-annotation.Example Usage
// Check if an annotation is present on a class element TypeElement typeElement = ...; // obtain a TypeElement String annotationClassName = "com.example.MyAnnotation"; boolean present = AnnotationUtils.isAnnotationPresent(typeElement, annotationClassName); // Check if an annotation is present on a method element Element methodElement = ...; // obtain a method Element present = AnnotationUtils.isAnnotationPresent(methodElement, "com.example.AnotherAnnotation"); // Handle cases where the element might be null present = AnnotationUtils.isAnnotationPresent(null, annotationClassName); // returns false // Handle cases where the annotation class name might be null present = AnnotationUtils.isAnnotationPresent(typeElement, null); // returns false
- Parameters:
element
- the element to check for the presence of the annotation; may benull
annotationClassName
- the fully qualified class name of the annotation to look for; may benull
- Returns:
true
if the annotation is present (either directly or as a meta-annotation),false
otherwise or if either parameter isnull
-
findAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> findAnnotations(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances from the givenAnnotatedConstruct
that match the provided annotation filters.If the annotated construct is
null
, this method returns an empty list. If no annotation filters are provided, all annotations present on the construct are returned. Otherwise, only annotations that satisfy all the provided predicates are included in the result.Example Usage
// Retrieve all annotations from a class element TypeElement typeElement = ...; // obtain a TypeElement List<AnnotationMirror> allAnnotations = AnnotationUtils.findAnnotations(typeElement); // Retrieve annotations that match a specific condition List<AnnotationMirror> filteredAnnotations = AnnotationUtils.findAnnotations(typeElement, annotation -> "com.example.MyAnnotation".contentEquals(annotation.getAnnotationType().asElement().toString())); // Retrieve annotations that match multiple conditions List<AnnotationMirror> multiFilteredAnnotations = AnnotationUtils.findAnnotations(typeElement, annotation -> isAnnotationPresent(annotation, "com.example.MetaAnnotation"), annotation -> annotation.getElementValues().size() > 1); // Handle cases where the annotated construct is null List<AnnotationMirror> annotations = AnnotationUtils.findAnnotations(null); // returns empty list
- Parameters:
annotatedConstruct
- the annotated construct to search for annotations, may benull
annotationFilters
- a varargs array of predicates used to filter annotations; may be empty ornull
- Returns:
- a non-null immutable list of matching
AnnotationMirror
instances; nevernull
-
findAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> findAllAnnotations(javax.lang.model.type.TypeMirror type, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances from the givenTypeMirror
, applying the specified annotation filters to narrow down the results. If the type mirror isnull
, this method returns an empty list.This method delegates to
findAllAnnotations(TypeElement, Predicate[])
after converting theTypeMirror
to aTypeElement
usingTypeUtils.ofTypeElement(TypeMirror)
.- Parameters:
type
- the type mirror to search for annotations, may benull
annotationFilters
- a varargs array of predicates used to filter annotations; may be empty ornull
- Returns:
- a non-null immutable list of matching
AnnotationMirror
instances; nevernull
-
findAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> findAllAnnotations(javax.lang.model.element.TypeElement element, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
-
findAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> findAllAnnotations(javax.lang.model.element.Element element, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances from the givenElement
, applying the specified annotation filters to narrow down the results. If the element isnull
, this method returns an empty list.This method attempts to resolve the element into a
TypeElement
. If successful, it delegates tofindAllAnnotations(TypeElement, Predicate[])
to retrieve annotations from the type hierarchy. Otherwise, it falls back to usingfindAnnotations(AnnotatedConstruct, Predicate[])
directly on the element.- Parameters:
element
- the annotated element to search for annotations, may benull
annotationFilters
- a varargs array of predicates used to filter annotations; may be empty ornull
- Returns:
- a non-null immutable list of matching
AnnotationMirror
instances; nevernull
-
findAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> findAllAnnotations(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.reflect.Type annotatedType, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances of the specified annotated type from the givenProcessingEnvironment
. If either the processing environment or the annotated type isnull
, this method returns an empty list.This method uses the fully qualified type name of the provided
Type
to locate the corresponding annotations. It delegates to the overloaded method that accepts aCharSequence
for the type name.- Parameters:
processingEnv
- the processing environment used to retrieve annotations, may benull
annotatedType
- the annotated type to search for annotations, may benull
annotationFilters
- a varargs array of predicates used to filter annotations; may be empty ornull
- Returns:
- a non-null immutable list of
AnnotationMirror
instances; nevernull
-
findAllAnnotations
@Nonnull @Immutable static java.util.List<javax.lang.model.element.AnnotationMirror> findAllAnnotations(javax.annotation.processing.ProcessingEnvironment processingEnv, java.lang.CharSequence annotatedTypeName, java.util.function.Predicate<? super javax.lang.model.element.AnnotationMirror>... annotationFilters)
Retrieves allAnnotationMirror
instances of the specified annotated type name from the givenProcessingEnvironment
. If either the processing environment or the annotated type name isnull
, this method returns an empty list.This method resolves the annotated type by delegating to
TypeUtils.getTypeElement(ProcessingEnvironment, CharSequence)
, and then uses the resolved type to find annotations with the provided filters.- Parameters:
processingEnv
- the processing environment used to retrieve annotations, may benull
annotatedTypeName
- the fully qualified class name of the annotation to look for, may benull
annotationFilters
- a varargs array of predicates used to filter annotations; may be empty ornull
- Returns:
- a non-null immutable list of matching
AnnotationMirror
instances; nevernull
-
matchesAnnotationType
static boolean matchesAnnotationType(javax.lang.model.element.AnnotationMirror annotationMirror, java.lang.reflect.Type annotationType)
Checks if the given annotation mirror has the same type as the specifiedType
.This method compares the fully qualified name of the annotation mirror's type with the provided type's name. Returns
false
if either parameter isnull
.Example Usage
AnnotationMirror annotationMirror = ...; // obtain an AnnotationMirror instance Type annotationType = ...; // obtain a Type instance // Check if both are non-null and the types match boolean isMatch = AnnotationUtils.matchesAnnotationType(annotationMirror, annotationType); // Handle cases where the annotation mirror might be null isMatch = AnnotationUtils.matchesAnnotationType(null, annotationType); // returns false // Handle cases where the annotation type might be null isMatch = AnnotationUtils.matchesAnnotationType(annotationMirror, null); // returns false
- Parameters:
annotationMirror
- the annotation mirror to compare; may benull
annotationType
- the target type to match against; may benull
- Returns:
true
if both parameters are non-null and their types match by name;false
otherwise
-
matchesAnnotationTypeName
static boolean matchesAnnotationTypeName(javax.lang.model.element.AnnotationMirror annotationMirror, java.lang.CharSequence annotationTypeName)
Checks if the given annotation mirror has the same type as the specified annotation class name.This method compares the fully qualified name of the annotation mirror's type with the provided annotation class name. Returns
false
if either parameter isnull
.Example Usage
AnnotationMirror annotationMirror = ...; // obtain an AnnotationMirror instance String annotationClassName = "com.example.MyAnnotation"; // Check if both are non-null and the types match boolean isMatch = AnnotationUtils.matchesAnnotationTypeName(annotationMirror, annotationClassName); // Handle cases where the annotation mirror might be null isMatch = AnnotationUtils.matchesAnnotationTypeName(null, annotationClassName); // returns false // Handle cases where the annotation class name might be null isMatch = AnnotationUtils.matchesAnnotationTypeName(annotationMirror, null); // returns false
- Parameters:
annotationMirror
- the annotation mirror to compare; may benull
annotationTypeName
- the target annotation class name to match against; may benull
- Returns:
true
if both parameters are non-null and their types match by name;false
otherwise
-
getAttributeName
static java.lang.String getAttributeName(javax.lang.model.element.ExecutableElement attributeMethod)
Retrieves the name of the attribute method from the givenExecutableElement
.This method is typically used to extract the attribute name from an annotation method declaration. The returned name corresponds to the method's simple name as defined in the annotation interface.
Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value(); int priority() default 0; } // Retrieve attribute names from methods ExecutableElement valueMethod = ...; // method representing 'value()' ExecutableElement priorityMethod = ...; // method representing 'priority()' String valueName = AnnotationUtils.getAttributeName(valueMethod); // returns "value" String priorityName = AnnotationUtils.getAttributeName(priorityMethod); // returns "priority" // Handle cases where the executable element is null String name = AnnotationUtils.getAttributeName(null); // returns null
- Parameters:
attributeMethod
- the executable element representing the annotation attribute method, may benull
- Returns:
- the name of the attribute method, or
null
if the provided element isnull
-
matchesAttributeMethod
static boolean matchesAttributeMethod(javax.lang.model.element.ExecutableElement attributeMethod, java.lang.String attributeName)
Checks if the provided executable element represents an annotation attribute method with the specified name.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value(); int priority() default 0; } // Check if the executable element corresponds to the "value" method ExecutableElement valueMethod = ...; // method representing 'value()' boolean isValueMethod = AnnotationUtils.matchesAttributeMethod(valueMethod, "value"); // returns true // Check if the executable element corresponds to the "priority" method ExecutableElement priorityMethod = ...; // method representing 'priority()' boolean isPriorityMethod = AnnotationUtils.matchesAttributeMethod(priorityMethod, "priority"); // returns true // Handle cases where the executable element is null boolean result = AnnotationUtils.matchesAttributeMethod(null, "value"); // returns false // Handle cases where the attribute name is null or blank result = AnnotationUtils.matchesAttributeMethod(valueMethod, null); // returns false result = AnnotationUtils.matchesAttributeMethod(valueMethod, ""); // returns false
- Parameters:
attributeMethod
- the executable element to check, may benull
attributeName
- the expected name of the attribute method, may benull
or blank- Returns:
true
if the method is not null and its name matches the given attribute name;false
otherwise
-
matchesAttributeValue
static boolean matchesAttributeValue(javax.lang.model.element.AnnotationValue one, javax.lang.model.element.AnnotationValue another)
Checks if two givenAnnotationValue
instances are equal by comparing their values.This method performs a deep comparison of the values contained within the annotation values. If both values are
null
, they are considered equal. If only one isnull
, they are not equal. Otherwise, the method compares the actual values usingObjects.equals(Object, Object)
.Example Usage
AnnotationValue value1 = ...; // obtain an AnnotationValue instance AnnotationValue value2 = ...; // obtain another AnnotationValue instance // Check if both annotation values are equal boolean isEqual = AnnotationUtils.matchesAttributeValue(value1, value2); // Handle cases where either value is null isEqual = AnnotationUtils.matchesAttributeValue(null, value2); // returns false isEqual = AnnotationUtils.matchesAttributeValue(value1, null); // returns false
- Parameters:
one
- the first annotation value to compare; may benull
another
- the second annotation value to compare; may benull
- Returns:
true
if both annotation values are eithernull
or their contents are equal;false
otherwise
-
matchesAttributeValue
static boolean matchesAttributeValue(javax.lang.model.element.AnnotationValue annotationValue, java.lang.Object attributeValue)
Checks if the value of the givenAnnotationValue
matches the specified attribute value.This method compares the actual value extracted from the annotation value with the provided attribute value. Returns
false
if either parameter isnull
or if the values do not match.Example Usage
AnnotationValue annotationValue = ...; // obtain an AnnotationValue instance String expectedValue = "example"; boolean isMatch = AnnotationUtils.matchesAttributeValue(annotationValue, expectedValue); // returns true if values match // Handle cases where the annotation value is null boolean result = AnnotationUtils.matchesAttributeValue(null, expectedValue); // returns false // Handle cases where the attribute value is null result = AnnotationUtils.matchesAttributeValue(annotationValue, null); // returns false
- Parameters:
annotationValue
- the annotation value to compare; may benull
attributeValue
- the target value to match against; may benull
- Returns:
true
if both parameters are non-null and their values match;false
otherwise
-
matchesDefaultAttributeValue
static boolean matchesDefaultAttributeValue(javax.lang.model.element.ExecutableElement attributeMethod, javax.lang.model.element.AnnotationValue annotationValue)
Checks if the provided annotation value matches the default value of the specified attribute method.This method is useful when determining if an attribute value in an annotation is explicitly set or if it falls back to the default value declared in the annotation interface.
Example Usage
public @interface MyAnnotation { String value() default "default"; } AnnotationMirror annotation = ...; // obtained from an annotated element ExecutableElement attributeMethod = getAttributeMethod(annotation, "value"); AnnotationValue annotationValue = getAttributeValue(annotation, "value"); boolean isDefaultValue = matchesDefaultAttributeValue(attributeMethod, annotationValue);
- Parameters:
attributeMethod
- the executable element representing the annotation attribute method, may benull
annotationValue
- the annotation value to compare against the default, may benull
- Returns:
true
if both the attribute method and annotation value are non-null and the value matches the default;false
otherwise
-
getAttributesMap
@Nonnull @Immutable static java.util.Map<java.lang.String,java.lang.Object> getAttributesMap(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves a map of attribute names to their corresponding values from the first matching annotation of the specified class on the given annotated construct. This includes both explicitly set values and default values for attributes.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve attributes from an annotated class TypeElement typeElement = ...; // obtain a TypeElement Map<String, Object> attributes = AnnotationUtils.getAttributesMap(typeElement, MyAnnotation.class); // Example output if MyAnnotation is applied with value="custom" // attributes will contain: // { // "value" = "custom", // "priority" = 0 // } // Handle cases where the annotated construct is null attributes = AnnotationUtils.getAttributesMap(null, MyAnnotation.class); // returns empty map // Handle cases where the annotation class is null attributes = AnnotationUtils.getAttributesMap(typeElement, null); // returns empty map
- Parameters:
annotatedConstruct
- the annotated construct (e.g., a class, method, or field) that may contain the annotation, may benull
annotationClass
- the annotation class used to locate the annotation on the construct, must not benull
- Returns:
- a non-null immutable map of attribute names to their corresponding values; never
null
, returns an empty map if no annotation is found, the construct isnull
, or the annotation class isnull
-
getAttributesMap
@Nonnull @Immutable static java.util.Map<java.lang.String,java.lang.Object> getAttributesMap(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass, boolean withDefault)
Retrieves a map of attribute names to their corresponding values from the first matching annotation of the specified class on the given annotated construct. This includes both explicitly set values and default values for attributes if thewithDefault
flag is set totrue
.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve attributes from an annotated class TypeElement typeElement = ...; // obtain a TypeElement Map<String, Object> attributes = AnnotationUtils.getAttributesMap(typeElement, MyAnnotation.class, true); // Example output if MyAnnotation is applied with value="custom" // attributes will contain: // { // "value" = "custom", // "priority" = 0 // } // Handle cases where the annotated construct is null attributes = AnnotationUtils.getAttributesMap(null, MyAnnotation.class, true); // returns empty map // Handle cases where the annotation class is null attributes = AnnotationUtils.getAttributesMap(typeElement, null, true); // returns empty map
- Parameters:
annotatedConstruct
- the annotated construct (e.g., a class, method, or field) that may contain the annotation, may benull
annotationClass
- the annotation class used to locate the annotation on the construct, must not benull
withDefault
- flag indicating whether to include default values for attributes that are not explicitly set; iftrue
, default values will be included where applicable- Returns:
- a non-null immutable map of attribute names to their corresponding values; never
null
, returns an empty map if no annotation is found, the construct isnull
, or the annotation class isnull
-
getAttributesMap
@Nonnull @Immutable static java.util.Map<java.lang.String,java.lang.Object> getAttributesMap(javax.lang.model.element.AnnotationMirror annotation)
Retrieves a map of attribute names to their corresponding values from the specified annotation. This includes both explicitly set values and default values for attributes.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve attributes from an annotation instance AnnotationMirror annotation = ...; // obtain an AnnotationMirror instance Map<String, Object> attributes = AnnotationUtils.getAttributesMap(annotation); // Example output if MyAnnotation is applied with value="custom" // attributes will contain: // { // "value" = "custom", // "priority" = 0 // } // Handle cases where the annotation is null Map<String, Object> emptyMap = AnnotationUtils.getAttributesMap(null); // returns empty map
- Parameters:
annotation
- the specified annotation, may benull
- Returns:
- a non-null immutable map of attribute names to their corresponding values; never
null
, returns an empty map if the annotation isnull
-
getAttributesMap
@Nonnull @Immutable static java.util.Map<java.lang.String,java.lang.Object> getAttributesMap(javax.lang.model.element.AnnotationMirror annotation, boolean withDefault)
Retrieves a map of attribute names to their corresponding values from the specified annotation. This includes both explicitly set values and default values for attributes if thewithDefault
flag is set totrue
.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve attributes from an annotation instance with default values AnnotationMirror annotation = ...; // obtain an AnnotationMirror instance Map<String, Object> attributes = AnnotationUtils.getAttributesMap(annotation, true); // Example output if MyAnnotation is applied with value="custom" // attributes will contain: // { // "value" = "custom", // "priority" = 0 // } // Retrieve attributes without including default values Map<String, Object> explicitAttributes = AnnotationUtils.getAttributesMap(annotation, false); // Handle cases where the annotation is null Map<String, Object> emptyMap = AnnotationUtils.getAttributesMap(null, true); // returns empty map
- Parameters:
annotation
- the specified annotation, may benull
withDefault
- flag indicating whether to include default values for attributes that are not explicitly set; iftrue
, default values will be included where applicable- Returns:
- a non-null immutable map of attribute names to their corresponding values; never
null
, returns an empty map if the annotation isnull
-
getElementValues
@Nonnull @Immutable static java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> getElementValues(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Retrieves the map of annotation attribute methods to their corresponding values from the specifiedAnnotatedConstruct
and annotation class. This method finds the first matching annotation on the construct and returns all its declared attribute values, including default values.This method is a convenience overload that defaults to including default values. If the annotated construct is
null
or the annotation class is not present, an empty map is returned.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve attribute values from an annotated class TypeElement typeElement = ...; // obtain a TypeElement Map<ExecutableElement, AnnotationValue> attributes = AnnotationUtils.getElementValues(typeElement, MyAnnotation.class); // Example output if MyAnnotation is applied with value="custom" // attributes will contain: // { // value(): "custom", // priority(): 0 // } // Handle cases where the annotated construct is null Map<ExecutableElement, AnnotationValue> emptyMap = AnnotationUtils.getElementValues(null, MyAnnotation.class); // returns empty map // Handle cases where the annotation class is null emptyMap = AnnotationUtils.getElementValues(typeElement, null); // returns empty map
- Parameters:
annotatedConstruct
- the annotated construct (e.g., a class, method, or field) that may contain the annotation, may benull
annotationClass
- the annotation class used to locate the annotation on the construct, must not benull
- Returns:
- a non-null immutable map of executable elements (attribute methods) to their annotation values; never
null
, returns an empty map if no annotation is found or if the construct isnull
-
getElementValues
@Nonnull @Immutable static java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> getElementValues(javax.lang.model.AnnotatedConstruct annotatedConstruct, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass, boolean withDefault)
Retrieves the map of annotation attribute methods to their corresponding values from the specifiedAnnotatedConstruct
and annotation class. This method finds the first matching annotation on the construct and returns all its declared attribute values, including default values if enabled.If the annotated construct is
null
or the annotation class is not present, an empty map is returned. If thewithDefault
flag is set totrue
, this method includes default values for attributes that are not explicitly set.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve attribute values from an annotated class with default values TypeElement typeElement = ...; // obtain a TypeElement Map<ExecutableElement, AnnotationValue> attributes = AnnotationUtils.getElementValues(typeElement, MyAnnotation.class, true); // Example output if MyAnnotation is applied with value="custom" // attributes will contain: // { // value(): "custom", // priority(): 0 // } // Retrieve attribute values without including default values Map<ExecutableElement, AnnotationValue> explicitAttributes = AnnotationUtils.getElementValues(typeElement, MyAnnotation.class, false); // Handle cases where the annotated construct is null Map<ExecutableElement, AnnotationValue> emptyMap = AnnotationUtils.getElementValues(null, MyAnnotation.class, true); // returns empty map // Handle cases where the annotation class is null emptyMap = AnnotationUtils.getElementValues(typeElement, null, true); // returns empty map
- Parameters:
annotatedConstruct
- the annotated construct (e.g., a class, method, or field) that may contain the annotation, may benull
annotationClass
- the annotation class used to locate the annotation on the construct, must not benull
withDefault
- flag indicating whether to include default values for attributes that are not explicitly set; iftrue
, default values will be included where applicable- Returns:
- a non-null immutable map of executable elements (attribute methods) to their annotation values; never
null
, returns an empty map if no annotation is found or if the construct isnull
-
getElementValues
@Nonnull @Immutable static java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> getElementValues(javax.lang.model.element.AnnotationMirror annotation)
Retrieves a map of annotation attribute methods to their corresponding values from the specifiedAnnotationMirror
. This method includes both explicitly set values and default values for attributes.If the provided annotation is
null
, an empty map is returned.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve attribute values from an annotation instance AnnotationMirror annotation = ...; // obtain an AnnotationMirror instance Map<ExecutableElement, AnnotationValue> attributes = AnnotationUtils.getElementValues(annotation); // Example output if MyAnnotation is applied with value="custom" // attributes will contain: // { // value(): "custom", // priority(): 0 // } // Handle cases where the annotation is null Map<ExecutableElement, AnnotationValue> emptyMap = AnnotationUtils.getElementValues(null); // returns empty map
- Parameters:
annotation
- the annotation mirror to extract attribute values from, may benull
- Returns:
- a non-null immutable map of executable elements (attribute methods) to their annotation values;
never
null
, returns an empty map if the annotation isnull
-
getElementValues
@Nonnull @Immutable static java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> getElementValues(javax.lang.model.element.AnnotationMirror annotation, boolean withDefault)
Retrieves a map of annotation attribute methods to their corresponding values from the specifiedAnnotationMirror
. This method includes both explicitly set values and default values for attributes if thewithDefault
flag is set totrue
.If the provided annotation is
null
, an empty map is returned.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve attribute values from an annotation instance with default values AnnotationMirror annotation = ...; // obtain an AnnotationMirror instance Map<ExecutableElement, AnnotationValue> attributes = AnnotationUtils.getElementValues(annotation, true); // Example output if MyAnnotation is applied with value="custom" // attributes will contain: // { // value(): "custom", // priority(): 0 // } // Retrieve attribute values without including default values Map<ExecutableElement, AnnotationValue> explicitAttributes = AnnotationUtils.getElementValues(annotation, false); // Handle cases where the annotation is null Map<ExecutableElement, AnnotationValue> emptyMap = AnnotationUtils.getElementValues(null, true); // returns empty map
- Parameters:
annotation
- the annotation mirror to extract attribute values from, may benull
withDefault
- flag indicating whether to include default values for attributes that are not explicitly set; iftrue
, default values will be included where applicable- Returns:
- a non-null immutable map of executable elements (attribute methods) to their annotation values;
never
null
, returns an empty map if the annotation isnull
-
getElementValue
@Nullable @Immutable static java.util.Map.Entry<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> getElementValue(javax.lang.model.element.AnnotationMirror annotation, java.lang.String attributeName, boolean withDefault)
Retrieves the attribute method and its corresponding annotation value from the specified annotation based on the given attribute name. If no explicit value is found andwithDefault
is true, it attempts to find and return the default value for the attribute.If the provided annotation is null or the attribute name is blank, this method returns null.
Example Usage
AnnotationMirror annotation = ...; // obtain an AnnotationMirror instance String attributeName = "value"; // the name of the attribute to retrieve // Retrieve an attribute with default value lookup enabled Map.Entry<ExecutableElement, AnnotationValue> attributeEntry = AnnotationUtils.getElementValue(annotation, attributeName, true); if (attributeEntry != null) { ExecutableElement attributeMethod = attributeEntry.getKey(); AnnotationValue annotationValue = attributeEntry.getValue(); // process attribute method and value } // Retrieve an attribute without default value lookup attributeEntry = AnnotationUtils.getElementValue(annotation, attributeName, false); // Handle cases where the annotation is null attributeEntry = AnnotationUtils.getElementValue(null, attributeName, true); // returns null // Handle cases where the attribute name is blank or null attributeEntry = AnnotationUtils.getElementValue(annotation, null, true); // returns null
- Parameters:
annotation
- the annotation mirror to extract the attribute value from, may benull
attributeName
- the name of the attribute method to look for, may be blankwithDefault
- flag indicating whether to include the default value if the attribute is not explicitly set; if true, the method will attempt to find and return the default value- Returns:
- an entry containing the executable element (attribute method) and its corresponding annotation value; returns null if the annotation is null, the attribute name is blank, or the attribute method cannot be found
-
getElementValue
@Nullable static java.util.Map.Entry<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> getElementValue(java.util.Map<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> elementValues, java.lang.String attributeName)
Retrieves the attribute method and its corresponding annotation value from the specified element values map based on the given attribute name.This method searches through the provided map of executable elements (attribute methods) to their annotation values to find a matching attribute by name. If no match is found, it returns
null
.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Assume elementValues contains: // { // value(): "custom", // priority(): 5 // } // Retrieve an existing attribute Entry<ExecutableElement, AnnotationValue> entry = AnnotationUtils.getElementValue(elementValues, "value"); if (entry != null) { String value = (String) entry.getValue().getValue(); // returns "custom" } // Retrieve a non-existent attribute entry = AnnotationUtils.getElementValue(elementValues, "nonExistent"); // returns null // Handle cases where the element values map is null or empty entry = AnnotationUtils.getElementValue(null, "value"); // returns null
- Parameters:
elementValues
- the map of executable elements (attribute methods) to their annotation values; may benull
or emptyattributeName
- the name of the attribute method to look for; may benull
or blank- Returns:
- an entry containing the executable element (attribute method) and its corresponding annotation value;
returns
null
if the element values map is empty, the attribute name is blank, or no matching attribute is found
-
getAttribute
@Nullable static <T> T getAttribute(javax.lang.model.element.AnnotationMirror annotation, java.lang.String attributeName)
Retrieves the value of the specified attribute from the given annotation.This method attempts to find the attribute by name in the provided annotation. If the attribute is not explicitly set, it will attempt to retrieve the default value associated with that attribute. If the attribute cannot be resolved or no value is found, this method returns
null
.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve the value of the "value" attribute AnnotationMirror annotation = ...; // obtain an AnnotationMirror instance String value = AnnotationUtils.getAttribute(annotation, "value"); // returns "default" or explicit value // Retrieve the value of the "priority" attribute int priority = AnnotationUtils.getAttribute(annotation, "priority"); // returns 0 or explicit value // Handle cases where the annotation is null String result = AnnotationUtils.getAttribute(null, "value"); // returns null // Handle cases where the attribute name is blank or null result = AnnotationUtils.getAttribute(annotation, null); // returns null
- Type Parameters:
T
- the type of the attribute value to return- Parameters:
annotation
- the annotation mirror to extract the attribute value from; may benull
attributeName
- the name of the attribute method to look for; may be blank ornull
- Returns:
- the value of the specified attribute if found, or the default value if available;
returns
null
if the annotation isnull
, the attribute name is blank, or the attribute cannot be resolved
-
getAttribute
@Nullable static <T> T getAttribute(javax.lang.model.element.AnnotationMirror annotation, java.lang.String attributeName, boolean withDefault)
Retrieves the value of the specified attribute from the given annotation, optionally including the default value.This method attempts to find the attribute by name in the provided annotation. If the attribute is not explicitly set, and the
withDefault
flag is set totrue
, it will attempt to retrieve the default value associated with that attribute. If the attribute cannot be resolved or no value is found, this method returnsnull
.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Retrieve the value of the "value" attribute including default AnnotationMirror annotation = ...; // obtain an AnnotationMirror instance String value = AnnotationUtils.getAttribute(annotation, "value", true); // returns "default" or explicit value // Retrieve the value of the "priority" attribute without default lookup int priority = AnnotationUtils.getAttribute(annotation, "priority", false); // returns 0 only if explicitly set // Handle cases where the annotation is null String result = AnnotationUtils.getAttribute(null, "value", true); // returns null // Handle cases where the attribute name is blank or null result = AnnotationUtils.getAttribute(annotation, null, true); // returns null
- Type Parameters:
T
- the type of the attribute value to return- Parameters:
annotation
- the annotation mirror to extract the attribute value from; may benull
attributeName
- the name of the attribute method to look for; may be blank ornull
withDefault
- flag indicating whether to include the default value if the attribute is not explicitly set; iftrue
, the method will attempt to find and return the default value- Returns:
- the value of the specified attribute if found, or the default value if available;
returns
null
if the annotation isnull
, the attribute name is blank, or the attribute cannot be resolved
-
getAttribute
@Nullable static <T> T getAttribute(java.util.Map.Entry<javax.lang.model.element.ExecutableElement,javax.lang.model.element.AnnotationValue> elementValue)
Retrieves the value of the specified attribute from the provided entry containing the attribute method and its corresponding annotation value.If the entry is null or either the attribute method or annotation value is unresolved, this method returns
null
. Otherwise, it delegates to the defaultAnnotationValueVisitor
to extract the attribute value.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; int priority() default 0; } // Assume elementValue contains the entry for "value" attribute Entry<ExecutableElement, AnnotationValue> elementValue = ...; // obtain from AnnotationUtils.getElementValue() String value = AnnotationUtils.getAttribute(elementValue); // returns "default" or explicit value // Handle cases where the element value entry is null String result = AnnotationUtils.getAttribute(null); // returns null
- Type Parameters:
T
- the expected type of the attribute value- Parameters:
elementValue
- an entry containing the attribute method and its corresponding annotation value; may benull
- Returns:
- the resolved value of the attribute if found; returns
null
if the entry is null, or if either the attribute method or annotation value is unresolved
-
getValue
@Nullable static <T> T getValue(javax.lang.model.element.AnnotationMirror annotation)
Retrieves the value of the default attribute method named"value"
from the specified annotation.This method delegates to
getAttribute(AnnotationMirror, String)
to obtain the value of the annotation'svalue()
method. Returnsnull
if the annotation isnull
or if the value cannot be resolved.Example Usage
// Given an annotation interface: public @interface MyAnnotation { String value() default "default"; } // Retrieve the "value" attribute from an annotation instance AnnotationMirror annotation = ...; // obtain an AnnotationMirror instance String value = AnnotationUtils.getValue(annotation); // returns "default" or explicit value // Handle cases where the annotation is null String result = AnnotationUtils.getValue(null); // returns null
- Type Parameters:
T
- the expected type of the attribute value- Parameters:
annotation
- the annotation mirror to extract the value from; may benull
- Returns:
- the resolved value of the annotation's
value()
method if found; returnsnull
if the annotation isnull
or the value cannot be resolved
-
getElementTypes
@Nonnull static java.lang.annotation.ElementType[] getElementTypes(javax.lang.model.element.AnnotationMirror annotation)
Retrieves theElementType
array from the specified annotation.This method checks the annotation's type for the presence of a
Target
annotation, which defines the element types the annotation can be applied to. If the provided annotation isnull
, this method returns an empty array.Example Usage
AnnotationMirror annotation = ...; // obtain an AnnotationMirror instance ElementType[] elementTypes = AnnotationUtils.getElementTypes(annotation); // Handle cases where the annotation is null ElementType[] emptyTypes = AnnotationUtils.getElementTypes(null); // returns empty array
- Parameters:
annotation
- the specified annotation, may benull
- Returns:
- a non-null array of
ElementType
; nevernull
, returns an empty array if the annotation isnull
-
getElementTypes
@Nonnull static java.lang.annotation.ElementType[] getElementTypes(javax.lang.model.type.DeclaredType annotationType)
Retrieves theElementType
array from the specified annotation type by checking theTarget
annotation associated with it. If the annotation type does not have aTarget
annotation, an empty array is returned.Example Usage
// Given an annotation type: public @interface MyAnnotation { } DeclaredType annotationType = ...; // obtain a DeclaredType for MyAnnotation ElementType[] elementTypes = AnnotationUtils.getElementTypes(annotationType); // If MyAnnotation is annotated with @Target(ElementType.TYPE) // elementTypes will contain: { ElementType.TYPE } // Handle cases where the annotation type does not have a @Target annotation ElementType[] emptyTypes = AnnotationUtils.getElementTypes(annotationType); // returns empty array
- Parameters:
annotationType
- the declared type of the annotation- Returns:
- a non-null immutable array of
ElementType
; nevernull
, returns an empty array if noTarget
annotation is present
-
-