KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > semantic > FindLocalUsagesQuery


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 package org.netbeans.modules.java.editor.semantic;
20
21 import com.sun.source.tree.ClassTree;
22 import com.sun.source.tree.IdentifierTree;
23 import com.sun.source.tree.MemberSelectTree;
24 import com.sun.source.tree.MethodTree;
25 import com.sun.source.tree.Tree;
26 import com.sun.source.tree.VariableTree;
27 import com.sun.source.util.TreePath;
28 import com.sun.source.util.TreePathScanner;
29 import java.util.EnumSet JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.Stack JavaDoc;
33 import javax.lang.model.element.Element;
34 import javax.swing.text.Document JavaDoc;
35 import org.netbeans.api.java.source.CompilationInfo;
36 import org.netbeans.api.java.source.support.CancellableTreePathScanner;
37 import org.netbeans.modules.editor.highlights.spi.Highlight;
38
39 /**
40  *
41  * @author Jan Lahoda
42  */

43 public class FindLocalUsagesQuery extends CancellableTreePathScanner<Void JavaDoc, Stack JavaDoc<Tree>> {
44     
45     private CompilationInfo info;
46     private Set JavaDoc<Highlight> usages;
47     private Element toFind;
48     private Document JavaDoc doc;
49     
50     /** Creates a new instance of FindLocalUsagesQuery */
51     public FindLocalUsagesQuery() {
52     }
53     
54     public Set JavaDoc<Highlight> findUsages(Element element, CompilationInfo info, Document JavaDoc doc) {
55         this.info = info;
56         this.usages = new HashSet JavaDoc<Highlight>();
57         this.toFind = element;
58         this.doc = doc;
59         
60         scan(info.getCompilationUnit(), null);
61         return usages;
62     }
63
64     private Highlight createHighlight(TreePath tree) {
65         return Utilities.createHighlight(info.getCompilationUnit(), info.getTrees().getSourcePositions(), doc, tree, EnumSet.of(ColoringAttributes.MARK_OCCURRENCES), MarkOccurencesHighlighter.ES_COLOR);
66     }
67     
68     private void handlePotentialVariable(TreePath tree) {
69         Element el = info.getTrees().getElement(tree);
70         
71         if (toFind.equals(el)) {
72             usages.add(createHighlight(tree));
73         }
74     }
75     
76     @Override JavaDoc
77     public Void JavaDoc visitIdentifier(IdentifierTree tree, Stack JavaDoc<Tree> d) {
78         handlePotentialVariable(getCurrentPath());
79         super.visitIdentifier(tree, d);
80         return null;
81     }
82     
83     @Override JavaDoc
84     public Void JavaDoc visitMethod(MethodTree tree, Stack JavaDoc<Tree> d) {
85         handlePotentialVariable(getCurrentPath());
86         super.visitMethod(tree, d);
87         return null;
88     }
89     
90     @Override JavaDoc
91     public Void JavaDoc visitMemberSelect(MemberSelectTree node, Stack JavaDoc<Tree> p) {
92         handlePotentialVariable(getCurrentPath());
93         super.visitMemberSelect(node, p);
94         return null;
95     }
96     
97     @Override JavaDoc
98     public Void JavaDoc visitVariable(VariableTree tree, Stack JavaDoc<Tree> d) {
99         handlePotentialVariable(getCurrentPath());
100         super.visitVariable(tree, d);
101         return null;
102     }
103     
104     @Override JavaDoc
105     public Void JavaDoc visitClass(ClassTree tree, Stack JavaDoc<Tree> d) {
106         handlePotentialVariable(getCurrentPath());
107         super.visitClass(tree, d);
108         return null;
109     }
110 }
111
Popular Tags