View Javadoc

    1   /*
    2    $Id: CodeVisitorSupport.java,v 1.10 2004/05/04 03:27:44 cpoirier Exp $
    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.ArrayExpression;
    52  import org.codehaus.groovy.ast.expr.BinaryExpression;
    53  import org.codehaus.groovy.ast.expr.BooleanExpression;
    54  import org.codehaus.groovy.ast.expr.CastExpression;
    55  import org.codehaus.groovy.ast.expr.ClassExpression;
    56  import org.codehaus.groovy.ast.expr.ClosureExpression;
    57  import org.codehaus.groovy.ast.expr.ConstantExpression;
    58  import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
    59  import org.codehaus.groovy.ast.expr.Expression;
    60  import org.codehaus.groovy.ast.expr.FieldExpression;
    61  import org.codehaus.groovy.ast.expr.GStringExpression;
    62  import org.codehaus.groovy.ast.expr.ListExpression;
    63  import org.codehaus.groovy.ast.expr.MapEntryExpression;
    64  import org.codehaus.groovy.ast.expr.MapExpression;
    65  import org.codehaus.groovy.ast.expr.MethodCallExpression;
    66  import org.codehaus.groovy.ast.expr.NegationExpression;
    67  import org.codehaus.groovy.ast.expr.NotExpression;
    68  import org.codehaus.groovy.ast.expr.PostfixExpression;
    69  import org.codehaus.groovy.ast.expr.PrefixExpression;
    70  import org.codehaus.groovy.ast.expr.PropertyExpression;
    71  import org.codehaus.groovy.ast.expr.RangeExpression;
    72  import org.codehaus.groovy.ast.expr.RegexExpression;
    73  import org.codehaus.groovy.ast.expr.StaticMethodCallExpression;
    74  import org.codehaus.groovy.ast.expr.TernaryExpression;
    75  import org.codehaus.groovy.ast.expr.TupleExpression;
    76  import org.codehaus.groovy.ast.expr.VariableExpression;
    77  import org.codehaus.groovy.ast.stmt.AssertStatement;
    78  import org.codehaus.groovy.ast.stmt.BlockStatement;
    79  import org.codehaus.groovy.ast.stmt.BreakStatement;
    80  import org.codehaus.groovy.ast.stmt.CaseStatement;
    81  import org.codehaus.groovy.ast.stmt.CatchStatement;
    82  import org.codehaus.groovy.ast.stmt.ContinueStatement;
    83  import org.codehaus.groovy.ast.stmt.DoWhileStatement;
    84  import org.codehaus.groovy.ast.stmt.ExpressionStatement;
    85  import org.codehaus.groovy.ast.stmt.ForStatement;
    86  import org.codehaus.groovy.ast.stmt.IfStatement;
    87  import org.codehaus.groovy.ast.stmt.ReturnStatement;
    88  import org.codehaus.groovy.ast.stmt.Statement;
    89  import org.codehaus.groovy.ast.stmt.SwitchStatement;
    90  import org.codehaus.groovy.ast.stmt.SynchronizedStatement;
    91  import org.codehaus.groovy.ast.stmt.ThrowStatement;
    92  import org.codehaus.groovy.ast.stmt.TryCatchStatement;
    93  import org.codehaus.groovy.ast.stmt.WhileStatement;
    94
    95  /***
    96   * Abstract base class for any GroovyCodeVisitory which by default
    97   * just walks the code and expression tree
    98   *
    99   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    100  * @version $Revision: 1.10 $
    101  */
    102 public abstract class CodeVisitorSupport implements GroovyCodeVisitor {
    103
    104     public void visitBlockStatement(BlockStatement block) {
    105         List statements = block.getStatements();
    106         for (Iterator iter = statements.iterator(); iter.hasNext(); ) {
    107             Statement statement = (Statement) iter.next();
    108             statement.visit(this);
    109         }
    110     }
    111
    112     public void visitForLoop(ForStatement forLoop) {
    113         forLoop.getCollectionExpression().visit(this);
    114         forLoop.getLoopBlock().visit(this);
    115     }
    116
    117     public void visitWhileLoop(WhileStatement loop) {
    118         loop.getBooleanExpression().visit(this);
    119         loop.getLoopBlock().visit(this);
    120     }
    121
    122     public void visitDoWhileLoop(DoWhileStatement loop) {
    123         loop.getLoopBlock().visit(this);
    124         loop.getBooleanExpression().visit(this);
    125     }
    126
    127     public void visitIfElse(IfStatement ifElse) {
    128         ifElse.getBooleanExpression().visit(this);
    129         ifElse.getIfBlock().visit(this);
    130         ifElse.getElseBlock().visit(this);
    131     }
    132
    133     public void visitExpressionStatement(ExpressionStatement statement) {
    134         statement.getExpression().visit(this);
    135     }
    136
    137     public void visitReturnStatement(ReturnStatement statement) {
    138         statement.getExpression().visit(this);
    139     }
    140
    141     public void visitAssertStatement(AssertStatement statement) {
    142         statement.getBooleanExpression().visit(this);
    143         statement.getMessageExpression().visit(this);
    144     }
    145
    146     public void visitTryCatchFinally(TryCatchStatement statement) {
    147         statement.getTryStatement().visit(this);
    148         List list = statement.getCatchStatements();
    149         for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    150             CatchStatement catchStatement = (CatchStatement) iter.next();
    151             catchStatement.visit(this);
    152         }
    153         statement.getFinallyStatement().visit(this);
    154     }
    155
    156     public void visitSwitch(SwitchStatement statement) {
    157         statement.getExpression().visit(this);
    158         List list = statement.getCaseStatements();
    159         for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    160             CaseStatement caseStatement = (CaseStatement) iter.next();
    161             caseStatement.visit(this);
    162         }
    163         statement.getDefaultStatement().visit(this);
    164     }
    165
    166     public void visitCaseStatement(CaseStatement statement) {
    167         statement.getExpression().visit(this);
    168         statement.getCode().visit(this);
    169     }
    170
    171     public void visitBreakStatement(BreakStatement statement) {
    172     }
    173
    174     public void visitContinueStatement(ContinueStatement statement) {
    175     }
    176
    177     public void visitSynchronizedStatement(SynchronizedStatement statement) {
    178         statement.getExpression().visit(this);
    179         statement.getCode().visit(this);
    180     }
    181
    182     public void visitThrowStatement(ThrowStatement statement) {
    183         statement.getExpression().visit(this);
    184     }
    185
    186     public void visitMethodCallExpression(MethodCallExpression call) {
    187         call.getObjectExpression().visit(this);
    188         call.getArguments().visit(this);
    189     }
    190
    191     public void visitStaticMethodCallExpression(StaticMethodCallExpression call) {
    192         call.getArguments().visit(this);
    193     }
    194
    195     public void visitConstructorCallExpression(ConstructorCallExpression call) {
    196         call.getArguments().visit(this);
    197     }
    198
    199     public void visitBinaryExpression(BinaryExpression expression) {
    200         expression.getLeftExpression().visit(this);
    201         expression.getRightExpression().visit(this);
    202     }
    203
    204     public void visitTernaryExpression(TernaryExpression expression) {
    205         expression.getBooleanExpression().visit(this);
    206         expression.getTrueExpression().visit(this);
    207         expression.getFalseExpression().visit(this);
    208     }
    209
    210     public void visitPostfixExpression(PostfixExpression expression) {
    211         expression.getExpression().visit(this);
    212     }
    213
    214     public void visitPrefixExpression(PrefixExpression expression) {
    215         expression.getExpression().visit(this);
    216     }
    217
    218     public void visitBooleanExpression(BooleanExpression expression) {
    219 		expression.getExpression().visit(this);
    220 	}
    221
    222 	public void visitNotExpression(NotExpression expression) {
    223 		expression.getExpression().visit(this);
    224 	}
    225
    226     public void visitClosureExpression(ClosureExpression expression) {
    227         expression.getCode().visit(this);
    228     }
    229
    230     public void visitTupleExpression(TupleExpression expression) {
    231         visitListOfExpressions(expression.getExpressions());
    232     }
    233
    234     public void visitListExpression(ListExpression expression) {
    235         visitListOfExpressions(expression.getExpressions());
    236     }
    237
    238     public void visitArrayExpression(ArrayExpression expression) {
    239         visitListOfExpressions(expression.getExpressions());
    240     }
    241
    242     public void visitMapExpression(MapExpression expression) {
    243         visitListOfExpressions(expression.getMapEntryExpressions());
    244
    245     }
    246
    247     public void visitMapEntryExpression(MapEntryExpression expression) {
    248         expression.getKeyExpression().visit(this);
    249         expression.getValueExpression().visit(this);
    250
    251     }
    252
    253     public void visitRangeExpression(RangeExpression expression) {
    254         expression.getFrom().visit(this);
    255         expression.getTo().visit(this);
    256     }
    257
    258     public void visitNegationExpression(NegationExpression expression) {
    259         expression.getExpression().visit(this);
    260     }
    261
    262     public void visitCastExpression(CastExpression expression) {
    263         expression.getExpression().visit(this);
    264     }
    265
    266     public void visitConstantExpression(ConstantExpression expression) {
    267     }
    268
    269     public void visitClassExpression(ClassExpression expression) {
    270     }
    271
    272     public void visitVariableExpression(VariableExpression expression) {
    273     }
    274
    275     public void visitPropertyExpression(PropertyExpression expression) {
    276         expression.getObjectExpression().visit(this);
    277     }
    278
    279     public void visitFieldExpression(FieldExpression expression) {
    280     }
    281
    282     public void visitRegexExpression(RegexExpression expression) {
    283     }
    284
    285     public void visitGStringExpression(GStringExpression expression) {
    286         visitListOfExpressions(expression.getStrings());
    287         visitListOfExpressions(expression.getValues());
    288     }
    289
    290     protected void visitListOfExpressions(List list) {
    291         for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    292             Expression expression = (Expression) iter.next();
    293             expression.visit(this);
    294         }
    295     }
    296
    297 }