Package spoon.reflect.declaration
Interface CtAnnotation<A extends Annotation>
- Type Parameters:
A
- type of represented annotation
- All Superinterfaces:
Cloneable
,CtCodeElement
,CtElement
,CtExpression<A>
,CtQueryable
,CtShadowable
,CtTypedElement<A>
,CtVisitable
,FactoryAccessor
,Serializable
,SourcePositionHolder
,TemplateParameter<A>
- All Known Implementing Classes:
CtAnnotationImpl
This element represents an annotation on an element.
// statement annotated by annotation @SuppressWarnings @SuppressWarnings("unchecked") java.util.List<?> x = new java.util.ArrayList<>()
-
Method Summary
Modifier and TypeMethodDescription<T extends CtAnnotation<A>>
TAdds a new key-value pair for this annotation<T extends CtAnnotation<A>>
TaddValue(String elementName, CtFieldAccess<?> value)
Adds a new key-field access pair for this annotation.<T extends CtAnnotation<A>>
TAdds a new key-literal pair for this annotation.<T extends CtAnnotation<A>>
TaddValue(String elementName, CtNewArray<? extends CtExpression> value)
Adds a new key-array pair for this annotation.<T extends CtAnnotation<A>>
TaddValue(String elementName, CtAnnotation<?> value)
Adds a new key-annotation pair for this annotation.clone()
Clone the element which calls this method in a new object.Returns the actual annotation (a dynamic proxy for this element).Get all values ofgetValues()
, plus the default ones defined in the annotation type.Returns the element which is annotated by this annotation.Returns the type of the element which is annotated by this annotation.static CtAnnotatedElementType
getAnnotatedElementTypeForCtElement(CtElement element)
Returns the annotation type of this annotation.getName()
Returns the class name of the annotation, eg "Deprecated" for @Deprecated.<T extends CtExpression>
TGets a value, as a CtExpression, for a given key without any conversion.int
getValueAsInt(String key)
Returns the actual value of an annotation property, as an integer (utility method)getValueAsObject(String key)
Returns the actual value of an annotation propertygetValueAsString(String key)
Returns the actual value of an annotation property, as a String (utility method)Returns this annotation's elements and their values.<T extends CtExpression>
TgetWrappedValue(String key)
Gets a value for a given key and try to fix its type based on the annotation type.<T extends CtAnnotation<A>>
TsetAnnotationType(CtTypeReference<? extends Annotation> type)
Sets the annotation's type.<T extends CtAnnotation<A>>
TsetElementValues(Map<String,Object> values)
Set's this annotation's element names and their values.<C extends CtExpression<A>>
CsetTypeCasts(List<CtTypeReference<?>> types)
Sets the type casts.<T extends CtAnnotation<A>>
TsetValues(Map<String,CtExpression> values)
Set's this annotation's element names and their values.Methods inherited from interface spoon.reflect.code.CtCodeElement
partiallyEvaluate
Methods inherited from interface spoon.reflect.declaration.CtElement
addAnnotation, addComment, asIterable, delete, descendantIterator, getAllMetadata, getAnnotatedChildren, getAnnotation, getAnnotation, getAnnotations, getComments, getDirectChildren, getDocComment, getElements, getMetadata, getMetadataKeys, getOriginalSourceFragment, getParent, getParent, getParent, getPath, getPosition, getReferencedTypes, getRoleInParent, getShortRepresentation, getValueByRole, hasAnnotation, hasParent, isImplicit, isParentInitialized, prettyprint, putMetadata, removeAnnotation, removeComment, replace, replace, setAllMetadata, setAnnotations, setComments, setDocComment, setImplicit, setParent, setPosition, setPositions, setValueByRole, toString, toStringDebug, updateAllParentsBelow
Methods inherited from interface spoon.reflect.code.CtExpression
addTypeCast, getTypeCasts
Methods inherited from interface spoon.reflect.visitor.chain.CtQueryable
filterChildren, map, map
Methods inherited from interface spoon.reflect.declaration.CtShadowable
isShadow, setShadow
Methods inherited from interface spoon.reflect.declaration.CtTypedElement
getType, setType
Methods inherited from interface spoon.reflect.visitor.CtVisitable
accept
Methods inherited from interface spoon.processing.FactoryAccessor
getFactory, setFactory
Methods inherited from interface spoon.template.TemplateParameter
S
-
Method Details
-
getActualAnnotation
A getActualAnnotation()Returns the actual annotation (a dynamic proxy for this element).NOTE: before using an annotation proxy, you have to make sure that all the types referenced by the annotation have been compiled and are in the classpath so that accessed values can be converted into the actual types.
-
getAnnotationType
CtTypeReference<A> getAnnotationType()Returns the annotation type of this annotation.- Returns:
- a reference to the type of this annotation
-
getValue
Gets a value, as a CtExpression, for a given key without any conversion. If you need the actual value (eg an integer and not a literal, seegetValueAsObject(String)
and similar methods. Note that this value type does not necessarily corresponds to the annotation type member. For example, in case the annotation type expect an array of Object, and a single value is given, Spoon will return only the object without the CtNewArray. If you want to get a type closer to the annotation type one, seegetWrappedValue(String)
.- Parameters:
key
- Name of searched value.- Returns:
- the value expression or null if not found.
-
getValueAsObject
Returns the actual value of an annotation property -
getValueAsInt
Returns the actual value of an annotation property, as an integer (utility method) -
getValueAsString
Returns the actual value of an annotation property, as a String (utility method) -
getWrappedValue
Gets a value for a given key and try to fix its type based on the annotation type. For example, if the annotation type member expects an array of String, and it can be resolved, this method will return a CtNewArray instead of a CtLiteral. Warning: the returned element might be detached from the model- Parameters:
key
- Name of searched value.- Returns:
- the value expression or null if not found.
-
getValues
Map<String,CtExpression> getValues()Returns this annotation's elements and their values. This is returned in the form of a map that associates element names with their corresponding values. If you iterate over the map with entrySet(), the iteration order complies with the order of annotation values in the source code.- Returns:
- this annotation's element names and their values, or an empty map if there are none
-
getAllValues
Map<String,CtExpression> getAllValues()Get all values ofgetValues()
, plus the default ones defined in the annotation type. -
setAnnotationType
Sets the annotation's type.- Parameters:
type
- reference to the type of this annotation
-
setElementValues
Set's this annotation's element names and their values. This is in the form of a map that associates element names with their corresponding values. Note that type values are stored asCtTypeReference
. -
setValues
Set's this annotation's element names and their values. This is in the form of a map that associates element names with their corresponding values. -
getAnnotatedElement
CtElement getAnnotatedElement()Returns the element which is annotated by this annotation.- Returns:
- annotated
CtElement
-
getAnnotatedElementType
CtAnnotatedElementType getAnnotatedElementType()Returns the type of the element which is annotated by this annotation.- Returns:
CtAnnotatedElementType
-
addValue
Adds a new key-value pair for this annotation -
addValue
Adds a new key-literal pair for this annotation. -
addValue
<T extends CtAnnotation<A>> T addValue(String elementName, CtNewArray<? extends CtExpression> value)Adds a new key-array pair for this annotation. -
addValue
Adds a new key-field access pair for this annotation. -
addValue
Adds a new key-annotation pair for this annotation. -
clone
CtAnnotation<A> clone()Description copied from interface:CtElement
Clone the element which calls this method in a new object. Note that that references are kept as is, and thus, so if you clone whole classes or methods, some parts of the cloned element (eg executable references) may still point to the initial element. In this case, consider using methodsRefactoring.copyType(CtType)
andRefactoring.copyMethod(CtMethod)
instead which does additional work beyond cloning.- Specified by:
clone
in interfaceCtCodeElement
- Specified by:
clone
in interfaceCtElement
- Specified by:
clone
in interfaceCtExpression<A extends Annotation>
-
setTypeCasts
Description copied from interface:CtExpression
Sets the type casts.- Specified by:
setTypeCasts
in interfaceCtExpression<A extends Annotation>
-
getAnnotatedElementTypeForCtElement
-
getName
String getName()Returns the class name of the annotation, eg "Deprecated" for @Deprecated.- Returns:
- the name of the annotation
-