1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.OperationCanceledException; 15 import org.eclipse.jdt.core.*; 16 import org.eclipse.jdt.core.compiler.CharOperation; 17 import org.eclipse.jdt.core.search.*; 18 import org.eclipse.jdt.internal.codeassist.ISearchRequestor; 19 import org.eclipse.jdt.internal.compiler.env.AccessRestriction; 20 import org.eclipse.jdt.internal.compiler.env.IBinaryType; 21 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; 22 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 23 import org.eclipse.jdt.internal.compiler.env.ISourceType; 24 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; 25 import org.eclipse.jdt.internal.core.search.BasicSearchEngine; 26 import org.eclipse.jdt.internal.core.search.IRestrictedAccessTypeRequestor; 27 28 32 public class SearchableEnvironment 33 implements INameEnvironment, IJavaSearchConstants { 34 35 public NameLookup nameLookup; 36 protected ICompilationUnit unitToSkip; 37 protected org.eclipse.jdt.core.ICompilationUnit[] workingCopies; 38 39 protected JavaProject project; 40 protected IJavaSearchScope searchScope; 41 42 protected boolean checkAccessRestrictions; 43 44 47 public SearchableEnvironment(JavaProject project, org.eclipse.jdt.core.ICompilationUnit[] workingCopies) throws JavaModelException { 48 this.project = project; 49 this.checkAccessRestrictions = 50 !JavaCore.IGNORE.equals(project.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true)) 51 || !JavaCore.IGNORE.equals(project.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true)); 52 this.workingCopies = workingCopies; 53 this.nameLookup = project.newNameLookup(workingCopies); 54 55 if(this.checkAccessRestrictions) { 57 this.searchScope = BasicSearchEngine.createJavaSearchScope(new IJavaElement[] {project}); 58 } else { 59 this.searchScope = BasicSearchEngine.createJavaSearchScope(this.nameLookup.packageFragmentRoots); 60 } 61 } 62 63 66 public SearchableEnvironment(JavaProject project, WorkingCopyOwner owner) throws JavaModelException { 67 this(project, owner == null ? null : JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true)); 68 } 69 70 private static int convertSearchFilterToModelFilter(int searchFilter) { 71 switch (searchFilter) { 72 case IJavaSearchConstants.CLASS: 73 return NameLookup.ACCEPT_CLASSES; 74 case IJavaSearchConstants.INTERFACE: 75 return NameLookup.ACCEPT_INTERFACES; 76 case IJavaSearchConstants.ENUM: 77 return NameLookup.ACCEPT_ENUMS; 78 case IJavaSearchConstants.ANNOTATION_TYPE: 79 return NameLookup.ACCEPT_ANNOTATIONS; 80 case IJavaSearchConstants.CLASS_AND_ENUM: 81 return NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_ENUMS; 82 case IJavaSearchConstants.CLASS_AND_INTERFACE: 83 return NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES; 84 default: 85 return NameLookup.ACCEPT_ALL; 86 } 87 } 88 92 protected NameEnvironmentAnswer find(String typeName, String packageName) { 93 if (packageName == null) 94 packageName = IPackageFragment.DEFAULT_PACKAGE_NAME; 95 NameLookup.Answer answer = 96 this.nameLookup.findType( 97 typeName, 98 packageName, 99 false, 100 NameLookup.ACCEPT_ALL, 101 this.checkAccessRestrictions); 102 if (answer != null) { 103 if (answer.type instanceof BinaryType) { try { 106 return new NameEnvironmentAnswer((IBinaryType) ((BinaryType) answer.type).getElementInfo(), answer.restriction); 107 } catch (JavaModelException npe) { 108 return null; 109 } 110 } else { try { 112 SourceTypeElementInfo sourceType = (SourceTypeElementInfo)((SourceType) answer.type).getElementInfo(); 114 ISourceType topLevelType = sourceType; 115 while (topLevelType.getEnclosingType() != null) { 116 topLevelType = topLevelType.getEnclosingType(); 117 } 118 IType[] types = sourceType.getHandle().getCompilationUnit().getTypes(); 120 ISourceType[] sourceTypes = new ISourceType[types.length]; 121 122 sourceTypes[0] = sourceType; 124 int length = types.length; 125 for (int i = 0, index = 1; i < length; i++) { 126 ISourceType otherType = 127 (ISourceType) ((JavaElement) types[i]).getElementInfo(); 128 if (!otherType.equals(topLevelType) && index < length) sourceTypes[index++] = otherType; 130 } 131 return new NameEnvironmentAnswer(sourceTypes, answer.restriction); 132 } catch (JavaModelException npe) { 133 return null; 134 } 135 } 136 } 137 return null; 138 } 139 140 147 public void findPackages(char[] prefix, ISearchRequestor requestor) { 148 this.nameLookup.seekPackageFragments( 149 new String (prefix), 150 true, 151 new SearchableEnvironmentRequestor(requestor)); 152 } 153 154 167 public void findExactTypes(char[] name, final boolean findMembers, int searchFor, final ISearchRequestor storage) { 168 169 try { 170 final String excludePath; 171 if (this.unitToSkip != null) { 172 if (!(this.unitToSkip instanceof IJavaElement)) { 173 findExactTypes( 175 new String (name), 176 storage, 177 convertSearchFilterToModelFilter(searchFor)); 178 return; 179 } 180 excludePath = ((IJavaElement) this.unitToSkip).getPath().toString(); 181 } else { 182 excludePath = null; 183 } 184 185 IProgressMonitor progressMonitor = new IProgressMonitor() { 186 boolean isCanceled = false; 187 public void beginTask(String n, int totalWork) { 188 } 190 public void done() { 191 } 193 public void internalWorked(double work) { 194 } 196 public boolean isCanceled() { 197 return isCanceled; 198 } 199 public void setCanceled(boolean value) { 200 isCanceled = value; 201 } 202 public void setTaskName(String n) { 203 } 205 public void subTask(String n) { 206 } 208 public void worked(int work) { 209 } 211 }; 212 IRestrictedAccessTypeRequestor typeRequestor = new IRestrictedAccessTypeRequestor() { 213 public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { 214 if (excludePath != null && excludePath.equals(path)) 215 return; 216 if (!findMembers && enclosingTypeNames != null && enclosingTypeNames.length > 0) 217 return; storage.acceptType(packageName, simpleTypeName, enclosingTypeNames, modifiers, access); 219 } 220 }; 221 try { 222 new BasicSearchEngine(this.workingCopies).searchAllTypeNames( 223 null, 224 SearchPattern.R_EXACT_MATCH, 225 name, 226 SearchPattern.R_EXACT_MATCH, 227 searchFor, 228 this.searchScope, 229 typeRequestor, 230 CANCEL_IF_NOT_READY_TO_SEARCH, 231 progressMonitor); 232 } catch (OperationCanceledException e) { 233 findExactTypes( 234 new String (name), 235 storage, 236 convertSearchFilterToModelFilter(searchFor)); 237 } 238 } catch (JavaModelException e) { 239 findExactTypes( 240 new String (name), 241 storage, 242 convertSearchFilterToModelFilter(searchFor)); 243 } 244 } 245 246 249 private void findExactTypes(String name, ISearchRequestor storage, int type) { 250 SearchableEnvironmentRequestor requestor = 251 new SearchableEnvironmentRequestor(storage, this.unitToSkip, this.project, this.nameLookup); 252 this.nameLookup.seekTypes(name, null, false, type, requestor); 253 } 254 255 258 public NameEnvironmentAnswer findType(char[][] compoundTypeName) { 259 if (compoundTypeName == null) return null; 260 261 int length = compoundTypeName.length; 262 if (length <= 1) { 263 if (length == 0) return null; 264 return find(new String (compoundTypeName[0]), null); 265 } 266 267 int lengthM1 = length - 1; 268 char[][] packageName = new char[lengthM1][]; 269 System.arraycopy(compoundTypeName, 0, packageName, 0, lengthM1); 270 271 return find( 272 new String (compoundTypeName[lengthM1]), 273 CharOperation.toString(packageName)); 274 } 275 276 279 public NameEnvironmentAnswer findType(char[] name, char[][] packageName) { 280 if (name == null) return null; 281 282 return find( 283 new String (name), 284 packageName == null || packageName.length == 0 ? null : CharOperation.toString(packageName)); 285 } 286 287 302 public void findTypes(char[] prefix, final boolean findMembers, boolean camelCaseMatch, int searchFor, final ISearchRequestor storage) { 303 304 310 try { 311 final String excludePath; 312 if (this.unitToSkip != null) { 313 if (!(this.unitToSkip instanceof IJavaElement)) { 314 findTypes( 316 new String (prefix), 317 storage, 318 convertSearchFilterToModelFilter(searchFor)); 319 return; 320 } 321 excludePath = ((IJavaElement) this.unitToSkip).getPath().toString(); 322 } else { 323 excludePath = null; 324 } 325 int lastDotIndex = CharOperation.lastIndexOf('.', prefix); 326 char[] qualification, simpleName; 327 if (lastDotIndex < 0) { 328 qualification = null; 329 if (camelCaseMatch) { 330 simpleName = prefix; 331 } else { 332 simpleName = CharOperation.toLowerCase(prefix); 333 } 334 } else { 335 qualification = CharOperation.subarray(prefix, 0, lastDotIndex); 336 if (camelCaseMatch) { 337 simpleName = CharOperation.subarray(prefix, lastDotIndex + 1, prefix.length); 338 } else { 339 simpleName = 340 CharOperation.toLowerCase( 341 CharOperation.subarray(prefix, lastDotIndex + 1, prefix.length)); 342 } 343 } 344 345 IProgressMonitor progressMonitor = new IProgressMonitor() { 346 boolean isCanceled = false; 347 public void beginTask(String name, int totalWork) { 348 } 350 public void done() { 351 } 353 public void internalWorked(double work) { 354 } 356 public boolean isCanceled() { 357 return isCanceled; 358 } 359 public void setCanceled(boolean value) { 360 isCanceled = value; 361 } 362 public void setTaskName(String name) { 363 } 365 public void subTask(String name) { 366 } 368 public void worked(int work) { 369 } 371 }; 372 IRestrictedAccessTypeRequestor typeRequestor = new IRestrictedAccessTypeRequestor() { 373 public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { 374 if (excludePath != null && excludePath.equals(path)) 375 return; 376 if (!findMembers && enclosingTypeNames != null && enclosingTypeNames.length > 0) 377 return; storage.acceptType(packageName, simpleTypeName, enclosingTypeNames, modifiers, access); 379 } 380 }; 381 try { 382 int matchRule = SearchPattern.R_PREFIX_MATCH; 383 if (camelCaseMatch) matchRule |= SearchPattern.R_CAMELCASE_MATCH; 384 new BasicSearchEngine(this.workingCopies).searchAllTypeNames( 385 qualification, 386 SearchPattern.R_EXACT_MATCH, 387 simpleName, 388 matchRule, searchFor, 390 this.searchScope, 391 typeRequestor, 392 CANCEL_IF_NOT_READY_TO_SEARCH, 393 progressMonitor); 394 } catch (OperationCanceledException e) { 395 findTypes( 396 new String (prefix), 397 storage, 398 convertSearchFilterToModelFilter(searchFor)); 399 } 400 } catch (JavaModelException e) { 401 findTypes( 402 new String (prefix), 403 storage, 404 convertSearchFilterToModelFilter(searchFor)); 405 } 406 } 407 408 414 private void findTypes(String prefix, ISearchRequestor storage, int type) { 415 SearchableEnvironmentRequestor requestor = 417 new SearchableEnvironmentRequestor(storage, this.unitToSkip, this.project, this.nameLookup); 418 int index = prefix.lastIndexOf('.'); 419 if (index == -1) { 420 this.nameLookup.seekTypes(prefix, null, true, type, requestor); 421 } else { 422 String packageName = prefix.substring(0, index); 423 JavaElementRequestor elementRequestor = new JavaElementRequestor(); 424 this.nameLookup.seekPackageFragments(packageName, false, elementRequestor); 425 IPackageFragment[] fragments = elementRequestor.getPackageFragments(); 426 if (fragments != null) { 427 String className = prefix.substring(index + 1); 428 for (int i = 0, length = fragments.length; i < length; i++) 429 if (fragments[i] != null) 430 this.nameLookup.seekTypes(className, fragments[i], true, type, requestor); 431 } 432 } 433 } 434 435 438 public boolean isPackage(char[][] parentPackageName, char[] subPackageName) { 439 String [] pkgName; 440 if (parentPackageName == null) 441 pkgName = new String [] {new String (subPackageName)}; 442 else { 443 int length = parentPackageName.length; 444 pkgName = new String [length+1]; 445 for (int i = 0; i < length; i++) 446 pkgName[i] = new String (parentPackageName[i]); 447 pkgName[length] = new String (subPackageName); 448 } 449 return this.nameLookup.isPackage(pkgName); 450 } 451 452 455 protected String toStringChar(char[] name) { 456 return "[" + new String (name) + "]" ; } 459 460 463 protected String toStringCharChar(char[][] names) { 464 StringBuffer result = new StringBuffer (); 465 for (int i = 0; i < names.length; i++) { 466 result.append(toStringChar(names[i])); 467 } 468 return result.toString(); 469 } 470 471 public void cleanup() { 472 } 474 } 475 | Popular Tags |