1 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 37 public class UseFinder extends NodeScanner<Void ,Object > { 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 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 } 73 return results; 74 } 75 76 private static final String [] sgtable = new String [128]; 77 78 public static String getMsg(int flags) { 79 String msg = sgtable[flags]; 80 if (msg == null) { 81 StringBuffer sb = new StringBuffer (); 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 115 public Void visitAssignment(AssignmentTree tree, Object 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 131 public Void visitCompoundAssignment(CompoundAssignmentTree tree, Object 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 147 public Void visitUnary(UnaryTree tree, Object 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 171 public Void visitIdentifier(IdentifierTree tree, Object p) { 172 if (model.getElement(tree) == target) 173 add(tree, GETUSE); 174 return super.visitIdentifier(tree, p); 175 } 176 177 @Override 178 public Void visitVariable(VariableTree tree, Object 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 190 public Void visitMemberSelect(MemberSelectTree tree, Object p) { 191 if (model.getElement(tree) == target) 192 add(tree, GETUSE); 193 return super.visitMemberSelect(tree, p); 194 } 195 } 196 | Popular Tags |