Class AnnotationUtils
Note: AnnotationMirrors are immutable.
-
Method Summary
Modifier and TypeMethodDescriptionstatic final @BinaryName StringannotationBinaryName(AnnotationMirror annotation) Returns the binary name of an annotation as a String.static Class<?> Converts an AnnotationMirror to a Class.static final @CanonicalName StringannotationName(AnnotationMirror annotation) Returns the fully-qualified name of an annotation as a String.static booleanannotationValueContains(List<? extends AnnotationValue> avList, String s) Returns true if an AnnotationValue list contains the given value.static booleanannotationValueContains(@Nullable AnnotationValue avList, String s) Returns true if an AnnotationValue list contains the given value.static booleanannotationValueContainsToString(List<? extends AnnotationValue> avList, String s) Returns true if an AnnotationValue list contains a value whosetoString()is the given string.static booleanReturns 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 booleanareSame(Collection<? extends AnnotationMirror> c1, Collection<? extends AnnotationMirror> c2) Checks that two collections contain the same annotations.static booleanareSame(AnnotationMirror a1, AnnotationMirror a2) Returns true iff both annotations are of the same type and have the same annotation values.static booleanareSameByClass(AnnotationMirror am, Class<? extends Annotation> annoClass) Deprecated.static booleanareSameByName(AnnotationMirror am, String aname) Checks that the annotationamhas the nameaname(a fully-qualified type name).static booleanReturn true iff a1 and a2 have the same annotation type.static intProvide an ordering forAnnotationMirrors.static intReturn -1, 0, or 1 depending on whether the name of a1 is less, equal to, or greater than that of a2 (lexicographically).static booleancontainsSame(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Checks that the collection contains the annotation.static booleancontainsSameByClass(Collection<? extends AnnotationMirror> c, Class<? extends Annotation> anno) Checks that the collection contains the annotation.static booleancontainsSameByName(Collection<? extends AnnotationMirror> c, String anno) Checks that the collection contains an annotation of the given name.static booleancontainsSameByName(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Checks that the collection contains an annotation of the given name.static @Nullable AnnotationMirrorgetAnnotationByClass(Collection<? extends AnnotationMirror> c, Class<? extends Annotation> anno) Returns the AnnotationMirror incthat has the same class asanno.static @Nullable AnnotationMirrorgetAnnotationByName(Collection<? extends AnnotationMirror> c, String anno) Returns the AnnotationMirror incthat has the same name asanno.static EnumSet<ElementKind> getElementKindsForElementType(ElementType elementType) Returns the set ofElementKinds corresponding toelementType.static EnumSet<ElementKind> getElementKindsForTarget(@Nullable Target target) Returns the set ofElementKinds to whichtargetapplies, ignoring TYPE_USE.static <T> TgetElementValue(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) static <T> TgetElementValue(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the given element of the annotationanno.static <T> TgetElementValue(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 nameelementNameof the annotationanno, or return null if no such element exists.static booleangetElementValueBoolean(AnnotationMirror anno, ExecutableElement element, boolean defaultValue) Get the given boolean element of the annotationanno.static @CanonicalName NamegetElementValueClassName(AnnotationMirror anno, CharSequence elementName, boolean useDefaults) Deprecated.use an ExecutableElementstatic @CanonicalName NamegetElementValueClassName(AnnotationMirror anno, ExecutableElement element) Get the Name of the class that is referenced by elementelement.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, ExecutableElement element, Class<T> expectedType) Get the element with the namenameof the annotationanno.static <T extends Enum<T>>
TgetElementValueEnum(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, T defaultValue) Get the element with the namenameof the annotationanno.static <T extends Enum<T>>
T[]getElementValueEnumArray(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType) Get the element with the namenameof the annotationanno.static <T extends Enum<T>>
T[]getElementValueEnumArray(AnnotationMirror anno, ExecutableElement element, Class<T> expectedType, T[] defaultValue) Get the element with the namenameof the annotationanno.static intgetElementValueInt(AnnotationMirror anno, ExecutableElement element) Get the given integer element of the annotationanno.static intgetElementValueInt(AnnotationMirror anno, ExecutableElement element, int defaultValue) Get the given integer element of the annotationanno.static longgetElementValueLong(AnnotationMirror anno, ExecutableElement element, long defaultValue) Get the given long element of the annotationanno.static <T> TgetElementValueNotOnClasspath(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, T defaultValue) Get the element with the nameelementNameof the annotationanno.static <T> @Nullable TgetElementValueOrNull(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Get the element with the nameelementNameof the annotationanno, or return null if no such element exists.static AnnotationMirrorSetgetExplicitAnnotationsOnConstructorResult(MethodTree constructorDeclaration) Returns the annotations explicitly written on a constructor result.static @Nullable AnnotationMirrorgetSame(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Returns the AnnotationMirror incthat is the same annotation asanno.static @Nullable AnnotationMirrorgetSameByName(Collection<? extends AnnotationMirror> c, AnnotationMirror anno) Returns the AnnotationMirror incthat is the same annotation asannoignoring values.static booleanReturns true if the given annotation has a @Inherited meta-annotation.static booleanhasTypeQualifierElementTypes(ElementType[] elements, Class<?> cls) Returns true if the given array containsElementType.TYPE_USE, false otherwise.static booleanReturns true if anno is a declaration annotation.static booleanReturn true iff the two AnnotationValue objects are the same.static booleanReturns true if the two annotations have the same elements (fields).static StringReturns a string representation of the annotation mirror, using simple (not fully-qualified) names.static voidAppends a string representation of the annotation mirror, using simple (not fully-qualified) names, to the StringBuilder.static StringReturns 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 addnewQualto the set thatkeymaps 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 annotationamhas 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#areSameByClassorareSameByName(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.AnnotationMirror)Checks that the annotationamhas the name ofannoClass. Values are ignored.This method is not very efficient. It is more efficient to use
AnnotatedTypeFactory#areSameByClassorareSameByName(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 incthat 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
annoiff 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#containsSameByClassorcontainsSameByName(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 incthat has the same class asanno.This method is not very efficient. It is more efficient to use
AnnotatedTypeFactory#getAnnotationByClassorgetAnnotationByName(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
annoiff 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 incthat 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
annoiff 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 incthat is the same annotation asannoignoring values.- Parameters:
c- a collection of AnnotationMirrorsanno- the annotation whose name to search for in c- Returns:
- AnnotationMirror with the same class as
annoiff c contains anno, according to areSameByName; otherwise,null
-
compareAnnotationMirrors
Provide an ordering forAnnotationMirrors. 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 ofElementKinds to whichtargetapplies, ignoring TYPE_USE.- Parameters:
target- a location where an annotation can be written- Returns:
- the set of
ElementKinds to whichtargetapplies, ignoring TYPE_USE
-
getElementKindsForElementType
Returns the set ofElementKinds 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
ElementKinds corresponding toelementType
-
getElementValueNotOnClasspath
public static <T> T getElementValueNotOnClasspath(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, T defaultValue) Get the element with the nameelementNameof the annotationanno. The result has typeexpectedType. If there is no value forelementName,defaultValueis returnedThis method is intended only for use when the class of the annotation is not on the user's classpath. This is for users of the Dataflow Framework that do not use the rest of the Checker Framework. Type-checkers can assume that checker-qual.jar is on the classpath and 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 valuedefaultValue- the value to return if the element is not present- Returns:
- the value of the element with the given name
-
getElementValue
@Deprecated public static <T> T getElementValue(AnnotationMirror anno, CharSequence elementName, Class<T> expectedType, boolean useDefaults) Deprecated.Get the element with the nameelementNameof 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, javax.lang.model.element.ExecutableElement, java.lang.Class<T>)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 nameelementNameof 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 nameelementNameof 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
-
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 nameelementNameof 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
-
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
-
getElementValue
public static <T> 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, javax.lang.model.element.ExecutableElement, java.lang.Class<T>)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> 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, javax.lang.model.element.ExecutableElement, java.lang.Class<T>)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 namenameof 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 namenameof 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 namenameof 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 namenameof 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 ofannowhose 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 argumentsam1andam2must 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
avcontainss
-
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 (asAnnotationValues)s- a string- Returns:
- true if
avcontainss
-
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
Listjust for the purpose of testing containment within it.- Parameters:
avList- an AnnotationValue that is null or a lists- a string- Returns:
- true if
avcontainss
-
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
Listjust for the purpose of testing containment within it.- Parameters:
avList- a list of Strings (asAnnotationValues)s- a string- Returns:
- true if
avcontainss
-
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 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 (asAnnotationValues)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 addnewQualto the set thatkeymaps 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 thatconstructorDeclarationis 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 ofElementTypevaluescls- 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_USEand 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
-
toStringSimple
Returns a string representation of the annotation mirror, using simple (not fully-qualified) names.- Parameters:
am- annotation to format- Returns:
- the string representation, using simple (not fully-qualified) names
-
toStringSimple
Appends a string representation of the annotation mirror, using simple (not fully-qualified) names, to the StringBuilder.- Parameters:
am- annotation to formatsb- StringBuilder to which the string representation of am, using simple (not fully-qualified) names, is appended
-
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#areSameByClassorareSameByName(javax.lang.model.element.AnnotationMirror, javax.lang.model.element.AnnotationMirror)