public class AnnotationUtils extends Object
Modifier and Type | Method and Description |
---|---|
static @Interned String |
annotationName(AnnotationMirror annotation) |
static Comparator<AnnotationMirror> |
annotationOrdering()
provide ordering for
AnnotationMirror based on their fully qualified name. |
static String |
annotationSimpleName(AnnotationMirror annotation) |
static boolean |
areSame(@Nullable AnnotationMirror a1,
@Nullable AnnotationMirror a2)
Checks if both annotations are the same.
|
static boolean |
areSame(Collection<? extends AnnotationMirror> c1,
Collection<? extends AnnotationMirror> c2)
Checks that two collections contain the same annotations.
|
static boolean |
areSameByClass(AnnotationMirror am,
Class<? extends Annotation> anno)
Checks that the annotation
am has the name of anno . |
static boolean |
areSameByName(AnnotationMirror am,
@Interned String aname)
Checks that the annotation
am has the name aname . |
static boolean |
areSameIgnoringValues(AnnotationMirror a1,
AnnotationMirror a2) |
static void |
clear() |
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 |
containsSameIgnoringValues(Collection<? extends AnnotationMirror> c,
AnnotationMirror anno)
Checks that the collection contains the annotation ignoring values.
|
static <V> Map<AnnotationMirror,V> |
createAnnotationMap()
Create a map suitable for storing
AnnotationMirror as keys. |
static Set<AnnotationMirror> |
createAnnotationSet()
Constructs a
Set suitable for storing AnnotationMirror s. |
static AnnotationMirror |
fromClass(Elements elements,
Class<? extends Annotation> clazz)
Creates an
AnnotationMirror given by a particular annotation class. |
static AnnotationMirror |
fromName(Elements elements,
CharSequence name)
Creates an
AnnotationMirror given by a particular fully-qualified name. |
static AnnotationMirror |
getAnnotationByClass(Collection<? extends AnnotationMirror> c,
Class<? extends Annotation> anno)
Returns the AnnotationMirror in
c that has the same class as anno . |
static <T> T |
getElementValue(AnnotationMirror anno,
CharSequence name,
Class<T> expectedType,
boolean useDefaults)
Get the attribute with the name
name of the annotation anno . |
static <T> List<T> |
getElementValueArray(AnnotationMirror anno,
CharSequence name,
Class<T> expectedType,
boolean useDefaults)
Get the attribute with the name
name of the annotation anno , where the
attribute has an array type. |
static Class<?> |
getElementValueClass(AnnotationMirror anno,
CharSequence name,
boolean useDefaults)
Get the Class that is referenced by attribute
name . |
static Name |
getElementValueClassName(AnnotationMirror anno,
CharSequence name,
boolean useDefaults)
Get the Name of the class that is referenced by attribute
name . |
static List<Name> |
getElementValueClassNames(AnnotationMirror anno,
CharSequence name,
boolean useDefaults)
Get the list of Names of the classes that are referenced by attribute
name . |
static <T extends Enum<T>> |
getElementValueEnum(AnnotationMirror anno,
CharSequence name,
Class<T> t,
boolean useDefaults)
Version that is suitable for Enum elements.
|
static <T extends Enum<T>> |
getElementValueEnumArray(AnnotationMirror anno,
CharSequence name,
Class<T> t,
boolean useDefaults)
Get the attribute with the name
name of the annotation anno , or the default
value if no attribute is present explicitly, where the attribute has an array type and the
elements are Enum s. |
static Map<? extends ExecutableElement,? extends AnnotationValue> |
getElementValuesWithDefaults(AnnotationMirror ad)
Returns the values of an annotation's attributes, including defaults.
|
static Set<AnnotationMirror> |
getExplicitAnnotationsOnConstructorResult(MethodTree constructorDeclaration)
Returns the annotations explicitly written on a constructor result.
|
static AnnotationMirror |
getSame(Collection<? extends AnnotationMirror> c,
AnnotationMirror anno)
Returns the AnnotationMirror in
c that is the same annotation as anno . |
static AnnotationMirror |
getSameIgnoringValues(Collection<? extends AnnotationMirror> c,
AnnotationMirror anno)
Returns the AnnotationMirror in
c that is the same annotation as anno
ignoring values. |
static <T> boolean |
hasElementValue(AnnotationMirror anno,
CharSequence name)
Verify whether the attribute with the name
name exists in the annotation anno . |
static boolean |
hasInheritedMeta(AnnotationMirror anno)
Returns true if the given annotation has a @Inherited meta-annotation.
|
static <T> void |
updateMappingToImmutableSet(Map<T,Set<AnnotationMirror>> map,
T key,
Set<AnnotationMirror> newQual)
See checkers.types.QualifierHierarchy#updateMappingToMutableSet(QualifierHierarchy, Map,
Object, AnnotationMirror) (Not linked because it is in an independent project.
|
public static void clear()
public static AnnotationMirror fromName(Elements elements, CharSequence name)
AnnotationMirror
given by a particular fully-qualified name.
getElementValues on the result returns an empty map.elements
- the element utilities to usename
- the name of the annotation to createAnnotationMirror
of type namepublic static AnnotationMirror fromClass(Elements elements, Class<? extends Annotation> clazz)
AnnotationMirror
given by a particular annotation class.elements
- the element utilities to useclazz
- the annotation classAnnotationMirror
of type given typepublic static final @Interned String annotationName(AnnotationMirror annotation)
public static String annotationSimpleName(AnnotationMirror annotation)
public static boolean areSame(@Nullable AnnotationMirror a1, @Nullable AnnotationMirror a2)
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).
public static boolean areSameIgnoringValues(AnnotationMirror a1, AnnotationMirror a2)
areSame(AnnotationMirror, AnnotationMirror)
public static boolean areSameByName(AnnotationMirror am, @Interned String aname)
am
has the name aname
. Values are ignored.public static boolean areSameByClass(AnnotationMirror am, Class<? extends Annotation> anno)
am
has the name of anno
. Values are ignored.public static boolean areSame(Collection<? extends AnnotationMirror> c1, Collection<? extends AnnotationMirror> c2)
public static boolean containsSame(Collection<? extends AnnotationMirror> c, AnnotationMirror anno)
public static AnnotationMirror getSame(Collection<? extends AnnotationMirror> c, AnnotationMirror anno)
c
that is the same annotation as anno
.anno
iff c contains anno, according
to areSame; otherwise, null
public static boolean containsSameByClass(Collection<? extends AnnotationMirror> c, Class<? extends Annotation> anno)
public static AnnotationMirror getAnnotationByClass(Collection<? extends AnnotationMirror> c, Class<? extends Annotation> anno)
c
that has the same class as anno
.anno
iff c contains anno, according
to areSameByClass; otherwise, null
public static boolean containsSameIgnoringValues(Collection<? extends AnnotationMirror> c, AnnotationMirror anno)
public static AnnotationMirror getSameIgnoringValues(Collection<? extends AnnotationMirror> c, AnnotationMirror anno)
c
that is the same annotation as anno
ignoring values.anno
iff c contains anno, according
to areSameIgnoringValues; otherwise, null
public static Comparator<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> Map<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 Set<AnnotationMirror> createAnnotationSet()
Set
suitable 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(AnnotationMirror anno)
public static Map<? extends ExecutableElement,? extends AnnotationValue> getElementValuesWithDefaults(AnnotationMirror ad)
ad
- annotation to examineAnnotationMirror.getElementValues()
,
JavacElements.getElementValuesWithDefaults(AnnotationMirror)
public static <T> boolean hasElementValue(AnnotationMirror anno, CharSequence name)
name
exists in the annotation anno
.anno
- the annotation to examinename
- the name of the attributepublic static <T> T getElementValue(AnnotationMirror anno, CharSequence name, Class<T> expectedType, boolean useDefaults)
name
of the annotation anno
. The result is
expected to have type expectedType
.
Note 1: The method does not work well for attributes of an array type (as it
would return a list of AnnotationValue
s). Use getElementValueArray
instead.
Note 2: The method does not work for attributes 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.
anno
- the annotation to disassemblename
- the name of the attribute to accessexpectedType
- the expected type used to cast the return typeuseDefaults
- whether to apply default values to the attributepublic static <T extends Enum<T>> T getElementValueEnum(AnnotationMirror anno, CharSequence name, Class<T> t, boolean useDefaults)
public static <T> List<T> getElementValueArray(AnnotationMirror anno, CharSequence name, Class<T> expectedType, boolean useDefaults)
name
of the annotation anno
, where the
attribute 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.
anno
- the annotation to disassemblename
- the name of the attribute to accessexpectedType
- the expected type used to cast the return typeuseDefaults
- whether to apply default values to the attributepublic static <T extends Enum<T>> List<T> getElementValueEnumArray(AnnotationMirror anno, CharSequence name, Class<T> t, boolean useDefaults)
name
of the annotation anno
, or the default
value if no attribute is present explicitly, where the attribute has an array type and the
elements are Enum
s. One element of the result is expected to have type expectedType
.public static Name getElementValueClassName(AnnotationMirror anno, CharSequence name, boolean useDefaults)
name
.
This is a convenience method for the most common use-case. Like getElementValue(anno, name, ClassType.class).getQualifiedName(), but this method ensures consistent use of the qualified name.
public static List<Name> getElementValueClassNames(AnnotationMirror anno, CharSequence name, boolean useDefaults)
name
.public static Class<?> getElementValueClass(AnnotationMirror anno, CharSequence name, boolean useDefaults)
name
. This method uses Class.forName to
load the class. It returns null if the class wasn't found.public static <T> void updateMappingToImmutableSet(Map<T,Set<AnnotationMirror>> map, T key, Set<AnnotationMirror> newQual)
public static Set<AnnotationMirror> getExplicitAnnotationsOnConstructorResult(MethodTree constructorDeclaration)
constructorDeclaration
is in fact a declaration of a constructor.constructorDeclaration
- declaration tree of constructor