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,
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.
|
ExecutableElement |
findElement(CharSequence key) |
static AnnotationMirror |
fromClass(Elements elements,
Class<? extends Annotation> aClass)
Creates an
AnnotationMirror given by a particular annotation class. |
static @Nullable AnnotationMirror |
fromName(Elements elements,
CharSequence name)
Creates an
AnnotationMirror given by a particular fully-qualified name. |
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, CharSequence name)
env
- the processing environmentname
- the fully-qualified name of the annotation to buildpublic AnnotationBuilder(ProcessingEnvironment env, AnnotationMirror annotation)
env
- the processing environmentannotation
- the annotation to copypublic static AnnotationMirror fromClass(Elements elements, Class<? extends Annotation> aClass)
AnnotationMirror
given by a particular annotation class. getElementValues
on the result returns an empty map. This may be in conflict with the annotation's definition,
which might contain elements (annotation fields). Use an AnnotationBuilder for annotations
that contain elements.
This method raises an user error if the annotation corresponding to the class could not be loaded.
Clients can use fromName(javax.lang.model.util.Elements, java.lang.CharSequence)
and check the result for null manually, if the error
from this method is not desired. 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 @Nullable AnnotationMirror fromName(Elements elements, CharSequence name)
AnnotationMirror
given by a particular fully-qualified name.
getElementValues on the result returns an empty map. This may be in conflict with the
annotation's definition, which might contain elements (annotation fields). Use an
AnnotationBuilder for annotations that contain elements.
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 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)