KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > ASTRewriteClear


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.dom;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.jdt.core.dom.*;
16
17 import org.eclipse.jdt.internal.corext.Assert;
18
19 public class ASTRewriteClear extends ASTVisitor {
20
21     private OldASTRewrite fRewrite;
22
23     public ASTRewriteClear(OldASTRewrite rewrite) {
24         fRewrite= rewrite;
25     }
26     
27     private void clearList(List JavaDoc list) {
28         for (int i= list.size() - 1; i >= 0 ; i--) {
29             ASTNode element= (ASTNode) list.get(i);
30             if (fRewrite.isInserted(element)) {
31                 list.remove(i);
32             } else {
33                 if (fRewrite.isCollapsed(element)) {
34                     List JavaDoc collapsed= ((Block) element).statements();
35                     list.remove(i);
36                     for (int k= collapsed.size() - 1; k >= 0 ; k--) {
37                         Object JavaDoc curr= collapsed.remove(k);
38                         list.add(i, curr);
39                     }
40                 }
41             }
42         }
43     }
44     
45     private boolean isInserted(ASTNode node) {
46         return node != null && fRewrite.isInserted(node);
47     }
48     
49     
50     /*
51      * @see ASTVisitor#visit(AnonymousClassDeclaration)
52      */

53     public boolean visit(AnonymousClassDeclaration node) {
54         clearList(node.bodyDeclarations());
55         return true;
56     }
57
58     /*
59      * @see ASTVisitor#visit(ArrayCreation)
60      */

61     public boolean visit(ArrayCreation node) {
62         clearList(node.dimensions());
63         if (isInserted(node.getInitializer())) {
64             node.setInitializer(null);
65         }
66         return true;
67     }
68
69     /*
70      * @see ASTVisitor#visit(ArrayInitializer)
71      */

72     public boolean visit(ArrayInitializer node) {
73         clearList(node.expressions());
74         return true;
75     }
76
77
78     /*
79      * @see ASTVisitor#visit(AssertStatement)
80      */

81     public boolean visit(AssertStatement node) {
82         node.getExpression().accept(this);
83         if (isInserted(node.getMessage())) {
84             node.setMessage(null);
85         }
86         return true;
87     }
88
89     /*
90      * @see ASTVisitor#visit(Block)
91      */

92     public boolean visit(Block node) {
93         clearList(node.statements());
94         return true;
95     }
96
97     /*
98      * @see ASTVisitor#visit(BreakStatement)
99      */

100     public boolean visit(BreakStatement node) {
101         if (isInserted(node.getLabel())) {
102             node.setLabel(null);
103         }
104         return true;
105     }
106
107     /*
108      * @see ASTVisitor#visit(ClassInstanceCreation)
109      */

110     public boolean visit(ClassInstanceCreation node) {
111         if (isInserted(node.getExpression())) {
112             node.setExpression(null);
113         }
114         clearList(node.arguments());
115         if (isInserted(node.getAnonymousClassDeclaration())) {
116             node.setAnonymousClassDeclaration(null);
117         }
118         return true;
119     }
120
121     /*
122      * @see ASTVisitor#visit(CompilationUnit)
123      */

124     public boolean visit(CompilationUnit node) {
125         if (isInserted(node.getPackage())) {
126             node.setPackage(null);
127         }
128         clearList(node.imports());
129         clearList(node.types());
130         return true;
131     }
132
133
134     /*
135      * @see ASTVisitor#visit(ConstructorInvocation)
136      */

137     public boolean visit(ConstructorInvocation node) {
138         clearList(node.arguments());
139         return true;
140     }
141
142     /*
143      * @see ASTVisitor#visit(ContinueStatement)
144      */

145     public boolean visit(ContinueStatement node) {
146         if (isInserted(node.getLabel())) {
147             node.setLabel(null);
148         }
149         return true;
150     }
151
152     /*
153      * @see ASTVisitor#visit(FieldDeclaration)
154      */

155     public boolean visit(FieldDeclaration node) {
156         if (isInserted(node.getJavadoc())) {
157             node.setJavadoc(null);
158         }
159         clearList(node.fragments());
160         return true;
161     }
162
163     /*
164      * @see ASTVisitor#visit(ForStatement)
165      */

166     public boolean visit(ForStatement node) {
167         clearList(node.initializers());
168         clearList(node.updaters());
169         if (isInserted(node.getExpression())) {
170             node.setExpression(null);
171         }
172         return true;
173     }
174
175     /*
176      * @see ASTVisitor#visit(IfStatement)
177      */

178     public boolean visit(IfStatement node) {
179         if (isInserted(node.getElseStatement())) {
180             node.setElseStatement(null);
181         }
182         return true;
183     }
184
185     /*
186      * @see ASTVisitor#visit(InfixExpression)
187      */

188     public boolean visit(InfixExpression node) {
189         clearList(node.extendedOperands());
190         return true;
191     }
192
193     /*
194      * @see ASTVisitor#visit(Initializer)
195      */

196     public boolean visit(Initializer node) {
197         if (isInserted(node.getJavadoc())) {
198             node.setJavadoc(null);
199         }
200         return true;
201     }
202
203     /*
204      * @see ASTVisitor#visit(MethodDeclaration)
205      */

206     public boolean visit(MethodDeclaration node) {
207         if (isInserted(node.getJavadoc())) {
208             node.setJavadoc(null);
209         }
210         if (isInserted(node.getReturnType())) {
211             node.setReturnType(node.getAST().newPrimitiveType(PrimitiveType.VOID));
212         }
213         clearList(node.parameters());
214         clearList(node.thrownExceptions());
215         if (isInserted(node.getBody())) {
216             node.setBody(null);
217         }
218         return true;
219     }
220
221     /*
222      * @see ASTVisitor#visit(MethodInvocation)
223      */

224     public boolean visit(MethodInvocation node) {
225         if (isInserted(node.getExpression())) {
226             node.setExpression(null);
227         }
228         clearList(node.arguments());
229         return true;
230     }
231
232     /*
233      * @see ASTVisitor#visit(ReturnStatement)
234      */

235     public boolean visit(ReturnStatement node) {
236         if (isInserted(node.getExpression())) {
237             node.setExpression(null);
238         }
239         return true;
240     }
241
242     /*
243      * @see ASTVisitor#visit(SingleVariableDeclaration)
244      */

245     public boolean visit(SingleVariableDeclaration node) {
246         if (isInserted(node.getInitializer())) {
247             node.setInitializer(null);
248         }
249         return true;
250     }
251
252     /*
253      * @see ASTVisitor#visit(SuperConstructorInvocation)
254      */

255     public boolean visit(SuperConstructorInvocation node) {
256         if (isInserted(node.getExpression())) {
257             node.setExpression(null);
258         }
259         clearList(node.arguments());
260         return true;
261     }
262
263     /*
264      * @see ASTVisitor#visit(SuperFieldAccess)
265      */

266     public boolean visit(SuperFieldAccess node) {
267         if (isInserted(node.getQualifier())) {
268             node.setQualifier(null);
269         }
270         return true;
271     }
272
273     /*
274      * @see ASTVisitor#visit(SuperMethodInvocation)
275      */

276     public boolean visit(SuperMethodInvocation node) {
277         if (isInserted(node.getQualifier())) {
278             node.setQualifier(null);
279         }
280         clearList(node.arguments());
281         return true;
282     }
283
284     /*
285      * @see ASTVisitor#visit(SwitchCase)
286      */

287     public boolean visit(SwitchCase node) {
288         if (isInserted(node.getExpression())) {
289             node.setExpression(null);
290         }
291         return true;
292     }
293
294     /*
295      * @see ASTVisitor#visit(SwitchStatement)
296      */

297     public boolean visit(SwitchStatement node) {
298         clearList(node.statements());
299         return true;
300     }
301
302     /*
303      * @see ASTVisitor#visit(ThisExpression)
304      */

305     public boolean visit(ThisExpression node) {
306         if (isInserted(node.getQualifier())) {
307             node.setQualifier(null);
308         }
309         return true;
310     }
311
312     /*
313      * @see ASTVisitor#visit(TryStatement)
314      */

315     public boolean visit(TryStatement node) {
316         clearList(node.catchClauses());
317         if (isInserted(node.getFinally())) {
318             node.setFinally(null);
319         }
320         return true;
321     }
322
323     /*
324      * @see ASTVisitor#visit(TypeDeclaration)
325      */

326     public boolean visit(TypeDeclaration node) {
327         if (isInserted(node.getJavadoc())) {
328             node.setJavadoc(null);
329         }
330         if (isInserted(node.getSuperclass())) {
331             node.setSuperclass(null);
332         }
333         clearList(node.superInterfaces());
334         clearList(node.bodyDeclarations());
335         return true;
336     }
337
338     /*
339      * @see ASTVisitor#visit(VariableDeclarationExpression)
340      */

341     public boolean visit(VariableDeclarationExpression node) {
342         clearList(node.fragments());
343         return true;
344     }
345
346     /*
347      * @see ASTVisitor#visit(VariableDeclarationFragment)
348      */

349     public boolean visit(VariableDeclarationFragment node) {
350         if (isInserted(node.getInitializer())) {
351             node.setInitializer(null);
352         }
353         return true;
354     }
355
356     /*
357      * @see ASTVisitor#visit(VariableDeclarationStatement)
358      */

359     public boolean visit(VariableDeclarationStatement node) {
360         clearList(node.fragments());
361         return true;
362     }
363     
364     /* (non-Javadoc)
365      * @see org.eclipse.jdt.core.dom.ASTVisitor#postVisit(org.eclipse.jdt.core.dom.ASTNode)
366      */

367     public void postVisit(ASTNode node) {
368         if (fRewrite.isInserted(node)) {
369             Assert.isTrue(false, "Inserted node not removed " + node + ", parent: " + node.getParent()); //$NON-NLS-1$ //$NON-NLS-2$
370
}
371     }
372
373 }
374
Popular Tags