Class AbstractInterruptibleASTTransformation

java.lang.Object
org.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.ast.ClassCodeVisitorSupport
org.codehaus.groovy.transform.AbstractInterruptibleASTTransformation
All Implemented Interfaces:
GroovyClassVisitor, GroovyCodeVisitor, ASTTransformation, ErrorCollecting, org.objectweb.asm.Opcodes

public abstract class AbstractInterruptibleASTTransformation extends ClassCodeVisitorSupport implements ASTTransformation, org.objectweb.asm.Opcodes
Base class for AST Transformations which will automatically throw an InterruptedException when some conditions are met.
Since:
1.8.0
  • Field Details

    • CHECK_METHOD_START_MEMBER

      protected static final String CHECK_METHOD_START_MEMBER
      Name of the annotation member that controls a check at method entry.
      See Also:
    • THROWN_EXCEPTION_TYPE

      protected static final String THROWN_EXCEPTION_TYPE
      Name of the annotation member that names the exception to throw.
      See Also:
    • source

      protected SourceUnit source
      Source unit being transformed. Set at the start of each visit(org.codehaus.groovy.ast.ASTNode[], org.codehaus.groovy.control.SourceUnit).
    • checkOnMethodStart

      protected boolean checkOnMethodStart
      Whether to wrap a method body with the interrupt check. Taken from the annotation, and true when the member is omitted. Subclasses consult this from their method visitor.
    • applyToAllClasses

      protected boolean applyToAllClasses
      Whether every class in the source unit is visited. True only when the annotation's applyToAllMembers and applyToAllClasses members are both true. When false, only the annotated class, or the script class, is visited.
    • applyToAllMembers

      protected boolean applyToAllMembers
      Whether every member is visited. Taken from the annotation, and true when the member is omitted. When false, visit(org.codehaus.groovy.ast.ASTNode[], org.codehaus.groovy.control.SourceUnit) limits itself to the annotated method, field, or declaration and forces applyToAllClasses false.
    • thrownExceptionType

      protected ClassNode thrownExceptionType
      Exception constructed by the interrupt check. Taken from the annotation's thrown member, or InterruptedException when that member is omitted.
  • Constructor Details

    • AbstractInterruptibleASTTransformation

      public AbstractInterruptibleASTTransformation()
  • Method Details

    • getSourceUnit

      protected SourceUnit getSourceUnit()
      Description copied from class: ClassCodeVisitorSupport
      Provides access to the SourceUnit for error reporting during visitation. Implementations must override this method.
      Specified by:
      getSourceUnit in class ClassCodeVisitorSupport
      Returns:
      the source unit for this visitor
    • type

      protected abstract ClassNode type()
      Returns the annotation type this transformation accepts. visit(org.codehaus.groovy.ast.ASTNode[], org.codehaus.groovy.control.SourceUnit) rejects a call whose annotation is a different type.
      Returns:
      the annotation class node this transform handles
    • createCondition

      protected abstract Expression createCondition()
      Subclasses should implement this method to set the condition of the interruption statement
    • getErrorMessage

      protected abstract String getErrorMessage()
      Subclasses should implement this method to provide good error resolution.
    • setupTransform

      protected void setupTransform(AnnotationNode node)
      Copies checkOnMethodStart, applyToAllMembers, applyToAllClasses, and thrown from node. Omitted members default to true, true, true, and InterruptedException. applyToAllClasses is forced false when applyToAllMembers is false.
      Parameters:
      node - annotation that triggered this transformation
    • visit

      public void visit(ASTNode[] nodes, SourceUnit source)
      Description copied from interface: ASTTransformation
      The method is invoked when an AST Transformation is active. For local transformations, it is invoked once each time the local annotation is encountered. For global transformations, it is invoked once for every source unit, which is typically a source file.
      Specified by:
      visit in interface ASTTransformation
      Parameters:
      nodes - The ASTnodes when the call was triggered. Element 0 is the AnnotationNode that triggered this annotation to be activated. Element 1 is the AnnotatedNode decorated, such as a MethodNode or ClassNode. For global transformations it is usually safe to ignore this parameter.
      source - The source unit being compiled. The source unit may contain several classes. For global transformations, information about the AST can be retrieved from this object.
    • getBooleanAnnotationParameter

      protected static boolean getBooleanAnnotationParameter(AnnotationNode node, String parameterName, boolean defaultValue)
      Reads a boolean annotation member. A missing member yields defaultValue. A constant is converted with Groovy's as Boolean coercion.

      Fails with GroovyBugError when the member is present but is not a constant, or cannot be coerced to boolean.

      Parameters:
      node - annotation to read
      parameterName - member name
      defaultValue - used when the member is absent
      Returns:
      the member as a boolean, or defaultValue
    • getClassAnnotationParameter

      protected static ClassNode getClassAnnotationParameter(AnnotationNode node, String parameterName, ClassNode defaultValue)
      Reads a class annotation member. A missing member yields defaultValue. A class expression yields its type.

      Fails with GroovyBugError when the member is present but is not a class expression.

      Parameters:
      node - annotation to read
      parameterName - member name
      defaultValue - used when the member is absent
      Returns:
      the member's class node, or defaultValue
    • internalError

      protected static void internalError(String message)
      Throws a bug error prefixed with Internal error:.

      Fails with GroovyBugError always.

      Parameters:
      message - detail appended to that prefix
    • createInterruptStatement

      protected Statement createInterruptStatement()
      Returns:
      Returns the interruption check statement.
    • wrapBlock

      protected final Statement wrapBlock(Statement statement)
      Takes a statement and wraps it into a block statement which first element is the interruption check statement.
      Parameters:
      statement - the statement to be wrapped
      Returns:
      a block statement which first element is for checking interruption, and the second one the statement to be wrapped.
    • visitForLoop

      public final void visitForLoop(ForStatement forStatement)
      Description copied from class: ClassCodeVisitorSupport
      Visits a ForStatement, invoking statement hooks and processing loop variable annotations.
      Specified by:
      visitForLoop in interface GroovyCodeVisitor
      Overrides:
      visitForLoop in class ClassCodeVisitorSupport
      Parameters:
      forStatement - the for statement to visit
    • visitDoWhileLoop

      public final void visitDoWhileLoop(DoWhileStatement doWhileStatement)
      Description copied from class: ClassCodeVisitorSupport
      Visits a DoWhileStatement, invoking statement hooks before parent traversal.
      Specified by:
      visitDoWhileLoop in interface GroovyCodeVisitor
      Overrides:
      visitDoWhileLoop in class ClassCodeVisitorSupport
      Parameters:
      doWhileStatement - the do-while statement to visit
    • visitWhileLoop

      public final void visitWhileLoop(WhileStatement whileStatement)
      Description copied from class: ClassCodeVisitorSupport
      Visits a WhileStatement, invoking statement hooks before parent traversal.
      Specified by:
      visitWhileLoop in interface GroovyCodeVisitor
      Overrides:
      visitWhileLoop in class ClassCodeVisitorSupport
      Parameters:
      whileStatement - the while statement to visit