KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
32
33 /**
34  * A NodeScanner that creates a map of references to each SearchEntry in
35  * a specified SearchResult list. This scanner is used as the second of
36  * two-stage searches; the FindUnusedVariables query, for example, first
37  * creates a list of all variables in a tree using Finder, then SetUseFinder
38  * is invoked to get access information about each variable in that list.
39  *
40  * @see org.netbeans.api.java.source.query.Finder
41  * @see org.netbeans.api.java.source.query.UseFinder
42  */

43 public class SetUseFinder extends NodeScanner<Void JavaDoc,Object JavaDoc> {
44     private Map<Element,SearchInfo> target;
45     
46     public SetUseFinder(QueryEnvironment env) {
47         attach(env);
48     }
49     private static class SearchInfo {
50         Element target;
51         SearchResult results;
52         PackageElement homePackage;
53         TypeElement homeClass;
54         int flags;
55         SearchInfo(QueryEnvironment env, Element e, boolean summary) {
56             target = e;
57             homePackage = env.getElementUtilities().packageElement(e);
58             homeClass = env.getElementUtilities().enclosingTypeElement(e);
59             if (!summary) {
60                 results = new SearchResult(null, "Uses of " + e);
61                 results.attach(env);
62             }
63         }
64     }
65     private void findInfo(SearchResult s, Tree scope, boolean summary) {
66         if (s == null)
67             return;
68         target = new HashMap<Element,SearchInfo>();
69         for (Iterator<SearchEntry> i = s.iterator(); i.hasNext(); ) {
70             Element sym = i.next().sym;
71             if (sym != null)
72                 target.put(sym, new SearchInfo(env, sym, summary));
73         }
74         if (scope != null)
75             scope.accept(this, null);
76     }
77     public Map<Element,SearchResult> findAll(SearchResult s, Tree scope) {
78         if (s == null)
79             return null;
80         findInfo(s, scope, false);
81         Map<Element,SearchResult> result = new HashMap<Element,SearchResult>();
82         for (Iterator<SearchInfo> i = target.values().iterator(); i.hasNext(); ) {
83             SearchInfo si = i.next();
84             result.put(si.target, si.results);
85         }
86         return result;
87     }
88     protected boolean acceptable(Element sym, int flags) {
89         return true;
90     }
91     public void computeSummary(SearchResult s, Tree scope) {
92         if (s == null)
93             return;
94         findInfo(s, scope, true);
95         for (Iterator<SearchEntry> i = s.iterator(); i.hasNext(); ) {
96             SearchEntry se = i.next();
97             Element sym = se.sym;
98             if (sym == null)
99                 i.remove();
100             else {
101                 SearchInfo si = target.get(sym);
102                 if (si != null && acceptable(sym, si.flags)) {
103                     se.flags = si.flags;
104                     se.note = UseFinder.getMsg(si.flags);
105                 } else
106                     i.remove();
107             }
108         }
109     }
110     private void add(Tree t, SearchInfo si, int usage) {
111         Element sym = currentSym;
112         if (sym == null) {
113             if (si.results != null)
114                 si.results.add(new SearchEntry(null, null, t, model.getPos(t), "No currentSym", 0));
115             return;
116         }
117         TypeElement here = env.getElementUtilities().enclosingTypeElement(currentSym);
118         PackageElement phere = env.getElementUtilities().packageElement(here);
119         int flags = usage <<
120                     (here == si.homeClass ? UseFinder.CLASSSHIFT
121                    : phere == si.homePackage ? UseFinder.PACKAGESHIFT
122                                              : UseFinder.WORLDSHIFT);
123         si.flags |= flags;
124         if (si.results != null)
125             si.results.add(new SearchEntry(null, sym, t, model.getPos(t),
126                                            UseFinder.getMsg(flags), flags));
127         return;
128     }
129     @Override JavaDoc
130     public Void JavaDoc visitAssignment(AssignmentTree tree, Object JavaDoc o) {
131         Tree lhs = tree.getVariable();
132         Tree rhs = tree.getExpression();
133         if (lhs instanceof MemberSelectTree) {
134             MemberSelectTree t = (MemberSelectTree)lhs;
135             scan(t.getExpression(), null);
136             SearchInfo si = target.get(model.getElement(t));
137             if (si != null)
138                 add(tree, si, UseFinder.SETUSE);
139         } else if (lhs instanceof IdentifierTree) {
140             SearchInfo si = target.get(model.getElement(lhs));
141             if (si != null)
142                 add(tree, si, UseFinder.SETUSE);
143         } else
144             scan(lhs, o);
145         scan(rhs, o);
146         return null;
147     }
148     @Override JavaDoc
149     public Void JavaDoc visitCompoundAssignment(CompoundAssignmentTree tree, Object JavaDoc o) {
150         Tree lhs = tree.getVariable();
151         Tree rhs = tree.getExpression();
152         if (lhs instanceof MemberSelectTree) {
153             MemberSelectTree t = (MemberSelectTree)lhs;
154             scan(t.getExpression(), null);
155             SearchInfo si = target.get(model.getElement(t));
156             if (si != null)
157                 add(tree, si, UseFinder.GETUSE | UseFinder.SETUSE);
158         } else if (lhs instanceof IdentifierTree) {
159             SearchInfo si = target.get(model.getElement(lhs));
160             if (si != null)
161                 add(tree, si, UseFinder.GETUSE | UseFinder.SETUSE);
162         } else
163             scan(lhs, o);
164         scan(rhs, o);
165         return null;
166     }
167     @Override JavaDoc
168     public Void JavaDoc visitUnary(UnaryTree tree, Object JavaDoc o) {
169         switch (tree.getKind()) {
170           case PREFIX_INCREMENT:
171           case PREFIX_DECREMENT:
172           case POSTFIX_INCREMENT:
173           case POSTFIX_DECREMENT:
174             if (tree.getExpression() instanceof MemberSelectTree) {
175                 MemberSelectTree t = (MemberSelectTree)tree.getExpression();
176                 scan(t.getExpression(), o);
177                 SearchInfo si = target.get(model.getElement(tree));
178                 if (si != null)
179                     add(tree, si, UseFinder.GETUSE | UseFinder.SETUSE);
180             } else if (tree.getExpression() instanceof IdentifierTree) {
181                 SearchInfo si = target.get(model.getElement(tree.getExpression()));
182                 if (si != null)
183                     add(tree, si, UseFinder.GETUSE | UseFinder.SETUSE);
184             } else
185                 scan(tree.getExpression(), o);
186             break;
187           default:
188             scan(tree.getExpression(), o);
189         }
190         return null;
191     }
192     public Void JavaDoc visitIdentifier(IdentifierTree tree, Object JavaDoc o) {
193         super.visitIdentifier(tree, o);
194         SearchInfo si = target.get(model.getElement(tree));
195         if (si != null)
196             add(tree, si, UseFinder.GETUSE);
197         return null;
198     }
199     public Void JavaDoc visitVariable(VariableTree tree, Object JavaDoc o) {
200         scan(tree.getInitializer(), o);
201         SearchInfo si = target.get(model.getElement(tree));
202         if (si != null) {
203             int flags = UseFinder.DECLARATION;
204             if (tree.getInitializer() != null)
205                 flags |= UseFinder.SETUSE << UseFinder.CLASSSHIFT;
206             si.flags |= flags;
207             if (si.results != null)
208                 si.results.add(new SearchEntry(null, currentSym, tree, model.getPos(tree),
209                                                UseFinder.getMsg(flags), flags));
210         }
211         return null;
212     }
213     public Void JavaDoc visitMemberSelect(MemberSelectTree tree, Object JavaDoc o) {
214         super.visitMemberSelect(tree, o);
215         SearchInfo si = target.get(model.getElement(tree));
216         if (si != null)
217             add(tree, si, UseFinder.GETUSE);
218         return null;
219     }
220 }
221
Popular Tags