public class AnnotationUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
annotationName(javax.lang.model.element.AnnotationMirror annotation) |
static java.util.Comparator<javax.lang.model.element.AnnotationMirror> |
annotationOrdering()
Provide ordering for
AnnotationMirror based on their fully qualified name. |
static boolean |
areSame(javax.lang.model.element.AnnotationMirror a1,
javax.lang.model.element.AnnotationMirror a2)
Returns true iff both annotations are of the same type and have the same annotation values.
|
static boolean |
areSame(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c1,
java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c2)
Checks that two collections contain the same annotations.
|
static boolean |
areSameByClass(javax.lang.model.element.AnnotationMirror am,
java.lang.Class<? extends java.lang.annotation.Annotation> annoClass)
Checks that the annotation
am has the name of annoClass . |
static boolean |
areSameByName(javax.lang.model.element.AnnotationMirror a1,
javax.lang.model.element.AnnotationMirror a2)
Return true iff a1 and a2 have the same annotation type.
|
static boolean |
areSameByName(javax.lang.model.element.AnnotationMirror am,
java.lang.String aname)
Checks that the annotation
am has the name aname (a fully-qualified type
name). |
static void |
clear() |
static boolean |
containsSame(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c,
javax.lang.model.element.AnnotationMirror anno)
Checks that the collection contains the annotation.
|
static boolean |
containsSameByClass(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c,
java.lang.Class<? extends java.lang.annotation.Annotation> anno)
Checks that the collection contains the annotation.
|
static boolean |
containsSameByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c,
javax.lang.model.element.AnnotationMirror anno)
Checks that the collection contains an annotation of the given name.
|
static boolean |
containsSameByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c,
java.lang.String anno)
Checks that the collection contains an annotation of the given name.
|
static <V> java.util.Map<javax.lang.model.element.AnnotationMirror,V> |
createAnnotationMap()
Create a map suitable for storing
AnnotationMirror as keys. |
static java.util.Set<javax.lang.model.element.AnnotationMirror> |
createAnnotationSet()
Constructs a
Set for storing AnnotationMirror s. |
static javax.lang.model.element.AnnotationMirror |
getAnnotationByClass(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c,
java.lang.Class<? extends java.lang.annotation.Annotation> anno)
Returns the AnnotationMirror in
c that has the same class as anno . |
static javax.lang.model.element.AnnotationMirror |
getAnnotationByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c,
java.lang.String anno)
Returns the AnnotationMirror in
c that has the same name as anno . |
static java.util.EnumSet<javax.lang.model.element.ElementKind> |
getElementKindsForElementType(java.lang.annotation.ElementType elementType)
Returns the set of
ElementKind s corresponding to elementType . |
static java.util.EnumSet<javax.lang.model.element.ElementKind> |
getElementKindsForTarget(@Nullable java.lang.annotation.Target target) |
static <T> T |
getElementValue(javax.lang.model.element.AnnotationMirror anno,
java.lang.CharSequence elementName,
java.lang.Class<T> expectedType,
boolean useDefaults)
Get the element with the name
elementName of the annotation anno . |
static <T> java.util.List<T> |
getElementValueArray(javax.lang.model.element.AnnotationMirror anno,
java.lang.CharSequence elementName,
java.lang.Class<T> expectedType,
boolean useDefaults)
Get the element with the name
elementName of the annotation anno , where the
element has an array type. |
static javax.lang.model.element.Name |
getElementValueClassName(javax.lang.model.element.AnnotationMirror anno,
java.lang.CharSequence elementName,
boolean useDefaults)
Get the Name of the class that is referenced by element
elementName . |
static java.util.List<javax.lang.model.element.Name> |
getElementValueClassNames(javax.lang.model.element.AnnotationMirror anno,
java.lang.CharSequence annoElement,
boolean useDefaults)
Get the list of Names of the classes that are referenced by element
elementName . |
static <T extends java.lang.Enum<T>> |
getElementValueEnum(javax.lang.model.element.AnnotationMirror anno,
java.lang.CharSequence elementName,
java.lang.Class<T> expectedType,
boolean useDefaults)
Get the element with the name
name of the annotation anno . |
static <T extends java.lang.Enum<T>> |
getElementValueEnumArray(javax.lang.model.element.AnnotationMirror anno,
java.lang.CharSequence elementName,
java.lang.Class<T> expectedType,
boolean useDefaults)
Get the element with the name
elementName of the annotation anno , or the
default value if no element is present explicitly, where the element has an array type and
the elements are Enum s. |
static java.util.Map<? extends javax.lang.model.element.ExecutableElement,? extends javax.lang.model.element.AnnotationValue> |
getElementValuesWithDefaults(javax.lang.model.element.AnnotationMirror ad)
Returns the values of an annotation's elements, including defaults.
|
static java.util.Set<javax.lang.model.element.AnnotationMirror> |
getExplicitAnnotationsOnConstructorResult(com.sun.source.tree.MethodTree constructorDeclaration)
Returns the annotations explicitly written on a constructor result.
|
static javax.lang.model.element.AnnotationMirror |
getSame(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c,
javax.lang.model.element.AnnotationMirror anno)
Returns the AnnotationMirror in
c that is the same annotation as anno . |
static javax.lang.model.element.AnnotationMirror |
getSameByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c,
javax.lang.model.element.AnnotationMirror anno)
Returns the AnnotationMirror in
c that is the same annotation as anno
ignoring values. |
static boolean |
hasElementValue(javax.lang.model.element.AnnotationMirror anno,
java.lang.CharSequence elementName)
Verify whether the element with the name
elementName exists in the annotation anno . |
static boolean |
hasInheritedMeta(javax.lang.model.element.AnnotationMirror anno)
Returns true if the given annotation has a @Inherited meta-annotation.
|
static boolean |
sameAnnotationValue(javax.lang.model.element.AnnotationValue av1,
javax.lang.model.element.AnnotationValue av2)
Return true iff the two AnnotationValue objects are the same.
|
static boolean |
sameElementValues(javax.lang.model.element.AnnotationMirror am1,
javax.lang.model.element.AnnotationMirror am2)
Returns true if the two annotations have the same elements (fields).
|
static <T> void |
updateMappingToImmutableSet(java.util.Map<T,java.util.Set<javax.lang.model.element.AnnotationMirror>> map,
T key,
java.util.Set<javax.lang.model.element.AnnotationMirror> newQual)
See
org.checkerframework.framework.type.QualifierHierarchy#updateMappingToMutableSet(QualifierHierarchy,
Map, Object, AnnotationMirror).
|
public static void clear()
public static final java.lang.String annotationName(javax.lang.model.element.AnnotationMirror annotation)
annotation
- the annotation whose name to returnpublic static boolean areSame(javax.lang.model.element.AnnotationMirror a1, javax.lang.model.element.AnnotationMirror a2)
This behavior differs from AnnotationMirror.equals(Object)
. The equals method
returns true iff both annotations are the same and annotate the same annotation target (e.g.
field, variable, etc) -- that is, if its arguments are the same annotation instance.
a1
- the first AnnotationMirror to comparea2
- the second AnnotationMirror to comparepublic static boolean areSameByName(javax.lang.model.element.AnnotationMirror a1, javax.lang.model.element.AnnotationMirror a2)
a1
- the first AnnotationMirror to comparea2
- the second AnnotationMirror to compareareSame(AnnotationMirror, AnnotationMirror)
public static boolean areSameByName(javax.lang.model.element.AnnotationMirror am, java.lang.String aname)
am
has the name aname
(a fully-qualified type
name). Values are ignored.
(Use areSameByClass(javax.lang.model.element.AnnotationMirror, java.lang.Class<? extends java.lang.annotation.Annotation>)
instead of this method when possible. It is faster.)
am
- the AnnotationMirror whose name to compareaname
- the string to comparepublic static boolean areSameByClass(javax.lang.model.element.AnnotationMirror am, java.lang.Class<? extends java.lang.annotation.Annotation> annoClass)
am
has the name of annoClass
. Values are ignored.
(Use this method rather than areSameByName(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.AnnotationMirror)
when possible. This method is faster.)
am
- the AnnotationMirror whose class to compareannoClass
- the class to comparepublic static boolean areSame(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c1, java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c2)
c1
- the first collection to comparec2
- the second collection to compareareSame(AnnotationMirror, AnnotationMirror)
public static boolean containsSame(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c, javax.lang.model.element.AnnotationMirror anno)
c
- a collection of AnnotationMirrorsanno
- the AnnotationMirror to search for in cpublic static javax.lang.model.element.AnnotationMirror getSame(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c, javax.lang.model.element.AnnotationMirror anno)
c
that is the same annotation as anno
.c
- a collection of AnnotationMirrorsanno
- the AnnotationMirror to search for in canno
iff c contains anno, according
to areSame; otherwise, null
public static boolean containsSameByClass(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c, java.lang.Class<? extends java.lang.annotation.Annotation> anno)
c
- a collection of AnnotationMirrorsanno
- the annotation class to search for in cpublic static javax.lang.model.element.AnnotationMirror getAnnotationByClass(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c, java.lang.Class<? extends java.lang.annotation.Annotation> anno)
c
that has the same class as anno
.c
- a collection of AnnotationMirrorsanno
- the class to search for in canno
iff c contains anno, according
to areSameByClass; otherwise, null
public static boolean containsSameByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c, java.lang.String anno)
c
- a collection of AnnotationMirrorsanno
- the name to search for in cpublic static javax.lang.model.element.AnnotationMirror getAnnotationByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c, java.lang.String anno)
c
that has the same name as anno
.c
- a collection of AnnotationMirrorsanno
- the name to search for in canno
iff c contains anno, according to
areSameByName; otherwise, null
public static boolean containsSameByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c, javax.lang.model.element.AnnotationMirror anno)
c
- a collection of AnnotationMirrorsanno
- the annotation whose name to search for in cpublic static javax.lang.model.element.AnnotationMirror getSameByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror> c, javax.lang.model.element.AnnotationMirror anno)
c
that is the same annotation as anno
ignoring values.c
- a collection of AnnotationMirrorsanno
- the annotation whose name to search for in canno
iff c contains anno, according
to areSameByName; otherwise, null
public static java.util.Comparator<javax.lang.model.element.AnnotationMirror> annotationOrdering()
AnnotationMirror
based on their fully qualified name. The
ordering ignores annotation values when ordering.
The ordering is meant to be used as TreeSet
or TreeMap
ordering. A Set
should not contain two annotations that only differ in values.
public static <V> java.util.Map<javax.lang.model.element.AnnotationMirror,V> createAnnotationMap()
AnnotationMirror
as keys.
It can store one instance of AnnotationMirror
of a given declared type, regardless
of the annotation element values.
V
- the value of the mapAnnotationMirror
as keypublic static java.util.Set<javax.lang.model.element.AnnotationMirror> createAnnotationSet()
Set
for storing AnnotationMirror
s.
It stores at most once instance of AnnotationMirror
of a given type, regardless of
the annotation element values.
AnnotationMirror
as elementpublic static boolean hasInheritedMeta(javax.lang.model.element.AnnotationMirror anno)
anno
- the annotation to check for an @Inherited meta-annotationpublic static java.util.EnumSet<javax.lang.model.element.ElementKind> getElementKindsForTarget(@Nullable java.lang.annotation.Target target)
target
- a location where an annotation can be writtenElementKind
s to which target
applies, ignoring TYPE_USEpublic static java.util.EnumSet<javax.lang.model.element.ElementKind> getElementKindsForElementType(java.lang.annotation.ElementType elementType)
ElementKind
s corresponding to elementType
. If the element
type is TYPE_USE, then ElementKinds returned should be the same as those returned for TYPE
and TYPE_PARAMETER, but this method returns the empty set instead.
If the Element is MODULE, the empty set is returned. This is so that this method can compile with Java 8.
elementType
- the elementType to find ElementKinds forElementKind
s corresponding to elementType
public static java.util.Map<? extends javax.lang.model.element.ExecutableElement,? extends javax.lang.model.element.AnnotationValue> getElementValuesWithDefaults(javax.lang.model.element.AnnotationMirror ad)
ad
- annotation to examineAnnotationMirror.getElementValues()
,
JavacElements.getElementValuesWithDefaults(AnnotationMirror)
public static boolean sameElementValues(javax.lang.model.element.AnnotationMirror am1, javax.lang.model.element.AnnotationMirror am2)
am1
and am2
must be the same type of annotation.am1
- the first AnnotationMirror to compaream2
- the second AnnotationMirror to comparepublic static boolean sameAnnotationValue(javax.lang.model.element.AnnotationValue av1, javax.lang.model.element.AnnotationValue av2)
av1
- the first AnnotationValue to compareav2
- the second AnnotationValue to comparepublic static boolean hasElementValue(javax.lang.model.element.AnnotationMirror anno, java.lang.CharSequence elementName)
elementName
exists in the annotation anno
.anno
- the annotation to examineelementName
- the name of the elementpublic static <T> T getElementValue(javax.lang.model.element.AnnotationMirror anno, java.lang.CharSequence elementName, java.lang.Class<T> expectedType, boolean useDefaults)
elementName
of the annotation anno
. The result
is expected to have type expectedType
.
Note 1: The method does not work well for elements of an array type (as it would
return a list of AnnotationValue
s). Use getElementValueArray
instead.
Note 2: The method does not work for elements of an enum type, as the
AnnotationValue is a VarSymbol and would be cast to the enum type, which doesn't work. Use
getElementValueEnum
instead.
T
- the class of the expected typeanno
- the annotation to disassembleelementName
- the name of the element to accessexpectedType
- the expected type used to cast the return typeuseDefaults
- whether to apply default values to the elementpublic static <T extends java.lang.Enum<T>> T getElementValueEnum(javax.lang.model.element.AnnotationMirror anno, java.lang.CharSequence elementName, java.lang.Class<T> expectedType, boolean useDefaults)
name
of the annotation anno
. The result is an
enum of type T
.T
- the class of the expected typeanno
- the annotation to disassembleelementName
- the name of the element to accessexpectedType
- the expected type used to cast the return type, an enumuseDefaults
- whether to apply default values to the elementpublic static <T> java.util.List<T> getElementValueArray(javax.lang.model.element.AnnotationMirror anno, java.lang.CharSequence elementName, java.lang.Class<T> expectedType, boolean useDefaults)
elementName
of the annotation anno
, where the
element has an array type. One element of the result is expected to have type expectedType
.
Parameter useDefaults is used to determine whether default values should be used for annotation values. Finding defaults requires more computation, so should be false when no defaulting is needed.
T
- the class of the expected typeanno
- the annotation to disassembleelementName
- the name of the element to accessexpectedType
- the expected type used to cast the return typeuseDefaults
- whether to apply default values to the elementpublic static <T extends java.lang.Enum<T>> java.util.List<T> getElementValueEnumArray(javax.lang.model.element.AnnotationMirror anno, java.lang.CharSequence elementName, java.lang.Class<T> expectedType, boolean useDefaults)
elementName
of the annotation anno
, or the
default value if no element is present explicitly, where the element has an array type and
the elements are Enum
s. One element of the result is expected to have type expectedType
.T
- the class of the expected typeanno
- the annotation to disassembleelementName
- the name of the element to accessexpectedType
- the expected type used to cast the return typeuseDefaults
- whether to apply default values to the elementpublic static javax.lang.model.element.Name getElementValueClassName(javax.lang.model.element.AnnotationMirror anno, java.lang.CharSequence elementName, boolean useDefaults)
elementName
.
This is a convenience method for the most common use-case. It is like getElementValue(anno, elementName, ClassType.class).getQualifiedName()
, but this method
ensures consistent use of the qualified name.
anno
- the annotation to disassembleelementName
- the name of the element to accessuseDefaults
- whether to apply default values to the elementpublic static java.util.List<javax.lang.model.element.Name> getElementValueClassNames(javax.lang.model.element.AnnotationMirror anno, java.lang.CharSequence annoElement, boolean useDefaults)
elementName
. It
fails if the class wasn't found. Like getElementValueClassNames(javax.lang.model.element.AnnotationMirror, java.lang.CharSequence, boolean)
, but returns classes
rather than names.anno
- the annotation whose field to accessannoElement
- the element/field of anno
whose content is a list of classesuseDefaults
- whether to apply default values to the elementanno.annoElement
public static <T> void updateMappingToImmutableSet(java.util.Map<T,java.util.Set<javax.lang.model.element.AnnotationMirror>> map, T key, java.util.Set<javax.lang.model.element.AnnotationMirror> newQual)
public static java.util.Set<javax.lang.model.element.AnnotationMirror> getExplicitAnnotationsOnConstructorResult(com.sun.source.tree.MethodTree constructorDeclaration)
constructorDeclaration
is in fact a declaration of a constructor.constructorDeclaration
- declaration tree of constructor