1 19 package org.netbeans.test.j2ee.refactoring.usages; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import org.netbeans.jmi.javamodel.Constructor; 24 import org.netbeans.jmi.javamodel.Feature; 25 import org.netbeans.jmi.javamodel.Method; 26 import org.netbeans.modules.refactoring.api.RefactoringSession; 27 import org.netbeans.modules.refactoring.api.WhereUsedQuery; 28 import org.netbeans.test.j2ee.refactoring.RefactoringTestCase; 29 import org.netbeans.test.j2ee.refactoring.Utility; 30 31 35 public class WhereUsedTestCase extends RefactoringTestCase { 36 37 public static final int CLASS_USAGES=0; 38 public static final int CLASS_SUBTYPES=1; 39 public static final int CLASS_DIRECTSUBTYPES=2; 40 41 42 public WhereUsedTestCase(String name) { 43 super(name); 44 } 45 46 49 protected void findClass(String className) { 50 setJavaClass(className); 51 Utility.beginTrans(false); 52 try { 53 ref("Find usages of: "+className); 54 WhereUsedQuery wu=new WhereUsedQuery(jc); 55 wu.setSearchInComments(false); 56 RefactoringSession result = RefactoringSession.create(null); 57 refProblems(wu.prepare(result)); 58 refUsages(result); 59 } finally { 60 Utility.endTrans(); 61 } 62 ref(""); 63 } 64 65 protected void findClassParams(String className, int type) { 66 setJavaClass(className); 67 Utility.beginTrans(false); 68 try { 69 ref("Find usages of: "+className); 70 WhereUsedQuery wu=new WhereUsedQuery(jc); 71 switch (type) { 72 case CLASS_USAGES: 73 wu.setFindUsages(true); 74 break; 75 case CLASS_SUBTYPES: 76 wu.setFindSubclasses(true); 77 break; 78 case CLASS_DIRECTSUBTYPES: 79 wu.setFindDirectSubclassesOnly(true); 80 break; 81 } 82 wu.setSearchInComments(true); 83 if (refProblems(wu.checkParameters())) throw new Exception ("Fatal problem"); 84 RefactoringSession result = RefactoringSession.create(null); 85 refProblems(wu.prepare(result)); 86 refUsages(result); 87 } catch (Throwable t) { 88 if (t.getMessage() == null || !t.getMessage().equals("Fatal problem")) { 89 t.printStackTrace(getLogStream()); 90 assertTrue(t.getMessage(), false); 91 } 92 } finally { 93 Utility.endTrans(); 94 } 95 ref(""); 96 } 97 98 protected void findField(String className, String fieldName) { 99 setJavaClass(className); 100 Utility.beginTrans(false); 101 try { 102 org.netbeans.jmi.javamodel.Field field = jc.getField(fieldName, false); 103 assertNotNull("Field "+fieldName+" of "+className+" is null", field); 104 ref("Find usages of: "+fieldName); 105 WhereUsedQuery wu=new WhereUsedQuery(field); 106 wu.setSearchInComments(true); 107 RefactoringSession result = RefactoringSession.create(null); 108 refProblems(wu.prepare(result)); 109 refUsages(result); 110 } finally { 111 Utility.endTrans(); 112 } 113 ref(""); 114 } 115 116 protected void findAnnotationTypeMember(String annotationName, String memberName) { 117 setJavaClass(annotationName); 118 Utility.beginTrans(false); 119 try { 120 Feature member = null; 121 for (Iterator it=jc.getFeatures().iterator();it.hasNext();) { 122 Feature f=(Feature)(it.next()); 123 if (f.getName().equals(memberName)) { 124 member=f; 125 break; 126 } 127 } 128 assertNotNull("Member "+memberName+" of "+annotationName+" is null", member); 129 ref("Find usages of: "+memberName); 130 WhereUsedQuery wu=new WhereUsedQuery(member); 131 wu.setSearchInComments(true); 132 RefactoringSession result = RefactoringSession.create(null); 133 refProblems(wu.prepare(result)); 134 refUsages(result); 135 } finally { 136 Utility.endTrans(); 137 } 138 ref(""); 139 } 140 141 protected void findConstructor(String className, String [] arguments) { 142 setJavaClass(className); 143 Utility.beginTrans(false); 144 try { 145 ArrayList list=new ArrayList (); 146 String refs="Find usages of "+className+"("; 147 if (arguments != null && arguments.length > 0) { 148 for (int i=0;i < arguments.length;i++) { 149 list.add(Utility.findType(arguments[i], typeProxy, jc)); 150 if (i > 0) { 151 refs+=", "; 152 } 153 refs+=arguments[i]; 154 } 155 } 156 refs+=")"; 157 ref(refs); 158 Constructor cons = jc.getConstructor(list, false); 159 WhereUsedQuery wu=new WhereUsedQuery(cons); 160 wu.setSearchInComments(true); 161 RefactoringSession result = RefactoringSession.create(null); 162 refProblems(wu.prepare(result)); 163 refUsages(result); 164 } finally { 165 Utility.endTrans(); 166 } 167 ref(""); 168 } 169 170 protected void findMethod(String className, String methodName, String [] arguments) { 171 setJavaClass(className); 172 Utility.beginTrans(false); 173 try { 174 ArrayList list=new ArrayList (); 175 String refs="Find usages of "+className+"."+methodName+"("; 176 if (arguments != null && arguments.length > 0) { 177 for (int i=0;i < arguments.length;i++) { 178 list.add(Utility.findType(arguments[i], typeProxy, jc)); 179 if (i > 0) { 180 refs+=", "; 181 } 182 refs+=arguments[i]; 183 } 184 } 185 refs+=")"; 186 ref(refs); 187 Method method = jc.getMethod(methodName, list, false); 188 assertNotNull("Method "+refs+" of "+className+" is null", method); 189 WhereUsedQuery wu=new WhereUsedQuery(method); 190 wu.setSearchInComments(true); 191 RefactoringSession result = RefactoringSession.create(null); 192 refProblems(wu.prepare(result)); 193 refUsages(result); 194 } finally { 195 Utility.endTrans(); 196 } 197 ref(""); 198 } 199 200 protected void findMethodParams(String className, String methodName, String [] arguments, boolean fromBaseClass, boolean methodOverriders, boolean findUsages) { 201 setJavaClass(className); 202 Utility.beginTrans(false); 203 try { 204 ArrayList list=new ArrayList (); 205 String refs="Find usages of "+className+"."+methodName+"("; 206 if (arguments != null && arguments.length > 0) { 207 for (int i=0;i < arguments.length;i++) { 208 list.add(typeProxy.resolve(arguments[i])); 209 if (i > 0) { 210 refs+=", "; 211 } 212 refs+=arguments[i]; 213 } 214 } 215 refs+=")"; 216 ref(refs); 217 Method method = jc.getMethod(methodName, list, false); 218 assertNotNull("Method "+refs+" of "+className+" is null", method); 219 WhereUsedQuery wu=new WhereUsedQuery(method); 220 wu.setSearchFromBaseClass(fromBaseClass); 221 wu.setFindOverridingMethods(methodOverriders); 222 wu.setFindUsages(findUsages); 223 wu.setSearchInComments(true); 224 if (refProblems(wu.checkParameters())) throw new Exception ("Fatal problem"); 225 RefactoringSession result = RefactoringSession.create(null); 226 refProblems(wu.prepare(result)); 227 refUsages(result); 228 } catch (Throwable t) { 229 if (t.getMessage() == null || !t.getMessage().equals("Fatal problem")) { 230 t.printStackTrace(getLogStream()); 231 assertTrue(t.getMessage(), false); 232 } 233 } finally { 234 Utility.endTrans(); 235 } 236 ref(""); 237 } 238 239 } 240 | Popular Tags |