Class AnnotationNode

java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotationNode
All Implemented Interfaces:
NodeMetaDataHandler

public class AnnotationNode extends ASTNode
Represents an annotation which can be attached to interfaces, classes, methods, fields, parameters, and other places.
  • Field Details

  • Constructor Details

    • AnnotationNode

      public AnnotationNode(ClassNode type)
      Creates an annotation use of the given annotation type. No member values are stored until addMember(String, Expression) or setMember(String, Expression) is called. Throws NullPointerException if type is null.
      Parameters:
      type - the annotation class; stored as given and not copied
  • Method Details

    • addMember

      public void addMember(String name, Expression value)
      Stores a member expression the first time name has no value. A missing member and a member set to null are both treated as unset. Members are kept in insertion order. setMember(String, Expression) overwrites a stored value instead of rejecting it. Throws GroovyBugError if a non-null value is already stored for name.
      Parameters:
      name - the annotation member name
      value - the member expression, stored without checking it against the annotation type
    • setMember

      public void setMember(String name, Expression value)
      Stores a member expression, replacing a value already recorded for the same name. The map preserves insertion order, so replacing a name does not move it.
      Parameters:
      name - the annotation member name
      value - the member expression to store
    • setAllowedTargets

      @Deprecated(since="6.0.0") public void setAllowedTargets(int ignored)
      Deprecated.
      since 6.0.0; targets are no longer stored on the annotation use
      Does nothing. Allowed targets are read from the annotation type's Target by isTargetAllowed(int).
      Parameters:
      ignored - unused bitmask of targets
    • setClassRetention

      @Deprecated(since="6.0.0") public void setClassRetention(boolean ignored)
      Deprecated.
      since 6.0.0; retention is no longer stored on the annotation use
      Does nothing. Class retention is derived from the annotation type's Retention by hasClassRetention().
      Parameters:
      ignored - unused retention flag
    • setSourceRetention

      @Deprecated(since="6.0.0") public void setSourceRetention(boolean ignored)
      Deprecated.
      since 6.0.0; retention is no longer stored on the annotation use
      Does nothing. Source retention is derived from the annotation type's Retention by hasSourceRetention().
      Parameters:
      ignored - unused retention flag
    • setRuntimeRetention

      @Deprecated(since="6.0.0") public void setRuntimeRetention(boolean ignored)
      Deprecated.
      since 6.0.0; retention is no longer stored on the annotation use
      Does nothing. Runtime retention is derived from the annotation type's Retention by hasRuntimeRetention().
      Parameters:
      ignored - unused retention flag
    • getClassNode

      public ClassNode getClassNode()
      Returns the annotation class passed to the constructor. This call does not resolve or redirect that class node.
      Returns:
      the annotation type
    • getMembers

      public Map<String,Expression> getMembers()
      Returns the member expressions in insertion order. Before any member is stored the result is an immutable empty map, and later additions are not visible through it. Once a member has been stored the result is the live map.
      Returns:
      the member map, never null
    • getMember

      public Expression getMember(String name)
      Returns the expression stored for one member.
      Parameters:
      name - the member name
      Returns:
      the member expression, or null when that member has not been set
    • isBuiltIn

      @Deprecated(since="6.0.0") public boolean isBuiltIn()
      Deprecated.
      since 6.0.0
      Always reports that the annotation is not built in. Annotation nodes no longer distinguish built-in annotations.
      Returns:
      false
    • isTargetAllowed

      public boolean isTargetAllowed(int target)
      Reports whether every bit set in target is permitted for this annotation type.

      The annotation class must already be a primary class node or resolved. The first check decodes Target from that class and caches the mask on the resolved class. Only a value that is a ListExpression is decoded. A PropertyExpression in that list contributes the bit for the ElementType it names; any other entry is skipped. If no Target annotation has such a list, the mask is the JLS 9.6.4.1 default: declaration contexts other than type parameters, and not type-use. ElementType.TYPE and ElementType.MODULE both contribute TYPE_TARGET. An ElementType constant with no mapped bit fails with GroovyBugError. A property name that is not an ElementType constant aborts the lookup. ExtendedTarget is then OR-ed in. ExtendedElementType.IMPORT contributes IMPORT_TARGET and ExtendedElementType.LOOP contributes STATEMENT_TARGET; an unknown extended element contributes nothing. A requested combination is allowed only when all of its bits are in the mask. A target of zero is always allowed. Throws IllegalStateException if the annotation class is neither primary nor resolved.

      Parameters:
      target - bitmask of element kinds, usually one *_TARGET constant
      Returns:
      true when every bit set in target is allowed
    • hasRuntimeRetention

      public boolean hasRuntimeRetention()
      Flag corresponding to RetentionPolicy.RUNTIME.
      Returns:
      true if the annotation should be visible at runtime; false otherwise
    • hasSourceRetention

      public boolean hasSourceRetention()
      Flag corresponding to RetentionPolicy.SOURCE.
      Returns:
      true if the annotation is only allowed in sources; false otherwise
    • hasClassRetention

      public boolean hasClassRetention()
      Flag corresponding to RetentionPolicy.CLASS. This is the default when no Retention annotation is present.
      Returns:
      true if the annotation is written in the bytecode but not visible at runtime; false otherwise
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getText

      public String getText()
      Description copied from class: ASTNode
      Returns a human-readable text representation of this AST node. Used for debugging and error messages. Default implementation returns a message indicating the representation is not yet implemented for this node type.
      Overrides:
      getText in class ASTNode
      Returns:
      text representation of this node, or placeholder for unimplemented types
    • targetToName

      public static String targetToName(int target)
      Names one target bit for error messages. Any value with the low bit set is TYPE, which is how TYPE_TARGET is told apart from ANNOTATION_TARGET. Every other value must equal exactly one *_TARGET constant; a mix of bits is not split apart.
      Parameters:
      target - a target bitmask, normally one constant
      Returns:
      the element-kind name, or unknown target when nothing matches