View Javadoc

    1   /*
    2    * ClassCodeVisitorSupport.java created on 14.12.2005
    3    *
    4    */
    5   package org.codehaus.groovy.ast;
    6
    7   import java.util.Iterator;
    8   import java.util.List;
    9
    10  import org.codehaus.groovy.ast.expr.Expression;
    11  import org.codehaus.groovy.ast.stmt.AssertStatement;
    12  import org.codehaus.groovy.ast.stmt.BlockStatement;
    13  import org.codehaus.groovy.ast.stmt.BreakStatement;
    14  import org.codehaus.groovy.ast.stmt.CaseStatement;
    15  import org.codehaus.groovy.ast.stmt.CatchStatement;
    16  import org.codehaus.groovy.ast.stmt.ContinueStatement;
    17  import org.codehaus.groovy.ast.stmt.DoWhileStatement;
    18  import org.codehaus.groovy.ast.stmt.ExpressionStatement;
    19  import org.codehaus.groovy.ast.stmt.ForStatement;
    20  import org.codehaus.groovy.ast.stmt.IfStatement;
    21  import org.codehaus.groovy.ast.stmt.ReturnStatement;
    22  import org.codehaus.groovy.ast.stmt.Statement;
    23  import org.codehaus.groovy.ast.stmt.SwitchStatement;
    24  import org.codehaus.groovy.ast.stmt.SynchronizedStatement;
    25  import org.codehaus.groovy.ast.stmt.ThrowStatement;
    26  import org.codehaus.groovy.ast.stmt.TryCatchStatement;
    27  import org.codehaus.groovy.ast.stmt.WhileStatement;
    28  import org.codehaus.groovy.control.SourceUnit;
    29  import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
    30  import org.codehaus.groovy.syntax.SyntaxException;
    31
    32  public abstract class ClassCodeVisitorSupport extends CodeVisitorSupport implements GroovyClassVisitor {
    33
    34      public void visitClass(ClassNode node) {
    35          visitAnnotations(node);
    36          node.visitContents(this);
    37          List list = node.getObjectInitializerStatements();
    38          for (Iterator iter = list.iterator(); iter.hasNext();) {
    39              Statement element = (Statement) iter.next();
    40              element.visit(this);
    41          }
    42      }
    43
    44      public void visitAnnotations(AnnotatedNode node) {
    45
    46      }
    47
    48      protected void visitClassCodeContainer(Statement code) {
    49          if (code != null) code.visit(this);
    50      }
    51
    52      protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
    53          Statement code = node.getCode();
    54          visitClassCodeContainer(code);
    55      }
    56
    57      public void visitConstructor(ConstructorNode node) {
    58          visitConstructorOrMethod(node,true);
    59      }
    60
    61      public void visitMethod(MethodNode node) {
    62          visitConstructorOrMethod(node,false);
    63      }
    64
    65      public void visitField(FieldNode node) {
    66          Expression init = node.getInitialExpression();
    67          if (init != null) init.visit(this);
    68      }
    69
    70      public void visitProperty(PropertyNode node) {
    71          Statement statement = node.getGetterBlock();
    72          visitClassCodeContainer(statement);
    73
    74          statement = node.getSetterBlock();
    75          visitClassCodeContainer(statement);
    76
    77          Expression init = node.getInitialExpression();
    78          if (init != null) init.visit(this);
    79      }
    80
    81      protected void addError(String msg, ASTNode expr) {
    82          int line = expr.getLineNumber();
    83          int col = expr.getColumnNumber();
    84          SourceUnit source = getSourceUnit();
    85          source.getErrorCollector().addErrorAndContinue(
    86            new SyntaxErrorMessage(new SyntaxException(msg + '\n', line, col), source)
    87          );
    88      }
    89
    90      abstract protected SourceUnit getSourceUnit();
    91
    92      protected void visitStatement(Statement statement) {}
    93
    94      public void visitAssertStatement(AssertStatement statement) {
    95          visitStatement(statement);
    96          super.visitAssertStatement(statement);
    97      }
    98
    99      public void visitBlockStatement(BlockStatement block) {
    100         visitStatement(block);
    101         super.visitBlockStatement(block);
    102     }
    103
    104     public void visitBreakStatement(BreakStatement statement) {
    105         visitStatement(statement);
    106         super.visitBreakStatement(statement);
    107     }
    108
    109     public void visitCaseStatement(CaseStatement statement) {
    110         visitStatement(statement);
    111         super.visitCaseStatement(statement);
    112     }
    113
    114     public void visitCatchStatement(CatchStatement statement) {
    115         visitStatement(statement);
    116         super.visitCatchStatement(statement);
    117     }
    118
    119     public void visitContinueStatement(ContinueStatement statement) {
    120         visitStatement(statement);
    121         super.visitContinueStatement(statement);
    122     }
    123
    124     public void visitDoWhileLoop(DoWhileStatement loop) {
    125         visitStatement(loop);
    126         super.visitDoWhileLoop(loop);
    127     }
    128
    129     public void visitExpressionStatement(ExpressionStatement statement) {
    130         visitStatement(statement);
    131         super.visitExpressionStatement(statement);
    132     }
    133
    134     public void visitForLoop(ForStatement forLoop) {
    135         visitStatement(forLoop);
    136         super.visitForLoop(forLoop);
    137     }
    138
    139     public void visitIfElse(IfStatement ifElse) {
    140         visitStatement(ifElse);
    141         super.visitIfElse(ifElse);
    142     }
    143
    144     public void visitReturnStatement(ReturnStatement statement) {
    145         visitStatement(statement);
    146         super.visitReturnStatement(statement);
    147     }
    148
    149     public void visitSwitch(SwitchStatement statement) {
    150         visitStatement(statement);
    151         super.visitSwitch(statement);
    152     }
    153
    154     public void visitSynchronizedStatement(SynchronizedStatement statement) {
    155         visitStatement(statement);
    156         super.visitSynchronizedStatement(statement);
    157     }
    158
    159     public void visitThrowStatement(ThrowStatement statement) {
    160         visitStatement(statement);
    161         super.visitThrowStatement(statement);
    162     }
    163
    164     public void visitTryCatchFinally(TryCatchStatement statement) {
    165         visitStatement(statement);
    166         super.visitTryCatchFinally(statement);
    167     }
    168
    169     public void visitWhileLoop(WhileStatement loop) {
    170         visitStatement(loop);
    171         super.visitWhileLoop(loop);
    172     }
    173 }