Class AnnotationBuilder
Constructing an AnnotationMirror requires:
 
- Constructing the builder with the desired annotation class
- Setting each value individually using setValuemethods
- 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 SummaryConstructorsConstructorDescriptionCreate 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 SummaryModifier and TypeMethodDescriptionbuild()voidcopyElementValuesFromAnnotation(AnnotationMirror other, String... ignorableElements) Copies every element value from the given annotation.voidcopyElementValuesFromAnnotation(AnnotationMirror valueHolder, Collection<ExecutableElement> ignorableElements) Copies every element value from the given annotation.voidcopyRenameElementValuesFromAnnotation(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 AnnotationMirrorfromClass(Elements elements, Class<? extends Annotation> aClass) Creates anAnnotationMirrorthat uses default values for elements/fields.static AnnotationMirrorfromClass(Elements elements, Class<? extends Annotation> aClass, Map<String, AnnotationValue> elementNamesValues) Creates anAnnotationMirrorgiven by a particular annotation class and a name-to-value mapping for the elements/fields.static @Nullable AnnotationMirrorfromName(Elements elements, @FullyQualifiedName CharSequence name) Creates anAnnotationMirrorgiven by a particular fully-qualified name.static @Nullable AnnotationMirrorfromName(Elements elements, @FullyQualifiedName CharSequence name, Map<String, AnnotationValue> elementNamesValues) Creates anAnnotationMirrorgiven 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- 
AnnotationBuilderCreate a new AnnotationBuilder for the given annotation and environment (with no elements/fields, but they can be added later).- Parameters:
- env- the processing environment
- anno- the class of the annotation to build
 
- 
AnnotationBuilderCreate a new AnnotationBuilder for the given annotation name (with no elements/fields, but they can be added later).- Parameters:
- env- the processing environment
- name- the canonical name of the annotation to build
 
- 
AnnotationBuilderCreate a new AnnotationBuilder that copies the given annotation, including its elements/fields.- Parameters:
- env- the processing environment
- annotation- the annotation to copy
 
 
- 
- 
Method Details- 
getAnnotationEltReturns the type element of the annotation that is being built.- Returns:
- the type element of the annotation that is being built
 
- 
elementNamesValuespublic 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 initialize
- elementValue- the initial value for the element/field
- Returns:
- a mappnig from the element name to the element value
 
- 
fromClassCreates anAnnotationMirrorthat 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 use
- aClass- the annotation class
- Returns:
- an AnnotationMirrorof the given type
- Throws:
- UserError- if the annotation corresponding to the class could not be loaded
 
- 
fromClasspublic static AnnotationMirror fromClass(Elements elements, Class<? extends Annotation> aClass, Map<String, AnnotationValue> elementNamesValues) Creates anAnnotationMirrorgiven 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 use
- aClass- the annotation class
- elementNamesValues- the values for the annotation's elements/fields
- Returns:
- an AnnotationMirrorof the given type
 
- 
fromNamepublic static @Nullable AnnotationMirror fromName(Elements elements, @FullyQualifiedName CharSequence name) Creates anAnnotationMirrorgiven 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 use
- name- the name of the annotation to create
- Returns:
- an AnnotationMirrorof type name or null if the annotation couldn't be loaded
 
- 
fromNamepublic static @Nullable AnnotationMirror fromName(Elements elements, @FullyQualifiedName CharSequence name, Map<String, AnnotationValue> elementNamesValues) Creates anAnnotationMirrorgiven by a particular fully-qualified name and element/field values. If any element is not specified by theelementValuesargument, 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 use
- name- the name of the annotation to create
- elementNamesValues- the values for the annotation's elements/fields
- Returns:
- an AnnotationMirrorof type name or null if the annotation couldn't be loaded
 
- 
build
- 
copyElementValuesFromAnnotationCopies 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 built
- ignorableElements- the names of elements of- otherthat can be safely dropped
 
- 
copyElementValuesFromAnnotationpublic 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 built
- ignorableElements- the elements that can be safely dropped
 
- 
copyRenameElementValuesFromAnnotationpublic 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 copied
- elementNameRenaming- a map from element names in- valueHolderto element names of the annotation being built
 
- 
setValueSet the element/field with the given name, to the given value.
- 
setValueSet the element/field with the given name, to the given value.- Parameters:
- elementName- the element/field name
- values- the new value for the element/field
- Returns:
- this
 
- 
setValuepublic AnnotationBuilder setValue(ExecutableElement element, List<? extends @NonNull Object> values) Set the element to the given value.- Parameters:
- element- the element
- values- the new value for the element
- Returns:
- this
 
- 
setValueSet the element/field with the given name, to the given value.
- 
setValueSet the element/field with the given name, to the given value.
- 
setValueSet the element/field with the given name, to the given value.
- 
setValueSet the element/field with the given name, to the given value.
- 
setValueSet the element/field with the given name, to the given value.
- 
setValueSet the element/field with the given name, to the given value.
- 
setValueSet the element/field with the given name, to the given value.
- 
setValueSet the element/field with the given name, to the given value.
- 
setValueSet the element/field with the given name, to the given value.
- 
removeElementRemove the element/field with the given name. Does not err if no such element/field is present.
- 
setValue
- 
setValue
- 
setValue
- 
setValue
- 
setValue
- 
setValue
- 
findElement
 
-