View Javadoc

    1   /*
    2    $Id: CodeVisitorSupport.java 3893 2006-07-10 15:42:26Z blackdrag $
    3
    4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
    5
    6    Redistribution and use of this software and associated documentation
    7    ("Software"), with or without modification, are permitted provided
    8    that the following conditions are met:
    9
    10   1. Redistributions of source code must retain copyright
    11      statements and notices.  Redistributions must also contain a
    12      copy of this document.
    13
    14   2. Redistributions in binary form must reproduce the
    15      above copyright notice, this list of conditions and the
    16      following disclaimer in the documentation and/or other
    17      materials provided with the distribution.
    18
    19   3. The name "groovy" must not be used to endorse or promote
    20      products derived from this Software without prior written
    21      permission of The Codehaus.  For written permission,
    22      please contact info@codehaus.org.
    23
    24   4. Products derived from this Software may not be called "groovy"
    25      nor may "groovy" appear in their names without prior written
    26      permission of The Codehaus. "groovy" is a registered
    27      trademark of The Codehaus.
    28
    29   5. Due credit should be given to The Codehaus -
    30      http://groovy.codehaus.org/
    31
    32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
    33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
    34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    43   OF THE POSSIBILITY OF SUCH DAMAGE.
    44
    45   */
    46  package org.codehaus.groovy.ast;
    47
    48  import java.util.Iterator;
    49  import java.util.List;
    50
    51  import org.codehaus.groovy.ast.expr.*;
    52  import org.codehaus.groovy.ast.stmt.AssertStatement;
    53  import org.codehaus.groovy.ast.stmt.BlockStatement;
    54  import org.codehaus.groovy.ast.stmt.BreakStatement;
    55  import org.codehaus.groovy.ast.stmt.CaseStatement;
    56  import org.codehaus.groovy.ast.stmt.CatchStatement;
    57  import org.codehaus.groovy.ast.stmt.ContinueStatement;
    58  import org.codehaus.groovy.ast.stmt.DoWhileStatement;
    59  import org.codehaus.groovy.ast.stmt.ExpressionStatement;
    60  import org.codehaus.groovy.ast.stmt.ForStatement;
    61  import org.codehaus.groovy.ast.stmt.IfStatement;
    62  import org.codehaus.groovy.ast.stmt.ReturnStatement;
    63  import org.codehaus.groovy.ast.stmt.Statement;
    64  import org.codehaus.groovy.ast.stmt.SwitchStatement;
    65  import org.codehaus.groovy.ast.stmt.SynchronizedStatement;
    66  import org.codehaus.groovy.ast.stmt.ThrowStatement;
    67  import org.codehaus.groovy.ast.stmt.TryCatchStatement;
    68  import org.codehaus.groovy.ast.stmt.WhileStatement;
    69
    70  /***
    71   * Abstract base class for any GroovyCodeVisitory which by default
    72   * just walks the code and expression tree
    73   *
    74   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    75   * @version $Revision: 3893 $
    76   */
    77  public abstract class CodeVisitorSupport implements GroovyCodeVisitor {
    78
    79      public void visitBlockStatement(BlockStatement block) {
    80          List statements = block.getStatements();
    81          for (Iterator iter = statements.iterator(); iter.hasNext(); ) {
    82              Statement statement = (Statement) iter.next();
    83              statement.visit(this);
    84          }
    85      }
    86
    87      public void visitForLoop(ForStatement forLoop) {
    88          forLoop.getCollectionExpression().visit(this);
    89          forLoop.getLoopBlock().visit(this);
    90      }
    91
    92      public void visitWhileLoop(WhileStatement loop) {
    93          loop.getBooleanExpression().visit(this);
    94          loop.getLoopBlock().visit(this);
    95      }
    96
    97      public void visitDoWhileLoop(DoWhileStatement loop) {
    98          loop.getLoopBlock().visit(this);
    99          loop.getBooleanExpression().visit(this);
    100     }
    101
    102     public void visitIfElse(IfStatement ifElse) {
    103         ifElse.getBooleanExpression().visit(this);
    104         ifElse.getIfBlock().visit(this);
    105         ifElse.getElseBlock().visit(this);
    106     }
    107
    108     public void visitExpressionStatement(ExpressionStatement statement) {
    109         statement.getExpression().visit(this);
    110     }
    111
    112     public void visitReturnStatement(ReturnStatement statement) {
    113         statement.getExpression().visit(this);
    114     }
    115
    116     public void visitAssertStatement(AssertStatement statement) {
    117         statement.getBooleanExpression().visit(this);
    118         statement.getMessageExpression().visit(this);
    119     }
    120
    121     public void visitTryCatchFinally(TryCatchStatement statement) {
    122         statement.getTryStatement().visit(this);
    123         List list = statement.getCatchStatements();
    124         for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    125             CatchStatement catchStatement = (CatchStatement) iter.next();
    126             catchStatement.visit(this);
    127         }
    128         statement.getFinallyStatement().visit(this);
    129     }
    130
    131     public void visitSwitch(SwitchStatement statement) {
    132         statement.getExpression().visit(this);
    133         List list = statement.getCaseStatements();
    134         for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    135             CaseStatement caseStatement = (CaseStatement) iter.next();
    136             caseStatement.visit(this);
    137         }
    138         statement.getDefaultStatement().visit(this);
    139     }
    140
    141     public void visitCaseStatement(CaseStatement statement) {
    142         statement.getExpression().visit(this);
    143         statement.getCode().visit(this);
    144     }
    145
    146     public void visitBreakStatement(BreakStatement statement) {
    147     }
    148
    149     public void visitContinueStatement(ContinueStatement statement) {
    150     }
    151
    152     public void visitSynchronizedStatement(SynchronizedStatement statement) {
    153         statement.getExpression().visit(this);
    154         statement.getCode().visit(this);
    155     }
    156
    157     public void visitThrowStatement(ThrowStatement statement) {
    158         statement.getExpression().visit(this);
    159     }
    160
    161     public void visitMethodCallExpression(MethodCallExpression call) {
    162         call.getObjectExpression().visit(this);
    163         call.getMethod().visit(this);
    164         call.getArguments().visit(this);
    165     }
    166
    167     public void visitStaticMethodCallExpression(StaticMethodCallExpression call) {
    168         call.getArguments().visit(this);
    169     }
    170
    171     public void visitConstructorCallExpression(ConstructorCallExpression call) {
    172         call.getArguments().visit(this);
    173     }
    174
    175     public void visitBinaryExpression(BinaryExpression expression) {
    176         expression.getLeftExpression().visit(this);
    177         expression.getRightExpression().visit(this);
    178     }
    179
    180     public void visitTernaryExpression(TernaryExpression expression) {
    181         expression.getBooleanExpression().visit(this);
    182         expression.getTrueExpression().visit(this);
    183         expression.getFalseExpression().visit(this);
    184     }
    185
    186     public void visitPostfixExpression(PostfixExpression expression) {
    187         expression.getExpression().visit(this);
    188     }
    189
    190     public void visitPrefixExpression(PrefixExpression expression) {
    191         expression.getExpression().visit(this);
    192     }
    193
    194     public void visitBooleanExpression(BooleanExpression expression) {
    195 		expression.getExpression().visit(this);
    196 	}
    197
    198 	public void visitNotExpression(NotExpression expression) {
    199 		expression.getExpression().visit(this);
    200 	}
    201
    202     public void visitClosureExpression(ClosureExpression expression) {
    203         expression.getCode().visit(this);
    204     }
    205
    206     public void visitTupleExpression(TupleExpression expression) {
    207         visitListOfExpressions(expression.getExpressions());
    208     }
    209
    210     public void visitListExpression(ListExpression expression) {
    211         visitListOfExpressions(expression.getExpressions());
    212     }
    213
    214     public void visitArrayExpression(ArrayExpression expression) {
    215         visitListOfExpressions(expression.getExpressions());
    216     }
    217
    218     public void visitMapExpression(MapExpression expression) {
    219         visitListOfExpressions(expression.getMapEntryExpressions());
    220
    221     }
    222
    223     public void visitMapEntryExpression(MapEntryExpression expression) {
    224         expression.getKeyExpression().visit(this);
    225         expression.getValueExpression().visit(this);
    226
    227     }
    228
    229     public void visitRangeExpression(RangeExpression expression) {
    230         expression.getFrom().visit(this);
    231         expression.getTo().visit(this);
    232     }
    233
    234     public void visitSpreadExpression(SpreadExpression expression) {
    235         expression.getExpression().visit(this);
    236     }
    237
    238     public void visitSpreadMapExpression(SpreadMapExpression expression) {
    239         expression.getExpression().visit(this);
    240     }
    241
    242     public void visitMethodPointerExpression(MethodPointerExpression expression) {
    243         expression.getExpression().visit(this);
    244     }
    245
    246     public void visitNegationExpression(NegationExpression expression) {
    247         expression.getExpression().visit(this);
    248     }
    249
    250     public void visitBitwiseNegExpression(BitwiseNegExpression expression) {
    251         expression.getExpression().visit(this);
    252     }
    253
    254     public void visitCastExpression(CastExpression expression) {
    255         expression.getExpression().visit(this);
    256     }
    257
    258     public void visitConstantExpression(ConstantExpression expression) {
    259     }
    260
    261     public void visitClassExpression(ClassExpression expression) {
    262     }
    263
    264     public void visitVariableExpression(VariableExpression expression) {
    265     }
    266
    267     public void visitDeclarationExpression(DeclarationExpression expression) {
    268         visitBinaryExpression(expression);
    269     }
    270
    271     public void visitPropertyExpression(PropertyExpression expression) {
    272     	expression.getObjectExpression().visit(this);
    273     	expression.getProperty().visit(this);
    274     }
    275
    276     public void visitAttributeExpression(AttributeExpression expression) {
    277     	expression.getObjectExpression().visit(this);
    278     	expression.getProperty().visit(this);
    279     }
    280
    281     public void visitFieldExpression(FieldExpression expression) {
    282     }
    283
    284     public void visitRegexExpression(RegexExpression expression) {
    285     }
    286
    287     public void visitGStringExpression(GStringExpression expression) {
    288         visitListOfExpressions(expression.getStrings());
    289         visitListOfExpressions(expression.getValues());
    290     }
    291
    292     protected void visitListOfExpressions(List list) {
    293         Expression expression, expr2, expr3;
    294         for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    295             expression = (Expression) iter.next();
    296             if (expression instanceof SpreadExpression) {
    297                 expr2 = ((SpreadExpression) expression).getExpression();
    298                 expr2.visit(this);
    299             }
    300             else {
    301                 expression.visit(this);
    302             }
    303         }
    304     }
    305     public void visitCatchStatement(CatchStatement statement) {
    306     	statement.getCode().visit(this);
    307     }
    308
    309     public void visitArgumentlistExpression(ArgumentListExpression ale) {
    310     	visitTupleExpression(ale);
    311     }
    312 }