Class AnnotationUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic final @BinaryName String
annotationBinaryName
(AnnotationMirror annotation) Returns the binary name of an annotation as a String.static Class<?>
Converts an AnnotationMirror to a Class.static final @CanonicalName String
annotationName
(AnnotationMirror annotation) Returns the fully-qualified name of an annotation as a String.static boolean
annotationValueContains
(@Nullable AnnotationValue avList, String s) Returns true if an AnnotationValue list contains the given value.static boolean
annotationValueContains
(List<? extends AnnotationValue> avList, String s) Returns true if an AnnotationValue list contains the given value.static boolean
Returns true if an AnnotationValue list contains a value whosetoString()
is the given string.static boolean
annotationValueContainsToString
(List<? extends AnnotationValue> avList, String s) Returns true if an AnnotationValue list contains a value whosetoString()
is the given string.static <T extends Enum<T>>
T[]annotationValueListToEnumArray
(List<AnnotationValue> la, Class<T> expectedType) Converts a list of AnnotationValue to an array of enum.static <T extends Enum<T>>
T[]annotationValueListToEnumArray
(AnnotationValue avList, Class<T> expectedType) Converts a list of AnnotationValue to an array of enum.static <T> List<T>
annotationValueToList
(List<? extends AnnotationValue> avList, Class<T> expectedType) Converts an annotation value to a list.static <T> List<T>
annotationValueToList
(AnnotationValue avList, Class<T> expectedType) Converts an annotation value to a list.static boolean
areSame
(Collection<? extends AnnotationMirror> c1, Collection<? extends AnnotationMirror> c2) Checks that two collections contain the same annotations.static boolean
areSame
(AnnotationMirror a1, AnnotationMirror a2) Returns true iff both annotations are of the same type and have the same annotation values.static boolean
areSameByClass
(AnnotationMirror am, Class<? extends Annotation> annoClass) Deprecated.static boolean
areSameByName
(AnnotationMirror am, String aname) Checks that the annotationam
has the nameaname
(a fully-qualified type name).static boolean
Return true iff a1 and a2 have the same annotation type.static int
Provide an ordering forAnnotationMirror
s.static int
Return -1, 0, or 1 depending on whether the name of a1 is less, equal to, or greater than that of a2 (lexicographically).static boolean
containsSame
(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Checks that the collection contains the annotation.static boolean
containsSameByClass
(Collection<? extends AnnotationMirror> c, Class<? extends Annotation> anno) Checks that the collection contains the annotation.static boolean
containsSameByName
(Collection<? extends AnnotationMirror> c, String anno) Checks that the collection contains an annotation of the given name.static boolean
containsSameByName
(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Checks that the collection contains an annotation of the given name.static @Nullable AnnotationMirror
getAnnotationByClass
(Collection<? extends AnnotationMirror> c, Class<? extends Annotation> anno) Returns the AnnotationMirror inc
that has the same class asanno
.static @Nullable AnnotationMirror
getAnnotationByName
(Collection<? extends AnnotationMirror> c, String anno) Returns the AnnotationMirror inc
that has the same name asanno
.static EnumSet<ElementKind>
getElementKindsForElementType
(ElementType elementType) Returns the set ofElementKind
s corresponding toelementType
.static EnumSet<ElementKind>
getElementKindsForTarget
(@Nullable Target target) Returns the set ofElementKind
s to whichtarget
applies, ignoring TYPE_USE.static <T> T
getElementValue
(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) static <T> @Nullable T
getElementValue
(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the given element of the annotationanno
.static <T> @Nullable T
getElementValue
(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, T defaultValue) Get the given element of the annotationanno
.static <T> List<T>
getElementValueArray
(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Deprecated.use#getElementValueArray(AnnotationMirror, ExecutableElement, Class)
or#getElementValueArray(AnnotationMirror, ExecutableElement, Class, Object)
static <T> List<T>
getElementValueArray
(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the given element of the annotationanno
, where the element has an array type.static <T> List<T>
getElementValueArray
(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, List<T> defaultValue) Get the given element of the annotationanno
, where the element has an array type.getElementValueArrayOrNull
(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Get the element with the nameelementName
of the annotationanno
, or return null if no such element exists.static boolean
getElementValueBoolean
(AnnotationMirror anno, ExecutableElement element, boolean defaultValue) Get the given boolean element of the annotationanno
.static @CanonicalName Name
getElementValueClassName
(AnnotationMirror anno, CharSequence elementName, boolean useDefaults) Deprecated.use an ExecutableElementstatic @CanonicalName Name
getElementValueClassName
(AnnotationMirror anno, ExecutableElement element) Get the Name of the class that is referenced by elementelement
.static List<@CanonicalName Name>
getElementValueClassNames
(AnnotationMirror anno, CharSequence annoElement, boolean useDefaults) Deprecated.static List<@CanonicalName Name>
getElementValueClassNames
(AnnotationMirror anno, ExecutableElement element) Get the list of Names of the classes that are referenced by elementelement
.static <T extends Enum<T>>
TgetElementValueEnum
(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) static <T extends Enum<T>>
TgetElementValueEnum
(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the element with the namename
of the annotationanno
.static <T extends Enum<T>>
TgetElementValueEnum
(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, T defaultValue) Get the element with the namename
of the annotationanno
.static <T extends Enum<T>>
T[]getElementValueEnumArray
(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) static <T extends Enum<T>>
T[]getElementValueEnumArray
(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the element with the namename
of the annotationanno
.static <T extends Enum<T>>
T[]getElementValueEnumArray
(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, T[] defaultValue) Get the element with the namename
of the annotationanno
.static int
getElementValueInt
(AnnotationMirror anno, ExecutableElement element) Get the given integer element of the annotationanno
.static int
getElementValueInt
(AnnotationMirror anno, ExecutableElement element, int defaultValue) Get the given integer element of the annotationanno
.static long
getElementValueLong
(AnnotationMirror anno, ExecutableElement element, long defaultValue) Get the given long element of the annotationanno
.static <T> @Nullable T
getElementValueOrNull
(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Get the element with the nameelementName
of the annotationanno
, or return null if no such element exists.static Map<? extends ExecutableElement,
? extends AnnotationValue> Deprecated.use a method that takes anExecutableElement
static AnnotationMirrorSet
getExplicitAnnotationsOnConstructorResult
(MethodTree constructorDeclaration) Returns the annotations explicitly written on a constructor result.static @Nullable AnnotationMirror
getSame
(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Returns the AnnotationMirror inc
that is the same annotation asanno
.static @Nullable AnnotationMirror
getSameByName
(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Returns the AnnotationMirror inc
that is the same annotation asanno
ignoring values.static boolean
hasElementValue
(AnnotationMirror anno, CharSequence elementName) Deprecated.use a method that takes anExecutableElement
static boolean
Returns true if the given annotation has a @Inherited meta-annotation.static boolean
hasTypeQualifierElementTypes
(ElementType[] elements, Class<?> cls) Returns true if the given array containsElementType.TYPE_USE
, false otherwise.static boolean
Returns true if anno is a declaration annotation.static boolean
Return true iff the two AnnotationValue objects are the same.static boolean
Returns true if the two annotations have the same elements (fields).static String
Returns a string representation of the annotation mirrors, using simple (not fully-qualified) names.updateMappingToImmutableSet
(Map<T, AnnotationMirrorSet> map, T key, AnnotationMirrorSet newQual) Update a map, to addnewQual
to the set thatkey
maps to.
-
Method Details
-
annotationName
Returns the fully-qualified name of an annotation as a String.- Parameters:
annotation
- the annotation whose name to return- Returns:
- the fully-qualified name of an annotation as a String
-
annotationBinaryName
Returns the binary name of an annotation as a String.- Parameters:
annotation
- the annotation whose binary name to return- Returns:
- the binary name of an annotation as a String
-
areSame
Returns true iff both annotations are of the same type and have the same annotation values.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.- Parameters:
a1
- the first AnnotationMirror to comparea2
- the second AnnotationMirror to compare- Returns:
- true iff a1 and a2 are the same annotation
-
compareByName
Return -1, 0, or 1 depending on whether the name of a1 is less, equal to, or greater than that of a2 (lexicographically).- Parameters:
a1
- the first AnnotationMirror to comparea2
- the second AnnotationMirror to compare- Returns:
- true iff a1 and a2 have the same annotation name
- See Also:
-
areSameByName
Return true iff a1 and a2 have the same annotation type.- Parameters:
a1
- the first AnnotationMirror to comparea2
- the second AnnotationMirror to compare- Returns:
- true iff a1 and a2 have the same annotation name
- See Also:
-
areSameByName
Checks that the annotationam
has the nameaname
(a fully-qualified type name). Values are ignored.- Parameters:
am
- the AnnotationMirror whose name to compareaname
- the string to compare- Returns:
- true if aname is the name of am
-
areSameByClass
@Deprecated public static boolean areSameByClass(AnnotationMirror am, Class<? extends Annotation> annoClass) Deprecated.useAnnotatedTypeFactory#areSameByClass
orareSameByName(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.AnnotationMirror)
Checks that the annotationam
has the name ofannoClass
. Values are ignored.This method is not very efficient. It is more efficient to use
AnnotatedTypeFactory#areSameByClass
orareSameByName(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.AnnotationMirror)
.- Parameters:
am
- the AnnotationMirror whose class to compareannoClass
- the class to compare- Returns:
- true if annoclass is the class of am
-
areSame
public static boolean areSame(Collection<? extends AnnotationMirror> c1, Collection<? extends AnnotationMirror> c2) Checks that two collections contain the same annotations.- Parameters:
c1
- the first collection to comparec2
- the second collection to compare- Returns:
- true iff c1 and c2 contain the same annotations, according to
areSame(AnnotationMirror, AnnotationMirror)
-
containsSame
Checks that the collection contains the annotation. Using Collection.contains does not always work, because it does not use areSame for comparison.- Parameters:
c
- a collection of AnnotationMirrorsanno
- the AnnotationMirror to search for in c- Returns:
- true iff c contains anno, according to areSame
-
getSame
public static @Nullable AnnotationMirror getSame(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Returns the AnnotationMirror inc
that is the same annotation asanno
.- Parameters:
c
- a collection of AnnotationMirrorsanno
- the AnnotationMirror to search for in c- Returns:
- AnnotationMirror with the same class as
anno
iff c contains anno, according to areSame; otherwise,null
-
containsSameByClass
public static boolean containsSameByClass(Collection<? extends AnnotationMirror> c, Class<? extends Annotation> anno) Checks that the collection contains the annotation. Using Collection.contains does not always work, because it does not use areSame for comparison.This method is not very efficient. It is more efficient to use
AnnotatedTypeFactory#containsSameByClass
orcontainsSameByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror>, java.lang.String)
.- Parameters:
c
- a collection of AnnotationMirrorsanno
- the annotation class to search for in c- Returns:
- true iff c contains anno, according to areSameByClass
-
getAnnotationByClass
public static @Nullable AnnotationMirror getAnnotationByClass(Collection<? extends AnnotationMirror> c, Class<? extends Annotation> anno) Returns the AnnotationMirror inc
that has the same class asanno
.This method is not very efficient. It is more efficient to use
AnnotatedTypeFactory#getAnnotationByClass
orgetAnnotationByName(java.util.Collection<? extends javax.lang.model.element.AnnotationMirror>, java.lang.String)
.- Parameters:
c
- a collection of AnnotationMirrorsanno
- the class to search for in c- Returns:
- AnnotationMirror with the same class as
anno
iff c contains anno, according to areSameByClass; otherwise,null
-
containsSameByName
Checks that the collection contains an annotation of the given name. Differs from using Collection.contains, which does not use areSameByName for comparison.- Parameters:
c
- a collection of AnnotationMirrorsanno
- the name to search for in c- Returns:
- true iff c contains anno, according to areSameByName
-
getAnnotationByName
public static @Nullable AnnotationMirror getAnnotationByName(Collection<? extends AnnotationMirror> c, String anno) Returns the AnnotationMirror inc
that has the same name asanno
.- Parameters:
c
- a collection of AnnotationMirrorsanno
- the name to search for in c- Returns:
- AnnotationMirror with the same name as
anno
iff c contains anno, according to areSameByName; otherwise,null
-
containsSameByName
public static boolean containsSameByName(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Checks that the collection contains an annotation of the given name. Differs from using Collection.contains, which does not use areSameByName for comparison.- Parameters:
c
- a collection of AnnotationMirrorsanno
- the annotation whose name to search for in c- Returns:
- true iff c contains anno, according to areSameByName
-
getSameByName
public static @Nullable AnnotationMirror getSameByName(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Returns the AnnotationMirror inc
that is the same annotation asanno
ignoring values.- Parameters:
c
- a collection of AnnotationMirrorsanno
- the annotation whose name to search for in c- Returns:
- AnnotationMirror with the same class as
anno
iff c contains anno, according to areSameByName; otherwise,null
-
compareAnnotationMirrors
Provide an ordering forAnnotationMirror
s. AnnotationMirrors are first compared by their fully-qualified names, then by their element values in order of the name of the element.- Parameters:
a1
- the first annotationa2
- the second annotation- Returns:
- an ordering over AnnotationMirrors based on their name and values
-
hasInheritedMeta
Returns true if the given annotation has a @Inherited meta-annotation.- Parameters:
anno
- the annotation to check for an @Inherited meta-annotation- Returns:
- true if the given annotation has a @Inherited meta-annotation
-
getElementKindsForTarget
Returns the set ofElementKind
s to whichtarget
applies, ignoring TYPE_USE.- Parameters:
target
- a location where an annotation can be written- Returns:
- the set of
ElementKind
s to whichtarget
applies, ignoring TYPE_USE
-
getElementKindsForElementType
Returns the set ofElementKind
s corresponding toelementType
. 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.- Parameters:
elementType
- the elementType to find ElementKinds for- Returns:
- the set of
ElementKind
s corresponding toelementType
-
getElementValuesWithDefaults
@Deprecated public static Map<? extends ExecutableElement,? extends AnnotationValue> getElementValuesWithDefaults(AnnotationMirror ad) Deprecated.use a method that takes anExecutableElement
Returns the values of an annotation's elements, including defaults. The method with the same name in JavacElements cannot be used directly, because it includes a cast to Attribute.Compound, which doesn't hold for annotations generated by the Checker Framework.This method is intended for use only by the framework. Clients should use a method that takes an
ExecutableElement
.- Parameters:
ad
- annotation to examine- Returns:
- the values of the annotation's elements, including defaults
- See Also:
-
AnnotationMirror.getElementValues()
JavacElements.getElementValuesWithDefaults(AnnotationMirror)
-
hasElementValue
Deprecated.use a method that takes anExecutableElement
Verify whether the element with the nameelementName
exists in the annotationanno
.This method is intended for use only by the framework. Clients should use a method that takes an
ExecutableElement
.- Parameters:
anno
- the annotation to examineelementName
- the name of the element- Returns:
- whether the element exists in anno
-
getElementValue
@Deprecated public static <T> T getElementValue(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Deprecated.Get the element with the nameelementName
of the annotationanno
. The result has typeexpectedType
.If the return type is an array, use
getElementValueArray(javax.lang.model.element.AnnotationMirror, java.lang.CharSequence, java.lang.Class<T>, boolean)
instead.If the return type is an enum, use
getElementValueEnum(javax.lang.model.element.AnnotationMirror, java.lang.CharSequence, java.lang.Class<T>, boolean)
instead.This method is intended only for use by the framework. A checker implementation should use
getElementValue(AnnotationMirror, ExecutableElement, Class)
orgetElementValue(AnnotationMirror, ExecutableElement, Class, Object)
.- Type Parameters:
T
- the class of the type- Parameters:
anno
- the annotation whose element to accesselementName
- the name of the element to accessexpectedType
- the type of the element and the return valueuseDefaults
- whether to apply default values to the element- Returns:
- the value of the element with the given name
-
getElementValueOrNull
public static <T> @Nullable T getElementValueOrNull(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Get the element with the nameelementName
of the annotationanno
, or return null if no such element exists.This method is intended only for use by the framework. A checker implementation should use
getElementValue(AnnotationMirror, ExecutableElement, Class, Object)
.- Type Parameters:
T
- the class of the type- Parameters:
anno
- the annotation whose element to accesselementName
- the name of the element to accessexpectedType
- the type of the element and the return valueuseDefaults
- whether to apply default values to the element- Returns:
- the value of the element with the given name, or null
-
getElementValueArrayOrNull
public static <T> @Nullable List<T> getElementValueArrayOrNull(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Get the element with the nameelementName
of the annotationanno
, or return null if no such element exists. One element of the result has typeexpectedType
.This method is intended only for use by the framework. A checker implementation should use
getElementValue(AnnotationMirror, ExecutableElement, Class, Object)
.- Type Parameters:
T
- the class of the component type- Parameters:
anno
- the annotation whose element to accesselementName
- the name of the element to accessexpectedType
- the component type of the element and of the return valueuseDefaults
- whether to apply default values to the element- Returns:
- the value of the element with the given name, or null
-
getElementValueEnum
@Deprecated public static <T extends Enum<T>> T getElementValueEnum(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Deprecated.Get the element with the namename
of the annotationanno
. The result is an enum of typeT
.This method is intended only for use by the framework. A checker implementation should use
getElementValueEnum(AnnotationMirror, ExecutableElement, Class)
orgetElementValueEnum(AnnotationMirror, ExecutableElement, Class, Enum)
.- Type Parameters:
T
- the class of the type- Parameters:
anno
- the annotation to disassembleelementName
- the name of the element to accessexpectedType
- the type of the element and the return value, an enumuseDefaults
- whether to apply default values to the element- Returns:
- the value of the element with the given name
-
getElementValueArray
@Deprecated public static <T> List<T> getElementValueArray(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Deprecated.use#getElementValueArray(AnnotationMirror, ExecutableElement, Class)
or#getElementValueArray(AnnotationMirror, ExecutableElement, Class, Object)
Get the element with the nameelementName
of the annotationanno
, where the element has an array type. One element of the result has typeexpectedType
.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.
This method is intended only for use by the framework. A checker implementation should use
#getElementValueArray(AnnotationMirror, ExecutableElement, Class)
or#getElementValueArray(AnnotationMirror, ExecutableElement, Class, Object)
.- Type Parameters:
T
- the class of the type- Parameters:
anno
- the annotation to disassembleelementName
- the name of the element to accessexpectedType
- the component type of the element and of the return typeuseDefaults
- whether to apply default values to the element- Returns:
- the value of the element with the given name; it is a new list, so it is safe for clients to side-effect
-
getElementValueEnumArray
@Deprecated public static <T extends Enum<T>> T[] getElementValueEnumArray(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Deprecated.Get the element with the nameelementName
of the annotationanno
, or the default value if no element is present explicitly, where the element has an array type and the elements areEnum
s. One element of the result has typeexpectedType
.This method is intended only for use by the framework. A checker implementation should use
getElementValueEnumArray(AnnotationMirror, ExecutableElement, Class)
orgetElementValueEnumArray(AnnotationMirror, ExecutableElement, Class, Enum[])
.- Type Parameters:
T
- the class of the type- Parameters:
anno
- the annotation to disassembleelementName
- the name of the element to accessexpectedType
- the component type of the element and of the return type, an enumuseDefaults
- whether to apply default values to the element- Returns:
- the value of the element with the given name
-
getElementValueClassName
@Deprecated public static @CanonicalName Name getElementValueClassName(AnnotationMirror anno, CharSequence elementName, boolean useDefaults) Deprecated.use an ExecutableElementGet the Name of the class that is referenced by elementelementName
.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.This method is intended only for use by the framework. A checker implementation should use
anno.getElementValues().get(someElement).getValue().asElement().getQualifiedName();
.- Parameters:
anno
- the annotation to disassembleelementName
- the name of the element to access; it must be present in the annotationuseDefaults
- whether to apply default values to the element- Returns:
- the name of the class that is referenced by element with the given name; may be an empty name, for a local or anonymous class
-
getElementValueClassNames
@Deprecated public static List<@CanonicalName Name> getElementValueClassNames(AnnotationMirror anno, CharSequence annoElement, boolean useDefaults) Deprecated.Get the list of Names of the classes that are referenced by elementelementName
. It fails if the class wasn't found.This method is intended only for use by the framework. A checker implementation should use
getElementValueClassNames(AnnotationMirror, ExecutableElement)
.- Parameters:
anno
- the annotation whose field to access; it must be present in the annotationannoElement
- the element/field ofanno
whose content is a list of classesuseDefaults
- whether to apply default values to the element- Returns:
- the names of classes in
anno.annoElement
-
getElementValue
public static <T> @Nullable T getElementValue(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the given element of the annotationanno
. The result has typeexpectedType
.If the return type is primitive, use
getElementValueInt(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.ExecutableElement)
orgetElementValueLong(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.ExecutableElement, long)
instead.If the return type is an array, use
getElementValueArray(javax.lang.model.element.AnnotationMirror, java.lang.CharSequence, java.lang.Class<T>, boolean)
instead.If the return type is an enum, use
getElementValueEnum(javax.lang.model.element.AnnotationMirror, java.lang.CharSequence, java.lang.Class<T>, boolean)
instead.- Type Parameters:
T
- the class of the type- Parameters:
anno
- the annotation whose element to accesselement
- the element to access; it must be present in the annotationexpectedType
- the type of the element and the return value- Returns:
- the value of the element with the given name
-
getElementValue
public static <T> @Nullable T getElementValue(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, T defaultValue) Get the given element of the annotationanno
. The result has typeexpectedType
.If the return type is primitive, use
getElementValueInt(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.ExecutableElement)
orgetElementValueLong(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.ExecutableElement, long)
instead.If the return type is an array, use
getElementValueArray(javax.lang.model.element.AnnotationMirror, java.lang.CharSequence, java.lang.Class<T>, boolean)
instead.If the return type is an enum, use
getElementValueEnum(javax.lang.model.element.AnnotationMirror, java.lang.CharSequence, java.lang.Class<T>, boolean)
instead.- Type Parameters:
T
- the class of the type- Parameters:
anno
- the annotation whose element to accesselement
- the element to accessexpectedType
- the type of the element and the return valuedefaultValue
- the value to return if the element is not present- Returns:
- the value of the element with the given name
-
getElementValueBoolean
public static boolean getElementValueBoolean(AnnotationMirror anno, ExecutableElement element, boolean defaultValue) Get the given boolean element of the annotationanno
.- Parameters:
anno
- the annotation whose element to accesselement
- the element to accessdefaultValue
- the value to return if the element is not present- Returns:
- the value of the element with the given name
-
getElementValueInt
Get the given integer element of the annotationanno
.- Parameters:
anno
- the annotation whose element to accesselement
- the element to access- Returns:
- the value of the element with the given name
-
getElementValueInt
public static int getElementValueInt(AnnotationMirror anno, ExecutableElement element, int defaultValue) Get the given integer element of the annotationanno
.- Parameters:
anno
- the annotation whose element to accesselement
- the element to accessdefaultValue
- the value to return if the element is not present- Returns:
- the value of the element with the given name
-
getElementValueLong
public static long getElementValueLong(AnnotationMirror anno, ExecutableElement element, long defaultValue) Get the given long element of the annotationanno
.- Parameters:
anno
- the annotation whose element to accesselement
- the element to accessdefaultValue
- the value to return if the element is not present- Returns:
- the value of the element with the given name
-
getElementValueEnum
public static <T extends Enum<T>> T getElementValueEnum(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the element with the namename
of the annotationanno
. The result is an enum of typeT
.- Type Parameters:
T
- the class of the type- Parameters:
anno
- the annotation to disassembleelement
- the element to access; it must be present in the annotationexpectedType
- the type of the element and the return value, an enum- Returns:
- the value of the element with the given name
-
getElementValueEnum
public static <T extends Enum<T>> T getElementValueEnum(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, T defaultValue) Get the element with the namename
of the annotationanno
. The result is an enum of typeT
.- Type Parameters:
T
- the class of the type- Parameters:
anno
- the annotation to disassembleelement
- the element to accessexpectedType
- the type of the element and the return value, an enumdefaultValue
- the value to return if the element is not present- Returns:
- the value of the element with the given name
-
getElementValueEnumArray
public static <T extends Enum<T>> T[] getElementValueEnumArray(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the element with the namename
of the annotationanno
. The result is an array of typeT
.- Type Parameters:
T
- the enum class of the component type- Parameters:
anno
- the annotation to disassembleelement
- the element to access; it must be present in the annotationexpectedType
- the component type of the element and of the return value, an enum- Returns:
- the value of the element with the given name
-
getElementValueEnumArray
public static <T extends Enum<T>> T[] getElementValueEnumArray(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, T[] defaultValue) Get the element with the namename
of the annotationanno
. The result is an array of typeT
.- Type Parameters:
T
- the enum class of the component type- Parameters:
anno
- the annotation to disassembleelement
- the element to accessexpectedType
- the component type of the element and of the return typedefaultValue
- the value to return if the annotation does not have the element- Returns:
- the value of the element with the given name
-
getElementValueArray
public static <T> List<T> getElementValueArray(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the given element of the annotationanno
, where the element has an array type. One element of the result has typeexpectedType
.- Type Parameters:
T
- the class of the component type- Parameters:
anno
- the annotation to disassembleelement
- the element to access; it must be present in the annotationexpectedType
- the component type of the element and of the return type- Returns:
- the value of the element with the given name; it is a new list, so it is safe for clients to side-effect
-
getElementValueArray
public static <T> List<T> getElementValueArray(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, List<T> defaultValue) Get the given element of the annotationanno
, where the element has an array type. One element of the result has typeexpectedType
.- Type Parameters:
T
- the class of the component type- Parameters:
anno
- the annotation to disassembleelement
- the element to accessexpectedType
- the component type of the element and of the return typedefaultValue
- the value to return if the element is not present- Returns:
- the value of the element with the given name; it is a new list, so it is safe for clients to side-effect
-
annotationValueListToEnumArray
public static <T extends Enum<T>> T[] annotationValueListToEnumArray(AnnotationValue avList, Class<T> expectedType) Converts a list of AnnotationValue to an array of enum.- Type Parameters:
T
- the element type of the enum array- Parameters:
avList
- a list of AnnotationValueexpectedType
- the component type of the element and of the return type, an enum- Returns:
- an array of enum, converted from the input list
-
annotationValueListToEnumArray
public static <T extends Enum<T>> T[] annotationValueListToEnumArray(List<AnnotationValue> la, Class<T> expectedType) Converts a list of AnnotationValue to an array of enum.- Type Parameters:
T
- the element type of the enum array- Parameters:
la
- a list of AnnotationValueexpectedType
- the component type of the element and of the return type, an enum- Returns:
- an array of enum, converted from the input list
-
getElementValueClassName
public static @CanonicalName Name getElementValueClassName(AnnotationMirror anno, ExecutableElement element) Get the Name of the class that is referenced by elementelement
.This is a convenience method for the most common use-case. It is like
getElementValue(anno, element, ClassType.class).getQualifiedName()
, but this method ensures consistent use of the qualified name.This method is intended only for use by the framework. A checker implementation should use
anno.getElementValues().get(someElement).getValue().asElement().getQualifiedName();
.- Parameters:
anno
- the annotation to disassembleelement
- the element to access; it must be present in the annotation- Returns:
- the name of the class that is referenced by element with the given name; may be an empty name, for a local or anonymous class
-
getElementValueClassNames
public static List<@CanonicalName Name> getElementValueClassNames(AnnotationMirror anno, ExecutableElement element) Get the list of Names of the classes that are referenced by elementelement
. It fails if the class wasn't found.- Parameters:
anno
- the annotation whose field to access; it must be present in the annotationelement
- the element/field ofanno
whose content is a list of classes- Returns:
- the names of classes in
anno.annoElement
-
sameElementValues
Returns true if the two annotations have the same elements (fields). The argumentsam1
andam2
must be the same type of annotation.- Parameters:
am1
- the first AnnotationMirror to compaream2
- the second AnnotationMirror to compare- Returns:
- true if the two annotations have the same elements (fields)
-
sameAnnotationValue
Return true iff the two AnnotationValue objects are the same. Use this instead of CheckerFrameworkAnnotationValue.equals, which wouldn't get called if the receiver is some AnnotationValue other than CheckerFrameworkAnnotationValue.- Parameters:
av1
- the first AnnotationValue to compareav2
- the second AnnotationValue to compare- Returns:
- true if the two annotation values are the same
-
annotationValueContains
Returns true if an AnnotationValue list contains the given value.Using this method is slightly cheaper than creating a new
List<String>
just for the purpose of testing containment within it.- Parameters:
avList
- an AnnotationValue that is null or a list of Stringss
- a string- Returns:
- true if
av
containss
-
annotationValueContains
Returns true if an AnnotationValue list contains the given value.Using this method is slightly cheaper than creating a new
List<String>
just for the purpose of testing containment within it.- Parameters:
avList
- a list of Strings (asAnnotationValue
s)s
- a string- Returns:
- true if
av
containss
-
annotationValueContainsToString
Returns true if an AnnotationValue list contains a value whosetoString()
is the given string.Using this method is slightly cheaper than creating a new
List
just for the purpose of testing containment within it.- Parameters:
avList
- an AnnotationValue that is null or a lists
- a string- Returns:
- true if
av
containss
-
annotationValueContainsToString
public static boolean annotationValueContainsToString(List<? extends AnnotationValue> avList, String s) Returns true if an AnnotationValue list contains a value whosetoString()
is the given string.Using this method is slightly cheaper than creating a new
List
just for the purpose of testing containment within it.- Parameters:
avList
- a list of Strings (asAnnotationValue
s)s
- a string- Returns:
- true if
av
containss
-
annotationValueToList
Converts an annotation value to a list.To test containment, use
annotationValueContains(AnnotationValue, String)
orannotationValueContainsToString(AnnotationValue, String)
.- Type Parameters:
T
- the class of the type- Parameters:
avList
- an AnnotationValue that is null or a list of StringsexpectedType
- the component type of the argument and of the return type, an enum- Returns:
- the annotation value, converted to a list
-
annotationValueToList
public static <T> List<T> annotationValueToList(List<? extends AnnotationValue> avList, Class<T> expectedType) Converts an annotation value to a list.To test containment, use
annotationValueContains(List, String)
orannotationValueContainsToString(List, String)
.- Type Parameters:
T
- the class of the type- Parameters:
avList
- a list of Strings (asAnnotationValue
s)expectedType
- the component type of the argument and of the return type, an enum- Returns:
- the annotation value, converted to a list
-
updateMappingToImmutableSet
public static <T extends @NonNull Object> void updateMappingToImmutableSet(Map<T, AnnotationMirrorSet> map, T key, AnnotationMirrorSet newQual) Update a map, to addnewQual
to the set thatkey
maps to. The mapped-to element is an unmodifiable set.See org.checkerframework.framework.type.QualifierHierarchy#updateMappingToMutableSet(QualifierHierarchy, Map, Object, AnnotationMirror).
- Type Parameters:
T
- the key type- Parameters:
map
- the map to updatekey
- the key whose value to updatenewQual
- the element to add to the given key's value
-
getExplicitAnnotationsOnConstructorResult
public static AnnotationMirrorSet getExplicitAnnotationsOnConstructorResult(MethodTree constructorDeclaration) Returns the annotations explicitly written on a constructor result. Callers should check thatconstructorDeclaration
is in fact a declaration of a constructor.- Parameters:
constructorDeclaration
- declaration tree of constructor- Returns:
- set of annotations explicit on the resulting type of the constructor
-
isDeclarationAnnotation
Returns true if anno is a declaration annotation. In other words, returns true if anno cannot be written on uses of types.- Parameters:
anno
- the AnnotationMirror- Returns:
- true if anno is a declaration annotation
-
hasTypeQualifierElementTypes
Returns true if the given array containsElementType.TYPE_USE
, false otherwise.- Parameters:
elements
- an array ofElementType
valuescls
- the annotation class being tested; used for diagnostic messages only- Returns:
- true iff the give array contains
ElementType.TYPE_USE
- Throws:
RuntimeException
- if the array contains bothElementType.TYPE_USE
and something besidesElementType.TYPE_PARAMETER
-
toStringSimple
Returns a string representation of the annotation mirrors, using simple (not fully-qualified) names.- Parameters:
annos
- annotations to format- Returns:
- the string representation, using simple (not fully-qualified) names
-
annotationMirrorToClass
Converts an AnnotationMirror to a Class. Throws an exception if it is not able to do so.- Parameters:
am
- an AnnotationMirror- Returns:
- the Class corresponding to the given AnnotationMirror
-
AnnotatedTypeFactory#areSameByClass
orareSameByName(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.AnnotationMirror)