001: /*
002: * ClassCodeVisitorSupport.java created on 14.12.2005
003: *
004: */
005: package org.codehaus.groovy.ast;
006:
007: import java.util.Iterator;
008: import java.util.List;
009:
010: import org.codehaus.groovy.ast.expr.Expression;
011: import org.codehaus.groovy.ast.stmt.AssertStatement;
012: import org.codehaus.groovy.ast.stmt.BlockStatement;
013: import org.codehaus.groovy.ast.stmt.BreakStatement;
014: import org.codehaus.groovy.ast.stmt.CaseStatement;
015: import org.codehaus.groovy.ast.stmt.CatchStatement;
016: import org.codehaus.groovy.ast.stmt.ContinueStatement;
017: import org.codehaus.groovy.ast.stmt.DoWhileStatement;
018: import org.codehaus.groovy.ast.stmt.ExpressionStatement;
019: import org.codehaus.groovy.ast.stmt.ForStatement;
020: import org.codehaus.groovy.ast.stmt.IfStatement;
021: import org.codehaus.groovy.ast.stmt.ReturnStatement;
022: import org.codehaus.groovy.ast.stmt.Statement;
023: import org.codehaus.groovy.ast.stmt.SwitchStatement;
024: import org.codehaus.groovy.ast.stmt.SynchronizedStatement;
025: import org.codehaus.groovy.ast.stmt.ThrowStatement;
026: import org.codehaus.groovy.ast.stmt.TryCatchStatement;
027: import org.codehaus.groovy.ast.stmt.WhileStatement;
028: import org.codehaus.groovy.control.SourceUnit;
029: import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
030: import org.codehaus.groovy.syntax.SyntaxException;
031:
032: public abstract class ClassCodeVisitorSupport extends
033: CodeVisitorSupport implements GroovyClassVisitor {
034:
035: public void visitClass(ClassNode node) {
036: visitAnnotations(node);
037: node.visitContents(this );
038: List list = node.getObjectInitializerStatements();
039: for (Iterator iter = list.iterator(); iter.hasNext();) {
040: Statement element = (Statement) iter.next();
041: element.visit(this );
042: }
043: }
044:
045: public void visitAnnotations(AnnotatedNode node) {
046:
047: }
048:
049: protected void visitClassCodeContainer(Statement code) {
050: if (code != null)
051: code.visit(this );
052: }
053:
054: protected void visitConstructorOrMethod(MethodNode node,
055: boolean isConstructor) {
056: Statement code = node.getCode();
057: visitClassCodeContainer(code);
058: }
059:
060: public void visitConstructor(ConstructorNode node) {
061: visitConstructorOrMethod(node, true);
062: }
063:
064: public void visitMethod(MethodNode node) {
065: visitConstructorOrMethod(node, false);
066: }
067:
068: public void visitField(FieldNode node) {
069: visitAnnotations(node);
070: Expression init = node.getInitialExpression();
071: if (init != null)
072: init.visit(this );
073: }
074:
075: public void visitProperty(PropertyNode node) {
076: Statement statement = node.getGetterBlock();
077: visitClassCodeContainer(statement);
078:
079: statement = node.getSetterBlock();
080: visitClassCodeContainer(statement);
081:
082: Expression init = node.getInitialExpression();
083: if (init != null)
084: init.visit(this );
085: }
086:
087: protected void addError(String msg, ASTNode expr) {
088: int line = expr.getLineNumber();
089: int col = expr.getColumnNumber();
090: SourceUnit source = getSourceUnit();
091: source.getErrorCollector().addErrorAndContinue(
092: new SyntaxErrorMessage(new SyntaxException(msg + '\n',
093: line, col), source));
094: }
095:
096: abstract protected SourceUnit getSourceUnit();
097:
098: protected void visitStatement(Statement statement) {
099: }
100:
101: public void visitAssertStatement(AssertStatement statement) {
102: visitStatement(statement);
103: super .visitAssertStatement(statement);
104: }
105:
106: public void visitBlockStatement(BlockStatement block) {
107: visitStatement(block);
108: super .visitBlockStatement(block);
109: }
110:
111: public void visitBreakStatement(BreakStatement statement) {
112: visitStatement(statement);
113: super .visitBreakStatement(statement);
114: }
115:
116: public void visitCaseStatement(CaseStatement statement) {
117: visitStatement(statement);
118: super .visitCaseStatement(statement);
119: }
120:
121: public void visitCatchStatement(CatchStatement statement) {
122: visitStatement(statement);
123: super .visitCatchStatement(statement);
124: }
125:
126: public void visitContinueStatement(ContinueStatement statement) {
127: visitStatement(statement);
128: super .visitContinueStatement(statement);
129: }
130:
131: public void visitDoWhileLoop(DoWhileStatement loop) {
132: visitStatement(loop);
133: super .visitDoWhileLoop(loop);
134: }
135:
136: public void visitExpressionStatement(ExpressionStatement statement) {
137: visitStatement(statement);
138: super .visitExpressionStatement(statement);
139: }
140:
141: public void visitForLoop(ForStatement forLoop) {
142: visitStatement(forLoop);
143: super .visitForLoop(forLoop);
144: }
145:
146: public void visitIfElse(IfStatement ifElse) {
147: visitStatement(ifElse);
148: super .visitIfElse(ifElse);
149: }
150:
151: public void visitReturnStatement(ReturnStatement statement) {
152: visitStatement(statement);
153: super .visitReturnStatement(statement);
154: }
155:
156: public void visitSwitch(SwitchStatement statement) {
157: visitStatement(statement);
158: super .visitSwitch(statement);
159: }
160:
161: public void visitSynchronizedStatement(
162: SynchronizedStatement statement) {
163: visitStatement(statement);
164: super .visitSynchronizedStatement(statement);
165: }
166:
167: public void visitThrowStatement(ThrowStatement statement) {
168: visitStatement(statement);
169: super .visitThrowStatement(statement);
170: }
171:
172: public void visitTryCatchFinally(TryCatchStatement statement) {
173: visitStatement(statement);
174: super .visitTryCatchFinally(statement);
175: }
176:
177: public void visitWhileLoop(WhileStatement loop) {
178: visitStatement(loop);
179: super.visitWhileLoop(loop);
180: }
181: }
|