View Javadoc

    1   /*
    2
    3    $Id: CodeVisitorSupport.java,v 1.17 2005/05/11 01:17:38 phk Exp $
    4
    5
    6
    7    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
    8
    9
    10
    11   Redistribution and use of this software and associated documentation
    12
    13   ("Software"), with or without modification, are permitted provided
    14
    15   that the following conditions are met:
    16
    17
    18
    19   1. Redistributions of source code must retain copyright
    20
    21      statements and notices.  Redistributions must also contain a
    22
    23      copy of this document.
    24
    25
    26
    27   2. Redistributions in binary form must reproduce the
    28
    29      above copyright notice, this list of conditions and the
    30
    31      following disclaimer in the documentation and/or other
    32
    33      materials provided with the distribution.
    34
    35
    36
    37   3. The name "groovy" must not be used to endorse or promote
    38
    39      products derived from this Software without prior written
    40
    41      permission of The Codehaus.  For written permission,
    42
    43      please contact info@codehaus.org.
    44
    45
    46
    47   4. Products derived from this Software may not be called "groovy"
    48
    49      nor may "groovy" appear in their names without prior written
    50
    51      permission of The Codehaus. "groovy" is a registered
    52
    53      trademark of The Codehaus.
    54
    55
    56
    57   5. Due credit should be given to The Codehaus -
    58
    59      http://groovy.codehaus.org/
    60
    61
    62
    63   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
    64
    65   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
    66
    67   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    68
    69   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    70
    71   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    72
    73   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    74
    75   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    76
    77   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    78
    79   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    80
    81   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    82
    83   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    84
    85   OF THE POSSIBILITY OF SUCH DAMAGE.
    86
    87
    88
    89   */
    90
    91  package org.codehaus.groovy.ast;
    92
    93
    94
    95  import java.util.Iterator;
    96
    97  import java.util.List;
    98
    99
    100
    101 import org.codehaus.groovy.ast.expr.*;
    102
    103 import org.codehaus.groovy.ast.stmt.AssertStatement;
    104
    105 import org.codehaus.groovy.ast.stmt.BlockStatement;
    106
    107 import org.codehaus.groovy.ast.stmt.BreakStatement;
    108
    109 import org.codehaus.groovy.ast.stmt.CaseStatement;
    110
    111 import org.codehaus.groovy.ast.stmt.CatchStatement;
    112
    113 import org.codehaus.groovy.ast.stmt.ContinueStatement;
    114
    115 import org.codehaus.groovy.ast.stmt.DoWhileStatement;
    116
    117 import org.codehaus.groovy.ast.stmt.ExpressionStatement;
    118
    119 import org.codehaus.groovy.ast.stmt.ForStatement;
    120
    121 import org.codehaus.groovy.ast.stmt.IfStatement;
    122
    123 import org.codehaus.groovy.ast.stmt.ReturnStatement;
    124
    125 import org.codehaus.groovy.ast.stmt.Statement;
    126
    127 import org.codehaus.groovy.ast.stmt.SwitchStatement;
    128
    129 import org.codehaus.groovy.ast.stmt.SynchronizedStatement;
    130
    131 import org.codehaus.groovy.ast.stmt.ThrowStatement;
    132
    133 import org.codehaus.groovy.ast.stmt.TryCatchStatement;
    134
    135 import org.codehaus.groovy.ast.stmt.WhileStatement;
    136
    137
    138
    139 /***
    140
    141  * Abstract base class for any GroovyCodeVisitory which by default
    142
    143  * just walks the code and expression tree
    144
    145  *
    146
    147  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    148
    149  * @version $Revision: 1.17 $
    150
    151  */
    152
    153 public abstract class CodeVisitorSupport implements GroovyCodeVisitor {
    154
    155
    156
    157     public void visitBlockStatement(BlockStatement block) {
    158
    159         List statements = block.getStatements();
    160
    161         for (Iterator iter = statements.iterator(); iter.hasNext(); ) {
    162
    163             Statement statement = (Statement) iter.next();
    164
    165             statement.visit(this);
    166
    167         }
    168
    169     }
    170
    171
    172
    173     public void visitForLoop(ForStatement forLoop) {
    174
    175         forLoop.getCollectionExpression().visit(this);
    176
    177         forLoop.getLoopBlock().visit(this);
    178
    179     }
    180
    181
    182
    183     public void visitWhileLoop(WhileStatement loop) {
    184
    185         loop.getBooleanExpression().visit(this);
    186
    187         loop.getLoopBlock().visit(this);
    188
    189     }
    190
    191
    192
    193     public void visitDoWhileLoop(DoWhileStatement loop) {
    194
    195         loop.getLoopBlock().visit(this);
    196
    197         loop.getBooleanExpression().visit(this);
    198
    199     }
    200
    201
    202
    203     public void visitIfElse(IfStatement ifElse) {
    204
    205         ifElse.getBooleanExpression().visit(this);
    206
    207         ifElse.getIfBlock().visit(this);
    208
    209         ifElse.getElseBlock().visit(this);
    210
    211     }
    212
    213
    214
    215     public void visitExpressionStatement(ExpressionStatement statement) {
    216
    217         statement.getExpression().visit(this);
    218
    219     }
    220
    221
    222
    223     public void visitReturnStatement(ReturnStatement statement) {
    224
    225         statement.getExpression().visit(this);
    226
    227     }
    228
    229
    230
    231     public void visitAssertStatement(AssertStatement statement) {
    232
    233         statement.getBooleanExpression().visit(this);
    234
    235         statement.getMessageExpression().visit(this);
    236
    237     }
    238
    239
    240
    241     public void visitTryCatchFinally(TryCatchStatement statement) {
    242
    243         statement.getTryStatement().visit(this);
    244
    245         List list = statement.getCatchStatements();
    246
    247         for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    248
    249             CatchStatement catchStatement = (CatchStatement) iter.next();
    250
    251             catchStatement.visit(this);
    252
    253         }
    254
    255         statement.getFinallyStatement().visit(this);
    256
    257     }
    258
    259
    260
    261     public void visitSwitch(SwitchStatement statement) {
    262
    263         statement.getExpression().visit(this);
    264
    265         List list = statement.getCaseStatements();
    266
    267         for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    268
    269             CaseStatement caseStatement = (CaseStatement) iter.next();
    270
    271             caseStatement.visit(this);
    272
    273         }
    274
    275         statement.getDefaultStatement().visit(this);
    276
    277     }
    278
    279
    280
    281     public void visitCaseStatement(CaseStatement statement) {
    282
    283         statement.getExpression().visit(this);
    284
    285         statement.getCode().visit(this);
    286
    287     }
    288
    289
    290
    291     public void visitBreakStatement(BreakStatement statement) {
    292
    293     }
    294
    295
    296
    297     public void visitContinueStatement(ContinueStatement statement) {
    298
    299     }
    300
    301
    302
    303     public void visitSynchronizedStatement(SynchronizedStatement statement) {
    304
    305         statement.getExpression().visit(this);
    306
    307         statement.getCode().visit(this);
    308
    309     }
    310
    311
    312
    313     public void visitThrowStatement(ThrowStatement statement) {
    314
    315         statement.getExpression().visit(this);
    316
    317     }
    318
    319
    320
    321     public void visitMethodCallExpression(MethodCallExpression call) {
    322
    323         call.getObjectExpression().visit(this);
    324
    325         call.getArguments().visit(this);
    326
    327     }
    328
    329
    330
    331     public void visitStaticMethodCallExpression(StaticMethodCallExpression call) {
    332
    333         call.getArguments().visit(this);
    334
    335     }
    336
    337
    338
    339     public void visitConstructorCallExpression(ConstructorCallExpression call) {
    340
    341         call.getArguments().visit(this);
    342
    343     }
    344
    345
    346
    347     public void visitBinaryExpression(BinaryExpression expression) {
    348
    349         expression.getLeftExpression().visit(this);
    350
    351         expression.getRightExpression().visit(this);
    352
    353     }
    354
    355
    356
    357     public void visitTernaryExpression(TernaryExpression expression) {
    358
    359         expression.getBooleanExpression().visit(this);
    360
    361         expression.getTrueExpression().visit(this);
    362
    363         expression.getFalseExpression().visit(this);
    364
    365     }
    366
    367
    368
    369     public void visitPostfixExpression(PostfixExpression expression) {
    370
    371         expression.getExpression().visit(this);
    372
    373     }
    374
    375
    376
    377     public void visitPrefixExpression(PrefixExpression expression) {
    378
    379         expression.getExpression().visit(this);
    380
    381     }
    382
    383
    384
    385     public void visitBooleanExpression(BooleanExpression expression) {
    386
    387 		expression.getExpression().visit(this);
    388
    389 	}
    390
    391
    392
    393 	public void visitNotExpression(NotExpression expression) {
    394
    395 		expression.getExpression().visit(this);
    396
    397 	}
    398
    399
    400
    401     public void visitClosureExpression(ClosureExpression expression) {
    402
    403         expression.getCode().visit(this);
    404
    405     }
    406
    407
    408
    409     public void visitTupleExpression(TupleExpression expression) {
    410
    411         visitListOfExpressions(expression.getExpressions());
    412
    413     }
    414
    415
    416
    417     public void visitListExpression(ListExpression expression) {
    418
    419         visitListOfExpressions(expression.getExpressions());
    420
    421     }
    422
    423
    424
    425     public void visitArrayExpression(ArrayExpression expression) {
    426
    427         visitListOfExpressions(expression.getExpressions());
    428
    429     }
    430
    431
    432
    433     public void visitMapExpression(MapExpression expression) {
    434
    435         visitListOfExpressions(expression.getMapEntryExpressions());
    436
    437
    438
    439     }
    440
    441
    442
    443     public void visitMapEntryExpression(MapEntryExpression expression) {
    444
    445         expression.getKeyExpression().visit(this);
    446
    447         expression.getValueExpression().visit(this);
    448
    449
    450
    451     }
    452
    453
    454
    455     public void visitRangeExpression(RangeExpression expression) {
    456
    457         expression.getFrom().visit(this);
    458
    459         expression.getTo().visit(this);
    460
    461     }
    462
    463
    464
    465     public void visitSpreadExpression(SpreadExpression expression) {
    466
    467         expression.getExpression().visit(this);
    468
    469     }
    470
    471
    472     public void visitSpreadMapExpression(SpreadMapExpression expression) {
    473         expression.getExpression().visit(this);
    474     }
    475
    476
    477     public void visitMethodPointerExpression(MethodPointerExpression expression) {
    478
    479         expression.getExpression().visit(this);
    480
    481     }
    482
    483
    484
    485     public void visitNegationExpression(NegationExpression expression) {
    486
    487         expression.getExpression().visit(this);
    488
    489     }
    490
    491
    492
    493     public void visitBitwiseNegExpression(BitwiseNegExpression expression) {
    494
    495         expression.getExpression().visit(this);
    496
    497     }
    498
    499
    500
    501     public void visitCastExpression(CastExpression expression) {
    502
    503         expression.getExpression().visit(this);
    504
    505     }
    506
    507
    508
    509     public void visitConstantExpression(ConstantExpression expression) {
    510
    511     }
    512
    513
    514
    515     public void visitClassExpression(ClassExpression expression) {
    516
    517     }
    518
    519
    520
    521     public void visitVariableExpression(VariableExpression expression) {
    522
    523     }
    524
    525
    526
    527     public void visitDeclarationExpression(DeclarationExpression expression) {
    528
    529         visitBinaryExpression(expression);
    530
    531     }
    532
    533
    534
    535     public void visitPropertyExpression(PropertyExpression expression) {
    536
    537         expression.getObjectExpression().visit(this);
    538
    539     }
    540
    541
    542
    543     public void visitAttributeExpression(AttributeExpression expression) {
    544
    545         expression.getObjectExpression().visit(this);
    546
    547     }
    548
    549
    550
    551     public void visitFieldExpression(FieldExpression expression) {
    552
    553     }
    554
    555
    556
    557     public void visitRegexExpression(RegexExpression expression) {
    558
    559     }
    560
    561
    562
    563     public void visitGStringExpression(GStringExpression expression) {
    564
    565         visitListOfExpressions(expression.getStrings());
    566
    567         visitListOfExpressions(expression.getValues());
    568
    569     }
    570
    571
    572
    573     protected void visitListOfExpressions(List list) {
    574
    575         Expression expression, expr2, expr3;
    576
    577         for (Iterator iter = list.iterator(); iter.hasNext(); ) {
    578
    579             expression = (Expression) iter.next();
    580
    581             if (expression instanceof SpreadExpression) {
    582
    583                 expr2 = ((SpreadExpression) expression).getExpression();
    584
    585                 expr2.visit(this);
    586
    587             }
    588
    589             else {
    590
    591                 expression.visit(this);
    592
    593             }
    594
    595         }
    596
    597     }
    598
    599     public void visitCatchStatement(CatchStatement statement) {
    600         statement.getCode().visit(this);
    601     }
    602
    603 }
    604