KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > code > AstMatchingNodeFinder


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

11 package org.eclipse.jdt.internal.corext.refactoring.code;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15
16 import org.eclipse.jdt.core.dom.ASTMatcher;
17 import org.eclipse.jdt.core.dom.ASTNode;
18 import org.eclipse.jdt.core.dom.ASTVisitor;
19 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
20 import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
21 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
22 import org.eclipse.jdt.core.dom.ArrayAccess;
23 import org.eclipse.jdt.core.dom.ArrayCreation;
24 import org.eclipse.jdt.core.dom.ArrayInitializer;
25 import org.eclipse.jdt.core.dom.ArrayType;
26 import org.eclipse.jdt.core.dom.AssertStatement;
27 import org.eclipse.jdt.core.dom.Assignment;
28 import org.eclipse.jdt.core.dom.Block;
29 import org.eclipse.jdt.core.dom.BlockComment;
30 import org.eclipse.jdt.core.dom.BooleanLiteral;
31 import org.eclipse.jdt.core.dom.BreakStatement;
32 import org.eclipse.jdt.core.dom.CastExpression;
33 import org.eclipse.jdt.core.dom.CatchClause;
34 import org.eclipse.jdt.core.dom.CharacterLiteral;
35 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
36 import org.eclipse.jdt.core.dom.CompilationUnit;
37 import org.eclipse.jdt.core.dom.ConditionalExpression;
38 import org.eclipse.jdt.core.dom.ConstructorInvocation;
39 import org.eclipse.jdt.core.dom.ContinueStatement;
40 import org.eclipse.jdt.core.dom.DoStatement;
41 import org.eclipse.jdt.core.dom.EmptyStatement;
42 import org.eclipse.jdt.core.dom.EnhancedForStatement;
43 import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
44 import org.eclipse.jdt.core.dom.EnumDeclaration;
45 import org.eclipse.jdt.core.dom.ExpressionStatement;
46 import org.eclipse.jdt.core.dom.FieldAccess;
47 import org.eclipse.jdt.core.dom.FieldDeclaration;
48 import org.eclipse.jdt.core.dom.ForStatement;
49 import org.eclipse.jdt.core.dom.IfStatement;
50 import org.eclipse.jdt.core.dom.ImportDeclaration;
51 import org.eclipse.jdt.core.dom.InfixExpression;
52 import org.eclipse.jdt.core.dom.Initializer;
53 import org.eclipse.jdt.core.dom.InstanceofExpression;
54 import org.eclipse.jdt.core.dom.Javadoc;
55 import org.eclipse.jdt.core.dom.LabeledStatement;
56 import org.eclipse.jdt.core.dom.LineComment;
57 import org.eclipse.jdt.core.dom.MarkerAnnotation;
58 import org.eclipse.jdt.core.dom.MemberRef;
59 import org.eclipse.jdt.core.dom.MemberValuePair;
60 import org.eclipse.jdt.core.dom.MethodDeclaration;
61 import org.eclipse.jdt.core.dom.MethodInvocation;
62 import org.eclipse.jdt.core.dom.MethodRef;
63 import org.eclipse.jdt.core.dom.MethodRefParameter;
64 import org.eclipse.jdt.core.dom.Modifier;
65 import org.eclipse.jdt.core.dom.NormalAnnotation;
66 import org.eclipse.jdt.core.dom.NullLiteral;
67 import org.eclipse.jdt.core.dom.NumberLiteral;
68 import org.eclipse.jdt.core.dom.PackageDeclaration;
69 import org.eclipse.jdt.core.dom.ParameterizedType;
70 import org.eclipse.jdt.core.dom.ParenthesizedExpression;
71 import org.eclipse.jdt.core.dom.PostfixExpression;
72 import org.eclipse.jdt.core.dom.PrefixExpression;
73 import org.eclipse.jdt.core.dom.PrimitiveType;
74 import org.eclipse.jdt.core.dom.QualifiedName;
75 import org.eclipse.jdt.core.dom.QualifiedType;
76 import org.eclipse.jdt.core.dom.ReturnStatement;
77 import org.eclipse.jdt.core.dom.SimpleName;
78 import org.eclipse.jdt.core.dom.SimpleType;
79 import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
80 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
81 import org.eclipse.jdt.core.dom.StringLiteral;
82 import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
83 import org.eclipse.jdt.core.dom.SuperFieldAccess;
84 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
85 import org.eclipse.jdt.core.dom.SwitchCase;
86 import org.eclipse.jdt.core.dom.SwitchStatement;
87 import org.eclipse.jdt.core.dom.SynchronizedStatement;
88 import org.eclipse.jdt.core.dom.TagElement;
89 import org.eclipse.jdt.core.dom.TextElement;
90 import org.eclipse.jdt.core.dom.ThisExpression;
91 import org.eclipse.jdt.core.dom.ThrowStatement;
92 import org.eclipse.jdt.core.dom.TryStatement;
93 import org.eclipse.jdt.core.dom.TypeDeclaration;
94 import org.eclipse.jdt.core.dom.TypeDeclarationStatement;
95 import org.eclipse.jdt.core.dom.TypeLiteral;
96 import org.eclipse.jdt.core.dom.TypeParameter;
97 import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
98 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
99 import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
100 import org.eclipse.jdt.core.dom.WhileStatement;
101 import org.eclipse.jdt.core.dom.WildcardType;
102
103 import org.eclipse.jdt.internal.corext.dom.*;
104
105 class AstMatchingNodeFinder {
106     
107     private AstMatchingNodeFinder(){
108     }
109     
110     public static ASTNode[] findMatchingNodes(ASTNode scope, ASTNode node){
111         Visitor visitor= new Visitor(node);
112         scope.accept(visitor);
113         return visitor.getMatchingNodes();
114     }
115     
116     private static class Visitor extends ASTVisitor{
117         
118         Collection JavaDoc fFound;
119         ASTMatcher fMatcher;
120         ASTNode fNodeToMatch;
121         
122         Visitor(ASTNode nodeToMatch){
123             fNodeToMatch= nodeToMatch;
124             fFound= new ArrayList JavaDoc();
125             fMatcher= new JdtASTMatcher();
126         }
127         
128         ASTNode[] getMatchingNodes(){
129             return (ASTNode[]) fFound.toArray(new ASTNode[fFound.size()]);
130         }
131         
132         private boolean matches(ASTNode node){
133             fFound.add(node);
134             return false;
135         }
136         
137         public boolean visit(AnonymousClassDeclaration node) {
138             if (node.subtreeMatch(fMatcher, fNodeToMatch))
139                 return matches(node);
140             return super.visit(node);
141         }
142
143         public boolean visit(ArrayAccess node) {
144             if (node.subtreeMatch(fMatcher, fNodeToMatch))
145                 return matches(node);
146             return super.visit(node);
147         }
148
149         public boolean visit(ArrayCreation node) {
150             if (node.subtreeMatch(fMatcher, fNodeToMatch))
151                 return matches(node);
152             return super.visit(node);
153         }
154
155         public boolean visit(ArrayInitializer node) {
156             if (node.subtreeMatch(fMatcher, fNodeToMatch))
157                 return matches(node);
158             return super.visit(node);
159         }
160
161         public boolean visit(ArrayType node) {
162             if (node.subtreeMatch(fMatcher, fNodeToMatch))
163                 return matches(node);
164             return super.visit(node);
165         }
166
167         public boolean visit(AssertStatement node) {
168             if (node.subtreeMatch(fMatcher, fNodeToMatch))
169                 return matches(node);
170             return super.visit(node);
171         }
172
173         public boolean visit(Assignment node) {
174             if (node.subtreeMatch(fMatcher, fNodeToMatch))
175                 return matches(node);
176             return super.visit(node);
177         }
178
179         public boolean visit(Block node) {
180             if (node.subtreeMatch(fMatcher, fNodeToMatch))
181                 return matches(node);
182             return super.visit(node);
183         }
184
185         public boolean visit(BooleanLiteral node) {
186             if (node.subtreeMatch(fMatcher, fNodeToMatch))
187                 return matches(node);
188             return super.visit(node);
189         }
190
191         public boolean visit(BreakStatement node) {
192             if (node.subtreeMatch(fMatcher, fNodeToMatch))
193                 return matches(node);
194             return super.visit(node);
195         }
196
197         public boolean visit(CastExpression node) {
198             if (node.subtreeMatch(fMatcher, fNodeToMatch))
199                 return matches(node);
200             return super.visit(node);
201         }
202
203         public boolean visit(CatchClause node) {
204             if (node.subtreeMatch(fMatcher, fNodeToMatch))
205                 return matches(node);
206             return super.visit(node);
207         }
208
209         public boolean visit(CharacterLiteral node) {
210             if (node.subtreeMatch(fMatcher, fNodeToMatch))
211                 return matches(node);
212             return super.visit(node);
213         }
214
215         public boolean visit(ClassInstanceCreation node) {
216             if (node.subtreeMatch(fMatcher, fNodeToMatch))
217                 return matches(node);
218             return super.visit(node);
219         }
220
221         public boolean visit(CompilationUnit node) {
222             if (node.subtreeMatch(fMatcher, fNodeToMatch))
223                 return matches(node);
224             return super.visit(node);
225         }
226
227         public boolean visit(ConditionalExpression node) {
228             if (node.subtreeMatch(fMatcher, fNodeToMatch))
229                 return matches(node);
230             return super.visit(node);
231         }
232
233         public boolean visit(ConstructorInvocation node) {
234             if (node.subtreeMatch(fMatcher, fNodeToMatch))
235                 return matches(node);
236             return super.visit(node);
237         }
238
239         public boolean visit(ContinueStatement node) {
240             if (node.subtreeMatch(fMatcher, fNodeToMatch))
241                 return matches(node);
242             return super.visit(node);
243         }
244
245         public boolean visit(DoStatement node) {
246             if (node.subtreeMatch(fMatcher, fNodeToMatch))
247                 return matches(node);
248             return super.visit(node);
249         }
250
251         public boolean visit(EmptyStatement node) {
252             if (node.subtreeMatch(fMatcher, fNodeToMatch))
253                 return matches(node);
254             return super.visit(node);
255         }
256
257         public boolean visit(ExpressionStatement node) {
258             if (node.subtreeMatch(fMatcher, fNodeToMatch))
259                 return matches(node);
260             return super.visit(node);
261         }
262
263         public boolean visit(FieldAccess node) {
264             if (node.subtreeMatch(fMatcher, fNodeToMatch))
265                 return matches(node);
266             return super.visit(node);
267         }
268
269         public boolean visit(FieldDeclaration node) {
270             if (node.subtreeMatch(fMatcher, fNodeToMatch))
271                 return matches(node);
272             return super.visit(node);
273         }
274
275         public boolean visit(ForStatement node) {
276             if (node.subtreeMatch(fMatcher, fNodeToMatch))
277                 return matches(node);
278             return super.visit(node);
279         }
280
281         public boolean visit(IfStatement node) {
282             if (node.subtreeMatch(fMatcher, fNodeToMatch))
283                 return matches(node);
284             return super.visit(node);
285         }
286
287         public boolean visit(ImportDeclaration node) {
288             if (node.subtreeMatch(fMatcher, fNodeToMatch))
289                 return matches(node);
290             return super.visit(node);
291         }
292
293         public boolean visit(InfixExpression node) {
294             if (node.subtreeMatch(fMatcher, fNodeToMatch))
295                 return matches(node);
296             return super.visit(node);
297         }
298
299         public boolean visit(Initializer node) {
300             if (node.subtreeMatch(fMatcher, fNodeToMatch))
301                 return matches(node);
302             return super.visit(node);
303         }
304
305         public boolean visit(InstanceofExpression node) {
306             if (node.subtreeMatch(fMatcher, fNodeToMatch))
307                 return matches(node);
308             return super.visit(node);
309         }
310
311         public boolean visit(Javadoc node) {
312             if (node.subtreeMatch(fMatcher, fNodeToMatch))
313                 return matches(node);
314             return super.visit(node);
315         }
316
317         public boolean visit(LabeledStatement node) {
318             if (node.subtreeMatch(fMatcher, fNodeToMatch))
319                 return matches(node);
320             return super.visit(node);
321         }
322
323         public boolean visit(MethodDeclaration node) {
324             if (node.subtreeMatch(fMatcher, fNodeToMatch))
325                 return matches(node);
326             return super.visit(node);
327         }
328
329         public boolean visit(MethodInvocation node) {
330             if (node.subtreeMatch(fMatcher, fNodeToMatch))
331                 return matches(node);
332             return super.visit(node);
333         }
334
335         public boolean visit(NullLiteral node) {
336             if (node.subtreeMatch(fMatcher, fNodeToMatch))
337                 return matches(node);
338             return super.visit(node);
339         }
340
341         public boolean visit(NumberLiteral node) {
342             if (node.subtreeMatch(fMatcher, fNodeToMatch))
343                 return matches(node);
344             return super.visit(node);
345         }
346
347         public boolean visit(PackageDeclaration node) {
348             if (node.subtreeMatch(fMatcher, fNodeToMatch))
349                 return matches(node);
350             return super.visit(node);
351         }
352
353         public boolean visit(ParenthesizedExpression node) {
354             if (node.subtreeMatch(fMatcher, fNodeToMatch))
355                 return matches(node);
356             return super.visit(node);
357         }
358
359         public boolean visit(PostfixExpression node) {
360             if (node.subtreeMatch(fMatcher, fNodeToMatch))
361                 return matches(node);
362             return super.visit(node);
363         }
364
365         public boolean visit(PrefixExpression node) {
366             if (node.subtreeMatch(fMatcher, fNodeToMatch))
367                 return matches(node);
368             return super.visit(node);
369         }
370
371         public boolean visit(PrimitiveType node) {
372             if (node.subtreeMatch(fMatcher, fNodeToMatch))
373                 return matches(node);
374             return super.visit(node);
375         }
376
377         public boolean visit(QualifiedName node) {
378             if (node.subtreeMatch(fMatcher, fNodeToMatch))
379                 return matches(node);
380             return super.visit(node);
381         }
382
383         public boolean visit(ReturnStatement node) {
384             if (node.subtreeMatch(fMatcher, fNodeToMatch))
385                 return matches(node);
386             return super.visit(node);
387         }
388
389         public boolean visit(SimpleName node) {
390             if (node.subtreeMatch(fMatcher, fNodeToMatch))
391                 return matches(node);
392             return super.visit(node);
393         }
394
395         public boolean visit(SimpleType node) {
396             if (node.subtreeMatch(fMatcher, fNodeToMatch))
397                 return matches(node);
398             return super.visit(node);
399         }
400
401         public boolean visit(SingleVariableDeclaration node) {
402             if (node.subtreeMatch(fMatcher, fNodeToMatch))
403                 return matches(node);
404             return super.visit(node);
405         }
406
407         public boolean visit(StringLiteral node) {
408             if (node.subtreeMatch(fMatcher, fNodeToMatch))
409                 return matches(node);
410             return super.visit(node);
411         }
412
413         public boolean visit(SuperConstructorInvocation node) {
414             if (node.subtreeMatch(fMatcher, fNodeToMatch))
415                 return matches(node);
416             return super.visit(node);
417         }
418
419         public boolean visit(SuperFieldAccess node) {
420             if (node.subtreeMatch(fMatcher, fNodeToMatch))
421                 return matches(node);
422             return super.visit(node);
423         }
424
425         public boolean visit(SuperMethodInvocation node) {
426             if (node.subtreeMatch(fMatcher, fNodeToMatch))
427                 return matches(node);
428             return super.visit(node);
429         }
430
431         public boolean visit(SwitchCase node) {
432             if (node.subtreeMatch(fMatcher, fNodeToMatch))
433                 return matches(node);
434             return super.visit(node);
435         }
436
437         public boolean visit(SwitchStatement node) {
438             if (node.subtreeMatch(fMatcher, fNodeToMatch))
439                 return matches(node);
440             return super.visit(node);
441         }
442
443         public boolean visit(SynchronizedStatement node) {
444             if (node.subtreeMatch(fMatcher, fNodeToMatch))
445                 return matches(node);
446             return super.visit(node);
447         }
448
449         public boolean visit(ThisExpression node) {
450             if (node.subtreeMatch(fMatcher, fNodeToMatch))
451                 return matches(node);
452             return super.visit(node);
453         }
454
455         public boolean visit(ThrowStatement node) {
456             if (node.subtreeMatch(fMatcher, fNodeToMatch))
457                 return matches(node);
458             return super.visit(node);
459         }
460
461         public boolean visit(TryStatement node) {
462             if (node.subtreeMatch(fMatcher, fNodeToMatch))
463                 return matches(node);
464             return super.visit(node);
465         }
466
467         public boolean visit(TypeDeclaration node) {
468             if (node.subtreeMatch(fMatcher, fNodeToMatch))
469                 return matches(node);
470             return super.visit(node);
471         }
472
473         public boolean visit(TypeDeclarationStatement node) {
474             if (node.subtreeMatch(fMatcher, fNodeToMatch))
475                 return matches(node);
476             return super.visit(node);
477         }
478
479         public boolean visit(TypeLiteral node) {
480             if (node.subtreeMatch(fMatcher, fNodeToMatch))
481                 return matches(node);
482             return super.visit(node);
483         }
484
485         public boolean visit(VariableDeclarationExpression node) {
486             if (node.subtreeMatch(fMatcher, fNodeToMatch))
487                 return matches(node);
488             return super.visit(node);
489         }
490
491         public boolean visit(VariableDeclarationFragment node) {
492             if (node.subtreeMatch(fMatcher, fNodeToMatch))
493                 return matches(node);
494             return super.visit(node);
495         }
496
497         public boolean visit(VariableDeclarationStatement node) {
498             if (node.subtreeMatch(fMatcher, fNodeToMatch))
499                 return matches(node);
500             return super.visit(node);
501         }
502
503         public boolean visit(WhileStatement node) {
504             if (node.subtreeMatch(fMatcher, fNodeToMatch))
505                 return matches(node);
506             return super.visit(node);
507         }
508
509         public boolean visit(AnnotationTypeDeclaration node) {
510             if (node.subtreeMatch(fMatcher, fNodeToMatch))
511                 return matches(node);
512             return super.visit(node);
513         }
514
515         public boolean visit(AnnotationTypeMemberDeclaration node) {
516             if (node.subtreeMatch(fMatcher, fNodeToMatch))
517                 return matches(node);
518             return super.visit(node);
519         }
520
521         public boolean visit(BlockComment node) {
522             if (node.subtreeMatch(fMatcher, fNodeToMatch))
523                 return matches(node);
524             return super.visit(node);
525         }
526
527         public boolean visit(EnhancedForStatement node) {
528             if (node.subtreeMatch(fMatcher, fNodeToMatch))
529                 return matches(node);
530             return super.visit(node);
531         }
532
533         public boolean visit(EnumConstantDeclaration node) {
534             if (node.subtreeMatch(fMatcher, fNodeToMatch))
535                 return matches(node);
536             return super.visit(node);
537         }
538
539         public boolean visit(EnumDeclaration node) {
540             if (node.subtreeMatch(fMatcher, fNodeToMatch))
541                 return matches(node);
542             return super.visit(node);
543         }
544
545         public boolean visit(LineComment node) {
546             if (node.subtreeMatch(fMatcher, fNodeToMatch))
547                 return matches(node);
548             return super.visit(node);
549         }
550
551         public boolean visit(MarkerAnnotation node) {
552             if (node.subtreeMatch(fMatcher, fNodeToMatch))
553                 return matches(node);
554             return super.visit(node);
555         }
556
557         public boolean visit(MemberRef node) {
558             if (node.subtreeMatch(fMatcher, fNodeToMatch))
559                 return matches(node);
560             return super.visit(node);
561         }
562
563         public boolean visit(MemberValuePair node) {
564             if (node.subtreeMatch(fMatcher, fNodeToMatch))
565                 return matches(node);
566             return super.visit(node);
567         }
568
569         public boolean visit(MethodRef node) {
570             if (node.subtreeMatch(fMatcher, fNodeToMatch))
571                 return matches(node);
572             return super.visit(node);
573         }
574
575         public boolean visit(MethodRefParameter node) {
576             if (node.subtreeMatch(fMatcher, fNodeToMatch))
577                 return matches(node);
578             return super.visit(node);
579         }
580
581         public boolean visit(Modifier node) {
582             if (node.subtreeMatch(fMatcher, fNodeToMatch))
583                 return matches(node);
584             return super.visit(node);
585         }
586
587         public boolean visit(NormalAnnotation node) {
588             if (node.subtreeMatch(fMatcher, fNodeToMatch))
589                 return matches(node);
590             return super.visit(node);
591         }
592
593         public boolean visit(ParameterizedType node) {
594             if (node.subtreeMatch(fMatcher, fNodeToMatch))
595                 return matches(node);
596             return super.visit(node);
597         }
598
599         public boolean visit(QualifiedType node) {
600             if (node.subtreeMatch(fMatcher, fNodeToMatch))
601                 return matches(node);
602             return super.visit(node);
603         }
604
605         public boolean visit(SingleMemberAnnotation node) {
606             if (node.subtreeMatch(fMatcher, fNodeToMatch))
607                 return matches(node);
608             return super.visit(node);
609         }
610
611         public boolean visit(TagElement node) {
612             if (node.subtreeMatch(fMatcher, fNodeToMatch))
613                 return matches(node);
614             return super.visit(node);
615         }
616
617         public boolean visit(TextElement node) {
618             if (node.subtreeMatch(fMatcher, fNodeToMatch))
619                 return matches(node);
620             return super.visit(node);
621         }
622
623         public boolean visit(TypeParameter node) {
624             if (node.subtreeMatch(fMatcher, fNodeToMatch))
625                 return matches(node);
626             return super.visit(node);
627         }
628
629         public boolean visit(WildcardType node) {
630             if (node.subtreeMatch(fMatcher, fNodeToMatch))
631                 return matches(node);
632             return super.visit(node);
633         }
634     }
635 }
636
Popular Tags