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          visitAnnotations(node);
    67          Expression init = node.getInitialExpression();
    68          if (init != null) init.visit(this);
    69      }
    70
    71      public void visitProperty(PropertyNode node) {
    72          Statement statement = node.getGetterBlock();
    73          visitClassCodeContainer(statement);
    74
    75          statement = node.getSetterBlock();
    76          visitClassCodeContainer(statement);
    77
    78          Expression init = node.getInitialExpression();
    79          if (init != null) init.visit(this);
    80      }
    81
    82      protected void addError(String msg, ASTNode expr) {
    83          int line = expr.getLineNumber();
    84          int col = expr.getColumnNumber();
    85          SourceUnit source = getSourceUnit();
    86          source.getErrorCollector().addErrorAndContinue(
    87            new SyntaxErrorMessage(new SyntaxException(msg + '\n', line, col), source)
    88          );
    89      }
    90
    91      abstract protected SourceUnit getSourceUnit();
    92
    93      protected void visitStatement(Statement statement) {}
    94
    95      public void visitAssertStatement(AssertStatement statement) {
    96          visitStatement(statement);
    97          super.visitAssertStatement(statement);
    98      }
    99
    100     public void visitBlockStatement(BlockStatement block) {
    101         visitStatement(block);
    102         super.visitBlockStatement(block);
    103     }
    104
    105     public void visitBreakStatement(BreakStatement statement) {
    106         visitStatement(statement);
    107         super.visitBreakStatement(statement);
    108     }
    109
    110     public void visitCaseStatement(CaseStatement statement) {
    111         visitStatement(statement);
    112         super.visitCaseStatement(statement);
    113     }
    114
    115     public void visitCatchStatement(CatchStatement statement) {
    116         visitStatement(statement);
    117         super.visitCatchStatement(statement);
    118     }
    119
    120     public void visitContinueStatement(ContinueStatement statement) {
    121         visitStatement(statement);
    122         super.visitContinueStatement(statement);
    123     }
    124
    125     public void visitDoWhileLoop(DoWhileStatement loop) {
    126         visitStatement(loop);
    127         super.visitDoWhileLoop(loop);
    128     }
    129
    130     public void visitExpressionStatement(ExpressionStatement statement) {
    131         visitStatement(statement);
    132         super.visitExpressionStatement(statement);
    133     }
    134
    135     public void visitForLoop(ForStatement forLoop) {
    136         visitStatement(forLoop);
    137         super.visitForLoop(forLoop);
    138     }
    139
    140     public void visitIfElse(IfStatement ifElse) {
    141         visitStatement(ifElse);
    142         super.visitIfElse(ifElse);
    143     }
    144
    145     public void visitReturnStatement(ReturnStatement statement) {
    146         visitStatement(statement);
    147         super.visitReturnStatement(statement);
    148     }
    149
    150     public void visitSwitch(SwitchStatement statement) {
    151         visitStatement(statement);
    152         super.visitSwitch(statement);
    153     }
    154
    155     public void visitSynchronizedStatement(SynchronizedStatement statement) {
    156         visitStatement(statement);
    157         super.visitSynchronizedStatement(statement);
    158     }
    159
    160     public void visitThrowStatement(ThrowStatement statement) {
    161         visitStatement(statement);
    162         super.visitThrowStatement(statement);
    163     }
    164
    165     public void visitTryCatchFinally(TryCatchStatement statement) {
    166         visitStatement(statement);
    167         super.visitTryCatchFinally(statement);
    168     }
    169
    170     public void visitWhileLoop(WhileStatement loop) {
    171         visitStatement(loop);
    172         super.visitWhileLoop(loop);
    173     }
    174 }