1 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 ; 30 import java.util.HashSet ; 31 import java.util.Set ; 32 import java.util.Stack ; 33 import javax.lang.model.element.Element; 34 import javax.swing.text.Document ; 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 43 public class FindLocalUsagesQuery extends CancellableTreePathScanner<Void , Stack <Tree>> { 44 45 private CompilationInfo info; 46 private Set <Highlight> usages; 47 private Element toFind; 48 private Document doc; 49 50 51 public FindLocalUsagesQuery() { 52 } 53 54 public Set <Highlight> findUsages(Element element, CompilationInfo info, Document doc) { 55 this.info = info; 56 this.usages = new HashSet <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 77 public Void visitIdentifier(IdentifierTree tree, Stack <Tree> d) { 78 handlePotentialVariable(getCurrentPath()); 79 super.visitIdentifier(tree, d); 80 return null; 81 } 82 83 @Override 84 public Void visitMethod(MethodTree tree, Stack <Tree> d) { 85 handlePotentialVariable(getCurrentPath()); 86 super.visitMethod(tree, d); 87 return null; 88 } 89 90 @Override 91 public Void visitMemberSelect(MemberSelectTree node, Stack <Tree> p) { 92 handlePotentialVariable(getCurrentPath()); 93 super.visitMemberSelect(node, p); 94 return null; 95 } 96 97 @Override 98 public Void visitVariable(VariableTree tree, Stack <Tree> d) { 99 handlePotentialVariable(getCurrentPath()); 100 super.visitVariable(tree, d); 101 return null; 102 } 103 104 @Override 105 public Void visitClass(ClassTree tree, Stack <Tree> d) { 106 handlePotentialVariable(getCurrentPath()); 107 super.visitClass(tree, d); 108 return null; 109 } 110 } 111 | Popular Tags |