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

public interface CtAnnotation<A extends Annotation> extends CtExpression<A>, CtShadowable
This element represents an annotation on an element.
     // statement annotated by annotation @SuppressWarnings
     @SuppressWarnings("unchecked")
     java.util.List<?> x = new java.util.ArrayList<>()
 
  • 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

      <T extends CtExpression> T getValue(String key)
      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, see getValueAsObject(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, see getWrappedValue(String).
      Parameters:
      key - Name of searched value.
      Returns:
      the value expression or null if not found.
    • getValueAsObject

      Object getValueAsObject(String key)
      Returns the actual value of an annotation property
    • getValueAsInt

      int getValueAsInt(String key)
      Returns the actual value of an annotation property, as an integer (utility method)
    • getValueAsString

      String getValueAsString(String key)
      Returns the actual value of an annotation property, as a String (utility method)
    • getWrappedValue

      <T extends CtExpression> T getWrappedValue(String key)
      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 of getValues(), plus the default ones defined in the annotation type.
    • setAnnotationType

      <T extends CtAnnotation<A>> T setAnnotationType(CtTypeReference<? extends Annotation> type)
      Sets the annotation's type.
      Parameters:
      type - reference to the type of this annotation
    • setElementValues

      <T extends CtAnnotation<A>> T setElementValues(Map<String,​Object> values)
      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 as CtTypeReference.
    • setValues

      <T extends CtAnnotation<A>> T setValues(Map<String,​CtExpression> values)
      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

      <T extends CtAnnotation<A>> T addValue(String elementName, Object value)
      Adds a new key-value pair for this annotation
    • addValue

      <T extends CtAnnotation<A>> T addValue(String elementName, CtLiteral<?> value)
      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

      <T extends CtAnnotation<A>> T addValue(String elementName, CtFieldAccess<?> value)
      Adds a new key-field access pair for this annotation.
    • addValue

      <T extends CtAnnotation<A>> T addValue(String elementName, CtAnnotation<?> value)
      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 methods Refactoring.copyType(CtType) and Refactoring.copyMethod(CtMethod) instead which does additional work beyond cloning.
      Specified by:
      clone in interface CtCodeElement
      Specified by:
      clone in interface CtElement
      Specified by:
      clone in interface CtExpression<A extends Annotation>
    • setTypeCasts

      <C extends CtExpression<A>> C setTypeCasts(List<CtTypeReference<?>> types)
      Description copied from interface: CtExpression
      Sets the type casts.
      Specified by:
      setTypeCasts in interface CtExpression<A extends Annotation>
    • getAnnotatedElementTypeForCtElement

      static CtAnnotatedElementType getAnnotatedElementTypeForCtElement(CtElement element)
    • getName

      String getName()
      Returns the class name of the annotation, eg "Deprecated" for @Deprecated.
      Returns:
      the name of the annotation