KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > save > ModifiedTreeChecker


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.source.save;
20
21 import org.netbeans.modules.java.source.builder.CommentHandlerService;
22 import org.netbeans.api.java.source.query.CommentHandler;
23 import org.netbeans.api.java.source.query.CommentSet;
24 import org.netbeans.api.java.source.query.Query;
25 import com.sun.tools.javac.code.*;
26 import com.sun.tools.javac.tree.JCTree;
27 import com.sun.tools.javac.tree.JCTree.*;
28 import com.sun.tools.javac.tree.TreeScanner;
29 import com.sun.tools.javac.util.*;
30
31 /**
32  * Scan a tree and report whether it has been transformed.
33  *
34  * TODO: remove this class once reattribution support is complete.
35  */

36 public class ModifiedTreeChecker extends TreeScanner {
37
38     boolean changed = false;
39     private Symbol rootPackage;
40     private CommentHandler comments;
41
42     public ModifiedTreeChecker(Context context) {
43     rootPackage = Symtab.instance(context).rootPackage;
44         comments = CommentHandlerService.instance(context);
45     }
46
47     public boolean isModified() {
48     return changed;
49     }
50
51     private boolean checkTree(JCTree tree) {
52     if (changed)
53         return false;
54     changed = (tree.pos == Query.NOPOS);
55         if (!changed && comments.hasComments(tree)) {
56             CommentSet cs = comments.getComments(tree);
57             changed = cs.hasChanges();
58         }
59     return !changed; // keep checking if not new...
60
}
61
62     private boolean checkSymbol(Symbol sym) {
63     if (changed)
64         return false;
65     changed = sym != null && symbolChanged(sym, rootPackage);
66     return !changed; // keep checking if not new...
67
}
68
69     /**
70      * Returns true if a symbol has been modified.
71      */

72     private static boolean symbolChanged(Symbol sym, Symbol rootPackage) {
73         if (sym instanceof Symbol.PackageSymbol) {
74             Symbol owner = sym.owner;
75             if (owner != null && owner != rootPackage)
76                 return symbolChanged(owner, rootPackage);
77         }
78         return false;
79     }
80
81     // Visitor methods -- don't call them directly.
82
public void visitTopLevel(JCCompilationUnit that) {
83     if (checkTree(that) && checkSymbol(that.packge))
84         super.visitTopLevel(that);
85     }
86
87     public void visitImport(JCImport that) {
88     if (checkTree(that))
89         super.visitImport(that);
90     }
91
92     public void visitClassDef(JCClassDecl that) {
93     if (checkTree(that) && checkSymbol(that.sym))
94         super.visitClassDef(that);
95     }
96     public void visitMethodDef(JCMethodDecl that) {
97     if (checkTree(that) && checkSymbol(that.sym))
98         super.visitMethodDef(that);
99     }
100     public void visitVarDef(JCVariableDecl that) {
101     if (checkTree(that) && checkSymbol(that.sym))
102         super.visitVarDef(that);
103     }
104     public void visitSkip(JCSkip that) {
105     if (checkTree(that))
106         super.visitSkip(that);
107     }
108     public void visitBlock(JCBlock that) {
109     if (checkTree(that))
110         super.visitBlock(that);
111     }
112     public void visitDoLoop(JCDoWhileLoop that) {
113     if (checkTree(that))
114         super.visitDoLoop(that);
115     }
116     public void visitWhileLoop(JCWhileLoop that) {
117     if (checkTree(that))
118         super.visitWhileLoop(that);
119     }
120     public void visitForLoop(JCForLoop that) {
121     if (checkTree(that))
122         super.visitForLoop(that);
123     }
124     public void visitLabelled(JCLabeledStatement that) {
125     if (checkTree(that))
126         super.visitLabelled(that);
127     }
128     public void visitSwitch(JCSwitch that) {
129     if (checkTree(that))
130         super.visitSwitch(that);
131     }
132     public void visitCase(JCCase that) {
133     if (checkTree(that))
134         super.visitCase(that);
135     }
136     public void visitSynchronized(JCSynchronized that) {
137     if (checkTree(that))
138         super.visitSynchronized(that);
139     }
140     public void visitTry(JCTry that) {
141     if (checkTree(that))
142         super.visitTry(that);
143     }
144     public void visitCatch(JCCatch that) {
145     if (checkTree(that))
146         super.visitCatch(that);
147     }
148     public void visitConditional(JCConditional that) {
149     if (checkTree(that))
150         super.visitConditional(that);
151     }
152     public void visitIf(JCIf that) {
153     if (checkTree(that))
154         super.visitIf(that);
155     }
156     public void visitExec(JCExpressionStatement that) {
157     if (checkTree(that))
158         super.visitExec(that);
159     }
160     public void visitBreak(JCBreak that) {
161     if (checkTree(that))
162         super.visitBreak(that);
163     }
164     public void visitContinue(JCContinue that) {
165     if (checkTree(that))
166         super.visitContinue(that);
167     }
168     public void visitReturn(JCReturn that) {
169     if (checkTree(that))
170         super.visitReturn(that);
171     }
172     public void visitThrow(JCThrow that) {
173     if (checkTree(that))
174         super.visitThrow(that);
175     }
176     public void visitAssert(JCAssert that) {
177     if (checkTree(that))
178         super.visitAssert(that);
179     }
180     public void visitApply(JCMethodInvocation that) {
181     if (checkTree(that))
182         super.visitApply(that);
183     }
184     public void visitNewClass(JCNewClass that) {
185     if (checkTree(that) && checkSymbol(that.constructor))
186         super.visitNewClass(that);
187     }
188     public void visitNewArray(JCNewArray that) {
189     if (checkTree(that))
190         super.visitNewArray(that);
191     }
192     public void visitParens(JCParens that) {
193     if (checkTree(that))
194         super.visitParens(that);
195     }
196     public void visitAssign(JCAssign that) {
197     if (checkTree(that))
198         super.visitAssign(that);
199     }
200     public void visitAssignop(JCAssignOp that) {
201     if (checkTree(that))
202         super.visitAssignop(that);
203     }
204     public void visitUnary(JCUnary that) {
205     if (checkTree(that))
206         super.visitUnary(that);
207     }
208     public void visitBinary(JCBinary that) {
209     if (checkTree(that))
210         super.visitBinary(that);
211     }
212     public void visitTypeCast(JCTypeCast that) {
213     if (checkTree(that))
214         super.visitTypeCast(that);
215     }
216     public void visitTypeTest(JCInstanceOf that) {
217     if (checkTree(that))
218         super.visitTypeTest(that);
219     }
220     public void visitIndexed(JCArrayAccess that) {
221     if (checkTree(that))
222         super.visitIndexed(that);
223     }
224     public void visitSelect(JCFieldAccess that) {
225     if (checkTree(that) && checkSymbol(that.sym))
226         super.visitSelect(that);
227     }
228     public void visitIdent(JCIdent that) {
229     if (checkTree(that) && checkSymbol(that.sym))
230         super.visitIdent(that);
231     }
232     public void visitLiteral(JCLiteral that) {
233     if (checkTree(that))
234         super.visitLiteral(that);
235     }
236     public void visitTypeIdent(JCPrimitiveTypeTree that) {
237     if (checkTree(that))
238         super.visitTypeIdent(that);
239     }
240     public void visitTypeArray(JCArrayTypeTree that) {
241     if (checkTree(that))
242         super.visitTypeArray(that);
243     }
244     public void visitTypeApply(JCTypeApply that) {
245     if (checkTree(that))
246         super.visitTypeApply(that);
247     }
248     public void visitTypeParameter(JCTypeParameter that) {
249     if (checkTree(that))
250         super.visitTypeParameter(that);
251     }
252     public void visitWildcard(JCWildcard that) {
253         if (checkTree(that))
254             super.visitWildcard(that);
255     }
256     /*
257     public void visitTypeBoundKind(TypeBoundKind that) {
258         if (checkTree(that))
259             super.visitTypeBoundKind(that);
260     }
261      */

262     public void visitErroneous(JCErroneous that) {
263     if (checkTree(that))
264         super.visitErroneous(that);
265     }
266 }
267
Popular Tags