Class AnnotationVisitor

java.lang.Object
org.codehaus.groovy.classgen.AnnotationVisitor

public class AnnotationVisitor extends Object
An Annotation visitor responsible for:
  • reading annotation metadata (@Retention, @Target, attribute types)
  • verify that an AnnotationNode conforms to annotation meta
  • enhancing an AnnotationNode AST to reflect real annotation meta
  • Constructor Details

    • AnnotationVisitor

      public AnnotationVisitor(SourceUnit source, ErrorCollector errorCollector)
      Creates a new annotation visitor.
      Parameters:
      source - the source unit being compiled
      errorCollector - the error collector for reporting validation errors
  • Method Details

    • setReportClass

      public void setReportClass(ClassNode node)
      Sets the class to report errors against.
      Parameters:
      node - the class node for error reporting
    • visit

      public AnnotationNode visit(AnnotationNode node)
      Visits and validates an annotation node, checking that it conforms to annotation metadata and enhancing the AST to reflect real annotation semantics.
      Parameters:
      node - the annotation node to visit and validate
      Returns:
      the validated and potentially modified annotation node
    • visitExpression

      protected void visitExpression(String attrName, Expression valueExpr, ClassNode attrType)
      Validates an annotation attribute value against its declared type.
      Parameters:
      attrName - the attribute name
      valueExpr - the supplied value expression
      attrType - the declared attribute type
    • checkReturnType

      public void checkReturnType(ClassNode attrType, ASTNode node)
      Verifies that an annotation member return type is valid under Java annotation rules.
      Parameters:
      attrType - the declared return type
      node - the node to report against on error
    • visitListExpression

      protected void visitListExpression(String attrName, ListExpression listExpr, ClassNode elementType)
      Validates each element of an array-valued annotation attribute.
      Parameters:
      attrName - the attribute name
      listExpr - the list expression representing the attribute value
      elementType - the declared component type
    • visitEnumExpression

      protected void visitEnumExpression(String attrName, PropertyExpression valueExpr, ClassNode attrType)
      Validates an enum-valued annotation attribute.
      Parameters:
      attrName - the attribute name
      valueExpr - the enum constant expression
      attrType - the declared enum type
    • visitConstantExpression

      protected void visitConstantExpression(String attrName, ConstantExpression valueExpr, ClassNode attrType)
      Validates a constant-valued annotation attribute.
      Parameters:
      attrName - the attribute name
      valueExpr - the constant expression
      attrType - the declared attribute type
    • visitAnnotationExpression

      protected void visitAnnotationExpression(String attrName, AnnotationConstantExpression valueExpr, ClassNode attrType)
      Validates a nested annotation attribute.
      Parameters:
      attrName - the attribute name
      valueExpr - the nested annotation expression
      attrType - the declared annotation type
    • addError

      protected void addError(String msg)
      Reports an error against the current annotation.
      Parameters:
      msg - the error message
    • addError

      protected void addError(String msg, ASTNode node)
      Reports an error against the supplied AST node.
      Parameters:
      msg - the error message
      node - the node to associate with the error
    • checkCircularReference

      public void checkCircularReference(ClassNode searchClass, ClassNode attrType, Expression startExp)
      Checks for circular references in nested annotations, which would cause infinite recursion.
      Parameters:
      searchClass - the annotation class being searched for in the dependency chain
      attrType - the attribute type to check
      startExp - the expression where the check started, for error reporting