public class AnnotationBuilder extends Object
Constructing an AnnotationMirror
requires:
setValue
methods
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 and Description |
---|
AnnotationBuilder(ProcessingEnvironment env,
AnnotationMirror annotation)
Create a new AnnotationBuilder that copies the given annotation, including its
elements/fields.
|
AnnotationBuilder(ProcessingEnvironment env,
@FullyQualifiedName CharSequence name)
Create 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).
|
Modifier and Type | Method and Description |
---|---|
AnnotationMirror |
build() |
void |
copyElementValuesFromAnnotation(AnnotationMirror valueHolder,
String... 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.
|
ExecutableElement |
findElement(CharSequence key) |
static AnnotationMirror |
fromClass(Elements elements,
Class<? extends Annotation> aClass)
Creates an
AnnotationMirror that uses default values for elements/fields. |
static AnnotationMirror |
fromClass(Elements elements,
Class<? extends Annotation> aClass,
Map<String,AnnotationValue> elementNamesValues)
Creates an
AnnotationMirror 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 an
AnnotationMirror given by a particular fully-qualified name. |
static @Nullable AnnotationMirror |
fromName(Elements elements,
@FullyQualifiedName CharSequence name,
Map<String,AnnotationValue> elementNamesValues)
Creates an
AnnotationMirror given by a particular fully-qualified name and
element/field values. |
TypeElement |
getAnnotationElt()
Returns the type element of the annotation that is being built.
|
AnnotationBuilder |
removeElement(CharSequence elementName)
Remove the element/field with the given name.
|
AnnotationBuilder |
setValue(CharSequence elementName,
AnnotationMirror value)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
Boolean value)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
Character value)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
Class<?> value) |
AnnotationBuilder |
setValue(CharSequence elementName,
Double value)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
Enum<?> value) |
AnnotationBuilder |
setValue(CharSequence elementName,
Enum<?>[] values) |
AnnotationBuilder |
setValue(CharSequence elementName,
Float value)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
Integer value)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
List<? extends Object> values)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
Long value)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
Object[] values)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
Short value)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
String value)
Set the element/field with the given name, to the given value.
|
AnnotationBuilder |
setValue(CharSequence elementName,
TypeMirror value) |
AnnotationBuilder |
setValue(CharSequence elementName,
VariableElement value) |
AnnotationBuilder |
setValue(CharSequence elementName,
VariableElement[] values) |
public AnnotationBuilder(ProcessingEnvironment env, Class<? extends Annotation> anno)
env
- the processing environmentanno
- the class of the annotation to buildpublic AnnotationBuilder(ProcessingEnvironment env, @FullyQualifiedName CharSequence name)
env
- the processing environmentname
- the canonical name of the annotation to buildpublic AnnotationBuilder(ProcessingEnvironment env, AnnotationMirror annotation)
env
- the processing environmentannotation
- the annotation to copypublic TypeElement getAnnotationElt()
public static Map<String,AnnotationValue> elementNamesValues(String elementName, Object elementValue)
elementName
- the name of an element/field to initializeelementValue
- the initial value for the element/fieldpublic static AnnotationMirror fromClass(Elements elements, Class<? extends Annotation> aClass)
AnnotationMirror
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.
elements
- the element utilities to useaClass
- the annotation classAnnotationMirror
of the given typepublic static AnnotationMirror fromClass(Elements elements, Class<? extends Annotation> aClass, Map<String,AnnotationValue> elementNamesValues)
AnnotationMirror
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.
elements
- the element utilities to useaClass
- the annotation classelementNamesValues
- the values for the annotation's elements/fieldsAnnotationMirror
of the given typepublic static @Nullable AnnotationMirror fromName(Elements elements, @FullyQualifiedName CharSequence name)
AnnotationMirror
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.
elements
- the element utilities to usename
- the name of the annotation to createAnnotationMirror
of type name or null if the annotation couldn't
be loadedpublic static @Nullable AnnotationMirror fromName(Elements elements, @FullyQualifiedName CharSequence name, Map<String,AnnotationValue> elementNamesValues)
AnnotationMirror
given by a particular fully-qualified name and
element/field values. If any element is not specified by the elementValues
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.
elements
- the element utilities to usename
- the name of the annotation to createelementNamesValues
- the values for the annotation's elements/fieldsAnnotationMirror
of type name or null if the annotation couldn't
be loadedpublic AnnotationMirror build()
public void copyElementValuesFromAnnotation(AnnotationMirror valueHolder, String... ignorableElements)
ignorableElements
.valueHolder
- the annotation that holds the values to be copiedignorableElements
- the elements that can be safely droppedpublic void copyRenameElementValuesFromAnnotation(AnnotationMirror valueHolder, Map<String,String> elementNameRenaming)
valueHolder
- the annotation that holds the values to be copiedelementNameRenaming
- a map from element names in valueHolder
to element names
of the annotation being builtpublic AnnotationBuilder setValue(CharSequence elementName, AnnotationMirror value)
public AnnotationBuilder setValue(CharSequence elementName, List<? extends Object> values)
public AnnotationBuilder setValue(CharSequence elementName, Object[] values)
public AnnotationBuilder setValue(CharSequence elementName, Boolean value)
public AnnotationBuilder setValue(CharSequence elementName, Character value)
public AnnotationBuilder setValue(CharSequence elementName, Double value)
public AnnotationBuilder setValue(CharSequence elementName, Float value)
public AnnotationBuilder setValue(CharSequence elementName, Integer value)
public AnnotationBuilder setValue(CharSequence elementName, Long value)
public AnnotationBuilder setValue(CharSequence elementName, Short value)
public AnnotationBuilder setValue(CharSequence elementName, String value)
public AnnotationBuilder removeElement(CharSequence elementName)
public AnnotationBuilder setValue(CharSequence elementName, TypeMirror value)
public AnnotationBuilder setValue(CharSequence elementName, Class<?> value)
public AnnotationBuilder setValue(CharSequence elementName, Enum<?> value)
public AnnotationBuilder setValue(CharSequence elementName, VariableElement value)
public AnnotationBuilder setValue(CharSequence elementName, Enum<?>[] values)
public AnnotationBuilder setValue(CharSequence elementName, VariableElement[] values)
public ExecutableElement findElement(CharSequence key)