KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.java.source.query.QueryEnvironment;
23 import org.netbeans.api.java.source.query.SearchEntry;
24 import org.netbeans.api.java.source.query.SearchResult;
25
26 import com.sun.source.tree.*;
27 import javax.lang.model.element.Element;
28 import javax.lang.model.element.PackageElement;
29 import javax.lang.model.element.TypeElement;
30
31 /**
32  * A NodeScanner that locates all references to a specified Element. A
33  * SearchResult is returned with an entry for each reference, along with
34  * a set of flags detailing whether the reference reads (gets) or writes
35  * (sets) the Element, as well as the access level of the read and/or write.
36  */

37 public class UseFinder extends NodeScanner<Void JavaDoc,Object JavaDoc> {
38     public static final int SETUSE = 1;
39     public static final int GETUSE = 2;
40     public static final int CLASSSHIFT = 4;
41     public static final int PACKAGESHIFT = 2;
42     public static final int WORLDSHIFT = 0;
43     public static final int DECLARATION = 1 << 6;
44     
45     public UseFinder(QueryEnvironment env) {
46         attach(env);
47     }
48     
49     private Element target;
50     private SearchResult results;
51     private PackageElement homePackage;
52     private TypeElement homeClass;
53
54     /**
55      * Locates all references to an Element within a Tree scope. To locate
56      * all references to an Element within the source model, use the root node
57      * as the scope.
58      *
59      * @see org.netbeans.modules.java.source.model.ASTModel#getRoot
60      */

61     public SearchResult find(Element e, Tree scope) {
62         if (e == null)
63             return null;
64         target = e;
65         homePackage = env.getElementUtilities().packageElement(e);
66         homeClass = env.getElementUtilities().enclosingTypeElement(e);
67         results = new SearchResult(null, "Uses of " + e);
68         if (scope != null) {
69             results.attach(env);
70             scope.accept(this, null);
71             //results.release(env); // enable when async results are supported
72
}
73         return results;
74     }
75     
76     private static final String JavaDoc[] sgtable = new String JavaDoc[128];
77     
78     public static String JavaDoc getMsg(int flags) {
79         String JavaDoc msg = sgtable[flags];
80         if (msg == null) {
81             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
82             sb.append((flags & DECLARATION) == 0 ? '-'
83                                                    : 'd');
84             for (int i = 6; --i >= 0; )
85                 sb.append((flags & (1 << i)) == 0 ? '-'
86                         : (i & 1) == 0 ? 'w'
87                                                     : 'r');
88             msg = sb.toString();
89             sgtable[flags] = msg;
90         }
91         return msg;
92     }
93     
94     private void add(Tree t, int usage) {
95         if (currentSym == null) {
96             results.add(new SearchEntry(null, null, t, model.getPos(t), "No currentSym", 0));
97             return;
98         }
99         TypeElement here = env.getElementUtilities().enclosingTypeElement(currentSym);
100         PackageElement phere = env.getElementUtilities().packageElement(here);
101         int flags = usage <<
102                     (here == homeClass ? CLASSSHIFT
103                    : phere == homePackage ? PACKAGESHIFT
104                                           : WORLDSHIFT);
105         results.add(new SearchEntry(null, currentSym, t, model.getPos(t), getMsg(flags), flags));
106         return;
107     }
108     private boolean isTarget(Tree tree) {
109         return (tree instanceof MemberSelectTree ||
110                 tree instanceof IdentifierTree) &&
111                model.getElement(tree) == target;
112     }
113     
114     @Override JavaDoc
115     public Void JavaDoc visitAssignment(AssignmentTree tree, Object JavaDoc p) {
116         if (tree.getVariable() instanceof MemberSelectTree) {
117             MemberSelectTree t = (MemberSelectTree)tree.getVariable();
118             scan(t.getExpression(), p);
119             if (model.getElement(t) == target)
120                 add(tree, SETUSE);
121         } else if (tree.getVariable() instanceof IdentifierTree) {
122             if (model.getElement(tree.getVariable()) == target)
123                 add(tree, SETUSE);
124         } else
125             scan(tree.getVariable(), p);
126         scan(tree.getExpression(), p);
127         return null;
128     }
129     
130     @Override JavaDoc
131     public Void JavaDoc visitCompoundAssignment(CompoundAssignmentTree tree, Object JavaDoc p) {
132         if (tree.getVariable() instanceof MemberSelectTree) {
133             MemberSelectTree t = (MemberSelectTree)tree.getVariable();
134             scan(t.getExpression(), p);
135             if (model.getElement(t) == target)
136                 add(tree, SETUSE | GETUSE);
137         } else if (tree.getVariable() instanceof IdentifierTree) {
138             if (model.getElement(tree.getVariable()) == target)
139                 add(tree, SETUSE | GETUSE);
140         } else
141             scan(tree.getVariable(), p);
142         scan(tree.getExpression(), p);
143         return null;
144     }
145     
146     @Override JavaDoc
147     public Void JavaDoc visitUnary(UnaryTree tree, Object JavaDoc p) {
148         switch (tree.getKind()) {
149           case PREFIX_INCREMENT:
150           case PREFIX_DECREMENT:
151           case POSTFIX_INCREMENT:
152           case POSTFIX_DECREMENT:
153             if (tree.getExpression() instanceof MemberSelectTree) {
154                 MemberSelectTree t = (MemberSelectTree)tree.getExpression();
155                 scan(t.getExpression(), p);
156                 if (model.getElement(t) == target)
157                     add(tree, SETUSE | GETUSE);
158             } else if (tree.getExpression() instanceof IdentifierTree) {
159                 if (model.getElement(tree.getExpression()) == target)
160                     add(tree, SETUSE | GETUSE);
161             } else
162                 scan(tree.getExpression(), p);
163             break;
164           default:
165             scan(tree.getExpression(), p);
166         }
167         return null;
168     }
169     
170     @Override JavaDoc
171     public Void JavaDoc visitIdentifier(IdentifierTree tree, Object JavaDoc p) {
172         if (model.getElement(tree) == target)
173             add(tree, GETUSE);
174         return super.visitIdentifier(tree, p);
175     }
176     
177     @Override JavaDoc
178     public Void JavaDoc visitVariable(VariableTree tree, Object JavaDoc p) {
179         if (model.getElement(tree) == target) {
180             int flags = DECLARATION;
181             if (tree.getInitializer() != null)
182                 flags |= SETUSE << CLASSSHIFT;
183             results.add(new SearchEntry(null, currentSym, tree, model.getPos(tree), getMsg(flags), flags));
184         }
185         scan(tree.getInitializer(), p);
186         return super.visitVariable(tree, p);
187     }
188     
189     @Override JavaDoc
190     public Void JavaDoc visitMemberSelect(MemberSelectTree tree, Object JavaDoc p) {
191         if (model.getElement(tree) == target)
192             add(tree, GETUSE);
193         return super.visitMemberSelect(tree, p);
194     }
195 }
196
Popular Tags