Class AnnotationBuilder
Constructing an AnnotationMirror
requires:
- Constructing the builder with the desired annotation class
- Setting each value individually using
setValue
methods - Calling
build()
to get the annotation
IllegalStateException
is thrown.
All setter methods throw IllegalArgumentException
if the specified element is not
found, or if the given value is not a subtype of the expected type.
TODO: Doesn't type-check arrays yet
-
Constructor Summary
ConstructorDescriptionCreate a new AnnotationBuilder for the given annotation name (with no elements/fields, but they can be added later).AnnotationBuilder
(ProcessingEnvironment env, Class<? extends Annotation> anno) Create a new AnnotationBuilder for the given annotation and environment (with no elements/fields, but they can be added later).AnnotationBuilder
(ProcessingEnvironment env, AnnotationMirror annotation) Create a new AnnotationBuilder that copies the given annotation, including its elements/fields. -
Method Summary
Modifier and TypeMethodDescriptionbuild()
void
copyElementValuesFromAnnotation
(AnnotationMirror other, String... ignorableElements) Copies every element value from the given annotation.void
copyElementValuesFromAnnotation
(AnnotationMirror valueHolder, Collection<ExecutableElement> ignorableElements) Copies every element value from the given annotation.void
copyRenameElementValuesFromAnnotation
(AnnotationMirror valueHolder, Map<String, String> elementNameRenaming) Copies the specified element values from the given annotation, using the specified renaming map.static Map<String,
AnnotationValue> elementNamesValues
(String elementName, Object elementValue) Creates a mapping between element/field names and values.findElement
(CharSequence key) static AnnotationMirror
fromClass
(Elements elements, Class<? extends Annotation> aClass) Creates anAnnotationMirror
that uses default values for elements/fields.static AnnotationMirror
fromClass
(Elements elements, Class<? extends Annotation> aClass, Map<String, AnnotationValue> elementNamesValues) Creates anAnnotationMirror
given by a particular annotation class and a name-to-value mapping for the elements/fields.static @Nullable AnnotationMirror
fromName
(Elements elements, @FullyQualifiedName CharSequence name) Creates anAnnotationMirror
given by a particular fully-qualified name.static @Nullable AnnotationMirror
fromName
(Elements elements, @FullyQualifiedName CharSequence name, Map<String, AnnotationValue> elementNamesValues) Creates anAnnotationMirror
given by a particular fully-qualified name and element/field values.Returns the type element of the annotation that is being built.removeElement
(CharSequence elementName) Remove the element/field with the given name.setValue
(CharSequence elementName, Boolean value) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, Character value) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, Class<?> value) setValue
(CharSequence elementName, Double value) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, Enum<?> value) setValue
(CharSequence elementName, Enum<?>[] values) setValue
(CharSequence elementName, Float value) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, Integer value) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, Long value) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, Object[] values) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, Short value) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, String value) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, List<? extends Object> values) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, AnnotationMirror value) Set the element/field with the given name, to the given value.setValue
(CharSequence elementName, VariableElement value) setValue
(CharSequence elementName, VariableElement[] values) setValue
(CharSequence elementName, TypeMirror value) setValue
(ExecutableElement element, List<? extends @NonNull Object> values) Set the element to the given value.
-
Constructor Details
-
AnnotationBuilder
Create a new AnnotationBuilder for the given annotation and environment (with no elements/fields, but they can be added later).- Parameters:
env
- the processing environmentanno
- the class of the annotation to build
-
AnnotationBuilder
Create a new AnnotationBuilder for the given annotation name (with no elements/fields, but they can be added later).- Parameters:
env
- the processing environmentname
- the canonical name of the annotation to build
-
AnnotationBuilder
Create a new AnnotationBuilder that copies the given annotation, including its elements/fields.- Parameters:
env
- the processing environmentannotation
- the annotation to copy
-
-
Method Details
-
getAnnotationElt
Returns the type element of the annotation that is being built.- Returns:
- the type element of the annotation that is being built
-
elementNamesValues
public static Map<String,AnnotationValue> elementNamesValues(String elementName, Object elementValue) Creates a mapping between element/field names and values.- Parameters:
elementName
- the name of an element/field to initializeelementValue
- the initial value for the element/field- Returns:
- a mapping from the element name to the element value
-
fromClass
Creates anAnnotationMirror
that uses default values for elements/fields. getElementValues on the result returns default values. If any element does not have a default, this method throws an exception.Most clients should use
fromName(javax.lang.model.util.Elements, java.lang.CharSequence)
, using a Name created by the compiler. This method is provided as a convenience to create an AnnotationMirror from scratch in a checker's code.- Parameters:
elements
- the element utilities to useaClass
- the annotation class- Returns:
- an
AnnotationMirror
of the given type - Throws:
UserError
- if the annotation corresponding to the class could not be loaded
-
fromClass
public static AnnotationMirror fromClass(Elements elements, Class<? extends Annotation> aClass, Map<String, AnnotationValue> elementNamesValues) Creates anAnnotationMirror
given by a particular annotation class and a name-to-value mapping for the elements/fields.For other elements, getElementValues on the result returns default values. If any such element does not have a default, this method throws an exception.
Most clients should use
fromName(javax.lang.model.util.Elements, java.lang.CharSequence)
, using a Name created by the compiler. This method is provided as a convenience to create an AnnotationMirror from scratch in a checker's code.- Parameters:
elements
- the element utilities to useaClass
- the annotation classelementNamesValues
- the values for the annotation's elements/fields- Returns:
- an
AnnotationMirror
of the given type
-
fromName
public static @Nullable AnnotationMirror fromName(Elements elements, @FullyQualifiedName CharSequence name) Creates anAnnotationMirror
given by a particular fully-qualified name. getElementValues on the result returns default values. If any element does not have a default, this method throws an exception.This method returns null if the annotation corresponding to the name could not be loaded.
- Parameters:
elements
- the element utilities to usename
- the name of the annotation to create- Returns:
- an
AnnotationMirror
of type name or null if the annotation couldn't be loaded
-
fromName
public static @Nullable AnnotationMirror fromName(Elements elements, @FullyQualifiedName CharSequence name, Map<String, AnnotationValue> elementNamesValues) Creates anAnnotationMirror
given by a particular fully-qualified name and element/field values. If any element is not specified by theelementValues
argument, the default value is used. If any such element does not have a default, this method throws an exception.This method returns null if the annotation corresponding to the name could not be loaded.
- Parameters:
elements
- the element utilities to usename
- the name of the annotation to createelementNamesValues
- the values for the annotation's elements/fields- Returns:
- an
AnnotationMirror
of type name or null if the annotation couldn't be loaded
-
build
-
copyElementValuesFromAnnotation
Copies every element value from the given annotation. If an element in the given annotation doesn't exist in the annotation to be built, an error is raised unless the element is specified inignorableElements
.- Parameters:
other
- the annotation that holds the values to be copied; need not be an annotation of the same type of the one being builtignorableElements
- the names of elements ofother
that can be safely dropped
-
copyElementValuesFromAnnotation
public void copyElementValuesFromAnnotation(AnnotationMirror valueHolder, Collection<ExecutableElement> ignorableElements) Copies every element value from the given annotation. If an element in the given annotation doesn't exist in the annotation to be built, an error is raised unless the element is specified inignorableElements
.- Parameters:
valueHolder
- the annotation that holds the values to be copied; must be the same type as the annotation being builtignorableElements
- the elements that can be safely dropped
-
copyRenameElementValuesFromAnnotation
public void copyRenameElementValuesFromAnnotation(AnnotationMirror valueHolder, Map<String, String> elementNameRenaming) Copies the specified element values from the given annotation, using the specified renaming map. Each value in the map must be an element name in the annotation being built. If an element from the given annotation is not a key in the map, it is ignored.- Parameters:
valueHolder
- the annotation that holds the values to be copiedelementNameRenaming
- a map from element names invalueHolder
to element names of the annotation being built
-
setValue
Set the element/field with the given name, to the given value. -
setValue
Set the element/field with the given name, to the given value.- Parameters:
elementName
- the element/field namevalues
- the new value for the element/field- Returns:
- this
-
setValue
public AnnotationBuilder setValue(ExecutableElement element, List<? extends @NonNull Object> values) Set the element to the given value.- Parameters:
element
- the elementvalues
- the new value for the element- Returns:
- this
-
setValue
Set the element/field with the given name, to the given value. -
setValue
Set the element/field with the given name, to the given value. -
setValue
Set the element/field with the given name, to the given value. -
setValue
Set the element/field with the given name, to the given value. -
setValue
Set the element/field with the given name, to the given value. -
setValue
Set the element/field with the given name, to the given value. -
setValue
Set the element/field with the given name, to the given value. -
setValue
Set the element/field with the given name, to the given value. -
setValue
Set the element/field with the given name, to the given value. -
removeElement
Remove the element/field with the given name. Does not err if no such element/field is present. -
setValue
-
setValue
-
setValue
-
setValue
-
setValue
-
setValue
-
findElement
-