KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > query > TreeMatcher


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.source.query;
21
22 import org.netbeans.modules.java.source.engine.EngineEnvironment;
23 import com.sun.source.tree.*;
24 import java.util.List JavaDoc;
25 import org.netbeans.modules.java.source.engine.ASTModel;
26 import org.netbeans.modules.java.source.engine.EngineEnvironment;
27 import org.netbeans.api.java.source.transform.Transformer;
28
29 /**
30  * Determines whether two trees deeply match each other, using a fast-fail algorithm.
31  */

32 public class TreeMatcher implements TreeVisitor<Void JavaDoc,Tree> {
33     protected boolean matches;
34     protected ASTModel model;
35     
36     public TreeMatcher(QueryEnvironment env) {
37         this.model = ((EngineEnvironment)env).getModel();
38     }
39
40     public boolean matches(Tree a, Tree b) {
41         matches = true;
42     match(a, b);
43     return matches;
44     }
45
46     public boolean matches(List JavaDoc<? extends Tree> a, List JavaDoc<? extends Tree> b) {
47         matches = true;
48         int n = a.size();
49         if (n != b.size())
50             return false;
51         for (int i = 0; i < n; i++) {
52             if (!matches(a.get(i), b.get(i)))
53                 return false;
54         }
55     return true;
56     }
57
58     /** Visitor method: Scan a single tree.
59      */

60     private void match(Tree tree, Tree other) {
61     if (matches)
62         if((tree = Transformer.deblock(tree)) != null &&
63                (other = Transformer.deblock(other)) != null)
64         if(tree.getKind() != other.getKind())
65             matches = false;
66         else {
67             tree.accept(this, other);
68         }
69         else if(other != tree)
70                 matches = false;
71     }
72
73     /** Visitor method: scan a list of trees.
74      */

75     private void match(List JavaDoc<? extends Tree> a, List JavaDoc<? extends Tree> b) {
76         if (!matches)
77             return;
78         if (a == null && b == null)
79             return;
80         if (a == null || b == null) {
81             matches = false;
82             return;
83         }
84         int n = a.size();
85         if (n != b.size()) {
86             matches = false;
87             return;
88         }
89         for (int i = 0; i < n && matches; i++)
90         match(a.get(i), b.get(i));
91     }
92     
93     private void match(CharSequence JavaDoc s1, CharSequence JavaDoc s2) {
94         matches = matches && (s1 == null && s2 == null) || (s1 != null && s1.equals(s2));
95     }
96
97 /****************************************************************************
98  * Visitor methods
99  ****************************************************************************/

100
101     public Void JavaDoc visitCompilationUnit(CompilationUnitTree tree, Tree p) {
102         CompilationUnitTree other = (CompilationUnitTree)p;
103     match(tree.getPackageAnnotations(), other.getPackageAnnotations());
104         match(tree.getPackageName(), other.getPackageName());
105         match(tree.getImports(), other.getImports());
106         match(tree.getTypeDecls(), other.getTypeDecls());
107         return null;
108     }
109
110     public Void JavaDoc visitImport(ImportTree tree, Tree p) {
111         ImportTree other = (ImportTree)p;
112         match(tree.getQualifiedIdentifier(), other.getQualifiedIdentifier());
113         return null;
114     }
115
116     public Void JavaDoc visitClass(ClassTree tree, Tree p) {
117         ClassTree other = (ClassTree)p;
118         match(tree.getSimpleName(), other.getSimpleName());
119         match(tree.getModifiers(), other.getModifiers());
120         match(tree.getTypeParameters(), other.getTypeParameters());
121         match(tree.getExtendsClause(), other.getExtendsClause());
122         match(tree.getImplementsClause(), other.getImplementsClause());
123         match(tree.getMembers(), other.getMembers());
124         return null;
125     }
126
127     public Void JavaDoc visitMethod(MethodTree tree, Tree p) {
128         MethodTree other = (MethodTree)p;
129         match(tree.getName(), other.getName());
130         match(tree.getModifiers(), other.getModifiers());
131         match(tree.getTypeParameters(), other.getTypeParameters());
132         match(tree.getParameters(), other.getParameters());
133         match(tree.getReturnType(), other.getReturnType());
134         match(tree.getThrows(), other.getThrows());
135         match(tree.getDefaultValue(), other.getDefaultValue());
136         match(tree.getBody(), other.getBody());
137         return null;
138     }
139
140     public Void JavaDoc visitVariable(VariableTree tree, Tree p) {
141         VariableTree other = (VariableTree)p;
142         match(tree.getName(), other.getName());
143         match(tree.getModifiers(), other.getModifiers());
144         match(tree.getType(), other.getType());
145         match(tree.getInitializer(), other.getInitializer());
146         return null;
147     }
148
149     public Void JavaDoc visitAnnotation(AnnotationTree tree, Tree p) {
150         AnnotationTree other = (AnnotationTree)p;
151         match(tree.getAnnotationType(), other.getAnnotationType());
152         match(tree.getArguments(), other.getArguments());
153         return null;
154     }
155
156     public Void JavaDoc visitMethodInvocation(MethodInvocationTree tree, Tree p) {
157         MethodInvocationTree other = (MethodInvocationTree)p;
158         match(tree.getMethodSelect(), other.getMethodSelect());
159         match(tree.getTypeArguments(), other.getTypeArguments());
160         match(tree.getArguments(), other.getArguments());
161         return null;
162     }
163
164     public Void JavaDoc visitAssert(AssertTree tree, Tree p) {
165         AssertTree other = (AssertTree)p;
166         match(tree.getCondition(), other.getCondition());
167         match(tree.getDetail(), other.getDetail());
168         return null;
169     }
170
171     public Void JavaDoc visitAssignment(AssignmentTree tree, Tree p) {
172         AssignmentTree other = (AssignmentTree)p;
173         match(tree.getVariable(), other.getVariable());
174         match(tree.getExpression(), other.getExpression());
175         return null;
176     }
177
178     public Void JavaDoc visitCompoundAssignment(CompoundAssignmentTree tree, Tree p) {
179         CompoundAssignmentTree other = (CompoundAssignmentTree)p;
180         match(tree.getVariable(), other.getVariable());
181         match(tree.getExpression(), other.getExpression());
182         return null;
183     }
184
185     public Void JavaDoc visitBinary(BinaryTree tree, Tree p) {
186         BinaryTree other = (BinaryTree)p;
187         match(tree.getLeftOperand(), other.getLeftOperand());
188         match(tree.getRightOperand(), other.getRightOperand());
189         return null;
190     }
191
192     public Void JavaDoc visitBlock(BlockTree tree, Tree p) {
193         BlockTree other = (BlockTree)p;
194         match(tree.getStatements(), other.getStatements());
195         matches = matches && tree.isStatic() == other.isStatic();
196         return null;
197     }
198
199     public Void JavaDoc visitBreak(BreakTree tree, Tree p) {
200         BreakTree other = (BreakTree)p;
201         match(tree.getLabel(), other.getLabel());
202         return null;
203     }
204
205     public Void JavaDoc visitCase(CaseTree tree, Tree p) {
206         CaseTree other = (CaseTree)p;
207         match(tree.getExpression(), other.getExpression());
208         match(tree.getStatements(), other.getStatements());
209         return null;
210     }
211
212     public Void JavaDoc visitCatch(CatchTree tree, Tree p) {
213         CatchTree other = (CatchTree)p;
214         match(tree.getParameter(), other.getParameter());
215         match(tree.getBlock(), other.getBlock());
216         return null;
217     }
218
219     public Void JavaDoc visitConditionalExpression(ConditionalExpressionTree tree, Tree p) {
220         ConditionalExpressionTree other = (ConditionalExpressionTree)p;
221         match(tree.getCondition(), other.getCondition());
222         match(tree.getFalseExpression(), other.getFalseExpression());
223         match(tree.getTrueExpression(), other.getTrueExpression());
224         return null;
225     }
226
227     public Void JavaDoc visitContinue(ContinueTree tree, Tree p) {
228         ContinueTree other = (ContinueTree)p;
229         match(tree.getLabel(), other.getLabel());
230         return null;
231     }
232
233     public Void JavaDoc visitDoWhileLoop(DoWhileLoopTree tree, Tree p) {
234         DoWhileLoopTree other = (DoWhileLoopTree)p;
235         match(tree.getCondition(), other.getCondition());
236         match(tree.getStatement(), other.getStatement());
237         return null;
238     }
239
240     public Void JavaDoc visitErroneous(ErroneousTree tree, Tree p) {
241         return null;
242     }
243
244     public Void JavaDoc visitExpressionStatement(ExpressionStatementTree tree, Tree p) {
245         ExpressionStatementTree other = (ExpressionStatementTree)p;
246         match(tree.getExpression(), other.getExpression());
247         return null;
248     }
249
250     public Void JavaDoc visitEnhancedForLoop(EnhancedForLoopTree tree, Tree p) {
251         EnhancedForLoopTree other = (EnhancedForLoopTree)p;
252         match(tree.getVariable(), other.getVariable());
253         match(tree.getExpression(), other.getExpression());
254         match(tree.getStatement(), other.getStatement());
255         return null;
256     }
257
258     public Void JavaDoc visitForLoop(ForLoopTree tree, Tree p) {
259         ForLoopTree other = (ForLoopTree)p;
260         match(tree.getInitializer(), other.getInitializer());
261         match(tree.getCondition(), other.getCondition());
262         match(tree.getUpdate(), other.getUpdate());
263         match(tree.getStatement(), other.getStatement());
264         return null;
265     }
266
267     public Void JavaDoc visitIdentifier(IdentifierTree tree, Tree p) {
268         IdentifierTree other = (IdentifierTree)p;
269         match(tree.getName(), other.getName());
270         return null;
271     }
272
273     public Void JavaDoc visitIf(IfTree tree, Tree p) {
274         IfTree other = (IfTree)p;
275         match(tree.getCondition(), other.getCondition());
276         match(tree.getThenStatement(), other.getThenStatement());
277         match(tree.getElseStatement(), other.getElseStatement());
278         return null;
279     }
280
281     public Void JavaDoc visitArrayAccess(ArrayAccessTree tree, Tree p) {
282         ArrayAccessTree other = (ArrayAccessTree)p;
283         match(tree.getExpression(), other.getExpression());
284         match(tree.getIndex(), other.getIndex());
285         return null;
286     }
287
288     public Void JavaDoc visitLabeledStatement(LabeledStatementTree tree, Tree p) {
289         LabeledStatementTree other = (LabeledStatementTree)p;
290         match(tree.getLabel(), other.getLabel());
291         match(tree.getStatement(), other.getStatement());
292         return null;
293     }
294
295     public Void JavaDoc visitLiteral(LiteralTree tree, Tree p) {
296         LiteralTree other = (LiteralTree)p;
297         Object JavaDoc v1 = tree.getValue();
298         Object JavaDoc v2 = other.getValue();
299         matches = matches &&
300                 (v1 == null && v2 == null || v1 != null && v1.equals(v2));
301         return null;
302     }
303
304     public Void JavaDoc visitModifiers(ModifiersTree tree, Tree p) {
305         ModifiersTree other = (ModifiersTree)p;
306         matches = matches && tree.getFlags().equals(other.getFlags());
307         match(tree.getAnnotations(), other.getAnnotations());
308         return null;
309     }
310
311     public Void JavaDoc visitNewArray(NewArrayTree tree, Tree p) {
312         NewArrayTree other = (NewArrayTree)p;
313         match(tree.getType(), other.getType());
314         match(tree.getDimensions(), other.getDimensions());
315         match(tree.getInitializers(), other.getInitializers());
316         return null;
317     }
318
319     public Void JavaDoc visitNewClass(NewClassTree tree, Tree p) {
320         NewClassTree other = (NewClassTree)p;
321         match(tree.getIdentifier(), other.getIdentifier());
322         match(tree.getTypeArguments(), other.getTypeArguments());
323         match(tree.getArguments(), other.getArguments());
324         match(tree.getClassBody(), other.getClassBody());
325         match(tree.getEnclosingExpression(), other.getEnclosingExpression());
326         return null;
327     }
328
329     public Void JavaDoc visitParenthesized(ParenthesizedTree tree, Tree p) {
330         ParenthesizedTree other = (ParenthesizedTree)p;
331         match(tree.getExpression(), other.getExpression());
332         return null;
333     }
334
335     public Void JavaDoc visitReturn(ReturnTree tree, Tree p) {
336         ReturnTree other = (ReturnTree)p;
337         match(tree.getExpression(), other.getExpression());
338         return null;
339     }
340
341     public Void JavaDoc visitMemberSelect(MemberSelectTree tree, Tree p) {
342         MemberSelectTree other = (MemberSelectTree)p;
343         match(tree.getIdentifier(), other.getIdentifier());
344         match(tree.getExpression(), other.getExpression());
345         return null;
346     }
347
348     public Void JavaDoc visitEmptyStatement(EmptyStatementTree tree, Tree p) {
349         return null;
350     }
351
352     public Void JavaDoc visitSwitch(SwitchTree tree, Tree p) {
353         SwitchTree other = (SwitchTree)p;
354         match(tree.getCases(), other.getCases());
355         match(tree.getExpression(), other.getExpression());
356         return null;
357     }
358
359     public Void JavaDoc visitSynchronized(SynchronizedTree tree, Tree p) {
360         SynchronizedTree other = (SynchronizedTree)p;
361         match(tree.getBlock(), other.getBlock());
362         match(tree.getExpression(), other.getExpression());
363         return null;
364     }
365
366     public Void JavaDoc visitThrow(ThrowTree tree, Tree p) {
367         ThrowTree other = (ThrowTree)p;
368         match(tree.getExpression(), other.getExpression());
369         return null;
370     }
371
372     public Void JavaDoc visitTry(TryTree tree, Tree p) {
373         TryTree other = (TryTree)p;
374         match(tree.getBlock(), other.getBlock());
375         match(tree.getCatches(), other.getCatches());
376         match(tree.getFinallyBlock(), other.getFinallyBlock());
377         return null;
378     }
379
380     public Void JavaDoc visitParameterizedType(ParameterizedTypeTree tree, Tree p) {
381         ParameterizedTypeTree other = (ParameterizedTypeTree)p;
382         match(tree.getType(), other.getType());
383         match(tree.getTypeArguments(), other.getTypeArguments());
384         return null;
385     }
386
387     public Void JavaDoc visitArrayType(ArrayTypeTree tree, Tree p) {
388         ArrayTypeTree other = (ArrayTypeTree)p;
389         match(tree.getType(), other.getType());
390         return null;
391     }
392
393     public Void JavaDoc visitTypeCast(TypeCastTree tree, Tree p) {
394         TypeCastTree other = (TypeCastTree)p;
395         match(tree.getType(), other.getType());
396         match(tree.getExpression(), other.getExpression());
397         return null;
398     }
399
400     public Void JavaDoc visitPrimitiveType(PrimitiveTypeTree tree, Tree p) {
401         PrimitiveTypeTree other = (PrimitiveTypeTree)p;
402         matches = matches &&
403             tree.getPrimitiveTypeKind() == other.getPrimitiveTypeKind();
404         return null;
405     }
406
407     public Void JavaDoc visitTypeParameter(TypeParameterTree tree, Tree p) {
408         TypeParameterTree other = (TypeParameterTree)p;
409         match(tree.getName(), other.getName());
410         match(tree.getBounds(), other.getBounds());
411         return null;
412     }
413
414     public Void JavaDoc visitInstanceOf(InstanceOfTree tree, Tree p) {
415         InstanceOfTree other = (InstanceOfTree)p;
416         match(tree.getType(), other.getType());
417         match(tree.getExpression(), other.getExpression());
418         return null;
419     }
420
421     public Void JavaDoc visitUnary(UnaryTree tree, Tree p) {
422         UnaryTree other = (UnaryTree)p;
423         match(tree.getExpression(), other.getExpression());
424         return null;
425     }
426
427     public Void JavaDoc visitWhileLoop(WhileLoopTree tree, Tree p) {
428         WhileLoopTree other = (WhileLoopTree)p;
429         match(tree.getCondition(), other.getCondition());
430         match(tree.getStatement(), other.getStatement());
431         return null;
432     }
433
434     public Void JavaDoc visitWildcard(WildcardTree tree, Tree p) {
435         WildcardTree other = (WildcardTree)p;
436         match(tree.getBound(), other.getBound());
437         return null;
438     }
439
440     public Void JavaDoc visitOther(Tree tree, Tree p) {
441         return null;
442     }
443 }
444
Popular Tags