1 11 package org.eclipse.jdt.core.search; 12 13 import org.eclipse.core.resources.*; 14 import org.eclipse.core.runtime.*; 15 16 import org.eclipse.jdt.core.*; 17 import org.eclipse.jdt.internal.compiler.env.AccessRestriction; 18 import org.eclipse.jdt.internal.core.search.*; 19 import org.eclipse.jdt.internal.core.search.matching.*; 20 21 36 public class SearchEngine { 37 38 42 static class SearchPatternAdapter implements ISearchPattern { 43 SearchPattern pattern; 44 SearchPatternAdapter(SearchPattern pattern) { 45 this.pattern = pattern; 46 } 47 } 48 49 53 static class ResultCollectorAdapter extends SearchRequestor { 54 IJavaSearchResultCollector resultCollector; 55 ResultCollectorAdapter(IJavaSearchResultCollector resultCollector) { 56 this.resultCollector = resultCollector; 57 } 58 61 public void acceptSearchMatch(SearchMatch match) throws CoreException { 62 this.resultCollector.accept( 63 match.getResource(), 64 match.getOffset(), 65 match.getOffset() + match.getLength(), 66 (IJavaElement) match.getElement(), 67 match.getAccuracy() 68 ); 69 } 70 73 public void beginReporting() { 74 this.resultCollector.aboutToStart(); 75 } 76 79 public void endReporting() { 80 this.resultCollector.done(); 81 } 82 } 83 84 88 static class TypeNameRequestorAdapter implements IRestrictedAccessTypeRequestor { 89 ITypeNameRequestor nameRequestor; 90 TypeNameRequestorAdapter(ITypeNameRequestor requestor) { 91 this.nameRequestor = requestor; 92 } 93 public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { 94 if (Flags.isInterface(modifiers)) { 95 nameRequestor.acceptInterface(packageName, simpleTypeName, enclosingTypeNames, path); 96 } else { 97 nameRequestor.acceptClass(packageName, simpleTypeName, enclosingTypeNames, path); 98 } 99 } 100 } 101 102 private BasicSearchEngine basicEngine; 104 105 108 public SearchEngine() { 109 this.basicEngine = new BasicSearchEngine(); 110 } 111 112 124 public SearchEngine(ICompilationUnit[] workingCopies) { 125 this.basicEngine = new BasicSearchEngine(workingCopies); 126 } 127 140 public SearchEngine(IWorkingCopy[] workingCopies) { 141 int length = workingCopies.length; 142 ICompilationUnit[] units = new ICompilationUnit[length]; 143 System.arraycopy(workingCopies, 0, units, 0, length); 144 this.basicEngine = new BasicSearchEngine(units); 145 } 146 147 155 public SearchEngine(WorkingCopyOwner workingCopyOwner) { 156 this.basicEngine = new BasicSearchEngine(workingCopyOwner); 157 } 158 159 168 public static IJavaSearchScope createHierarchyScope(IType type) throws JavaModelException { 169 return BasicSearchEngine.createHierarchyScope(type); 170 } 171 172 185 public static IJavaSearchScope createHierarchyScope(IType type, WorkingCopyOwner owner) throws JavaModelException { 186 return BasicSearchEngine.createHierarchyScope(type, owner); 187 } 188 189 202 public static IJavaSearchScope createJavaSearchScope(IResource[] resources) { 203 int length = resources.length; 204 IJavaElement[] elements = new IJavaElement[length]; 205 for (int i = 0; i < length; i++) { 206 elements[i] = JavaCore.create(resources[i]); 207 } 208 return createJavaSearchScope(elements); 209 } 210 211 231 public static IJavaSearchScope createJavaSearchScope(IJavaElement[] elements) { 232 return BasicSearchEngine.createJavaSearchScope(elements); 233 } 234 235 255 public static IJavaSearchScope createJavaSearchScope(IJavaElement[] elements, boolean includeReferencedProjects) { 256 return BasicSearchEngine.createJavaSearchScope(elements, includeReferencedProjects); 257 } 258 259 290 public static IJavaSearchScope createJavaSearchScope(IJavaElement[] elements, int includeMask) { 291 return BasicSearchEngine.createJavaSearchScope(elements, includeMask); 292 } 293 294 303 public static ISearchPattern createOrSearchPattern(ISearchPattern leftPattern, ISearchPattern rightPattern) { 304 SearchPattern left = ((SearchPatternAdapter) leftPattern).pattern; 305 SearchPattern right = ((SearchPatternAdapter) rightPattern).pattern; 306 SearchPattern pattern = SearchPattern.createOrPattern(left, right); 307 return new SearchPatternAdapter(pattern); 308 } 309 310 357 public static ISearchPattern createSearchPattern(String stringPattern, int searchFor, int limitTo, boolean isCaseSensitive) { 358 int matchMode = stringPattern.indexOf('*') != -1 || stringPattern.indexOf('?') != -1 359 ? SearchPattern.R_PATTERN_MATCH 360 : SearchPattern.R_EXACT_MATCH; 361 int matchRule = isCaseSensitive ? matchMode | SearchPattern.R_CASE_SENSITIVE : matchMode; 362 return new SearchPatternAdapter(SearchPattern.createPattern(stringPattern, searchFor, limitTo, matchRule)); 363 } 364 365 387 public static ISearchPattern createSearchPattern(IJavaElement element, int limitTo) { 388 return new SearchPatternAdapter(SearchPattern.createPattern(element, limitTo)); 389 } 390 391 399 public static TypeNameMatch createTypeNameMatch(IType type, int modifiers) { 400 return BasicSearchEngine.createTypeNameMatch(type, modifiers); 401 } 402 403 408 public static IJavaSearchScope createWorkspaceScope() { 409 return BasicSearchEngine.createWorkspaceScope(); 410 } 411 417 public static SearchParticipant getDefaultSearchParticipant() { 418 return BasicSearchEngine.getDefaultSearchParticipant(); 419 } 420 421 456 public void search(IWorkspace workspace, String patternString, int searchFor, int limitTo, IJavaSearchScope scope, IJavaSearchResultCollector resultCollector) throws JavaModelException { 457 try { 458 int matchMode = patternString.indexOf('*') != -1 || patternString.indexOf('?') != -1 459 ? SearchPattern.R_PATTERN_MATCH 460 : SearchPattern.R_EXACT_MATCH; 461 search( 462 SearchPattern.createPattern(patternString, searchFor, limitTo, matchMode | SearchPattern.R_CASE_SENSITIVE), 463 new SearchParticipant[] {getDefaultSearchParticipant()}, 464 scope, 465 new ResultCollectorAdapter(resultCollector), 466 resultCollector.getProgressMonitor()); 467 } catch (CoreException e) { 468 if (e instanceof JavaModelException) 469 throw (JavaModelException) e; 470 throw new JavaModelException(e); 471 } 472 } 473 474 499 public void search(IWorkspace workspace, IJavaElement element, int limitTo, IJavaSearchScope scope, IJavaSearchResultCollector resultCollector) throws JavaModelException { 500 search(workspace, createSearchPattern(element, limitTo), scope, resultCollector); 501 } 502 503 518 public void search(IWorkspace workspace, ISearchPattern searchPattern, IJavaSearchScope scope, IJavaSearchResultCollector resultCollector) throws JavaModelException { 519 try { 520 search( 521 ((SearchPatternAdapter)searchPattern).pattern, 522 new SearchParticipant[] {getDefaultSearchParticipant()}, 523 scope, 524 new ResultCollectorAdapter(resultCollector), 525 resultCollector.getProgressMonitor()); 526 } catch (CoreException e) { 527 if (e instanceof JavaModelException) 528 throw (JavaModelException) e; 529 throw new JavaModelException(e); 530 } 531 } 532 533 549 public void search(SearchPattern pattern, SearchParticipant[] participants, IJavaSearchScope scope, SearchRequestor requestor, IProgressMonitor monitor) throws CoreException { 550 this.basicEngine.search(pattern, participants, scope, requestor, monitor); 551 } 552 553 608 public void searchAllTypeNames( 609 final char[] packageExactName, 610 final char[] typeName, 611 final int matchRule, 612 int searchFor, 613 IJavaSearchScope scope, 614 final TypeNameRequestor nameRequestor, 615 int waitingPolicy, 616 IProgressMonitor progressMonitor) throws JavaModelException { 617 618 searchAllTypeNames(packageExactName, SearchPattern.R_EXACT_MATCH, typeName, matchRule, searchFor, scope, nameRequestor, waitingPolicy, progressMonitor); 619 } 620 621 685 public void searchAllTypeNames( 686 final char[] packageName, 687 final int packageMatchRule, 688 final char[] typeName, 689 final int typeMatchRule, 690 int searchFor, 691 IJavaSearchScope scope, 692 final TypeNameRequestor nameRequestor, 693 int waitingPolicy, 694 IProgressMonitor progressMonitor) throws JavaModelException { 695 696 TypeNameRequestorWrapper requestorWrapper = new TypeNameRequestorWrapper(nameRequestor); 697 this.basicEngine.searchAllTypeNames(packageName, packageMatchRule, typeName, typeMatchRule, searchFor, scope, requestorWrapper, waitingPolicy, progressMonitor); 698 } 699 700 769 public void searchAllTypeNames( 770 final char[] packageName, 771 final int packageMatchRule, 772 final char[] typeName, 773 final int typeMatchRule, 774 int searchFor, 775 IJavaSearchScope scope, 776 final TypeNameMatchRequestor nameMatchRequestor, 777 int waitingPolicy, 778 IProgressMonitor progressMonitor) throws JavaModelException { 779 780 TypeNameMatchRequestorWrapper requestorWrapper = new TypeNameMatchRequestorWrapper(nameMatchRequestor, scope); 781 this.basicEngine.searchAllTypeNames(packageName, packageMatchRule, typeName, typeMatchRule, searchFor, scope, requestorWrapper, waitingPolicy, progressMonitor); 782 } 783 784 810 public void searchAllTypeNames( 811 final char[][] qualifications, 812 final char[][] typeNames, 813 IJavaSearchScope scope, 814 final TypeNameRequestor nameRequestor, 815 int waitingPolicy, 816 IProgressMonitor progressMonitor) throws JavaModelException { 817 818 TypeNameRequestorWrapper requestorWrapper = new TypeNameRequestorWrapper(nameRequestor); 819 this.basicEngine.searchAllTypeNames( 820 qualifications, 821 typeNames, 822 SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, 823 IJavaSearchConstants.TYPE, 824 scope, 825 requestorWrapper, 826 waitingPolicy, 827 progressMonitor); 828 } 829 830 861 public void searchAllTypeNames( 862 final char[][] qualifications, 863 final char[][] typeNames, 864 IJavaSearchScope scope, 865 final TypeNameMatchRequestor nameMatchRequestor, 866 int waitingPolicy, 867 IProgressMonitor progressMonitor) throws JavaModelException { 868 869 TypeNameMatchRequestorWrapper requestorWrapper = new TypeNameMatchRequestorWrapper(nameMatchRequestor, scope); 870 this.basicEngine.searchAllTypeNames( 871 qualifications, 872 typeNames, 873 SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, 874 IJavaSearchConstants.TYPE, 875 scope, 876 requestorWrapper, 877 waitingPolicy, 878 progressMonitor); 879 } 880 881 927 public void searchAllTypeNames( 928 final char[] packageName, 929 final char[] typeName, 930 final int matchRule, 931 int searchFor, 932 IJavaSearchScope scope, 933 final ITypeNameRequestor nameRequestor, 934 int waitingPolicy, 935 IProgressMonitor progressMonitor) throws JavaModelException { 936 937 TypeNameRequestorAdapter requestorAdapter = new TypeNameRequestorAdapter(nameRequestor); 938 this.basicEngine.searchAllTypeNames(packageName, SearchPattern.R_EXACT_MATCH, typeName, matchRule, searchFor, scope, requestorAdapter, waitingPolicy, progressMonitor); 939 } 940 941 985 public void searchAllTypeNames( 986 IWorkspace workspace, 987 final char[] packageName, 988 final char[] typeName, 989 final int matchMode, 990 final boolean isCaseSensitive, 991 int searchFor, 992 IJavaSearchScope scope, 993 final ITypeNameRequestor nameRequestor, 994 int waitingPolicy, 995 IProgressMonitor progressMonitor) throws JavaModelException { 996 997 searchAllTypeNames( 998 packageName, 999 typeName, 1000 isCaseSensitive ? matchMode | SearchPattern.R_CASE_SENSITIVE : matchMode, 1001 searchFor, 1002 scope, 1003 nameRequestor, 1004 waitingPolicy, 1005 progressMonitor); 1006 } 1007 1008 1045 public void searchDeclarationsOfAccessedFields(IJavaElement enclosingElement, SearchRequestor requestor, IProgressMonitor monitor) throws JavaModelException { 1046 this.basicEngine.searchDeclarationsOfAccessedFields(enclosingElement, requestor, monitor); 1047 } 1048 1049 1086 public void searchDeclarationsOfAccessedFields(IWorkspace workspace, IJavaElement enclosingElement, IJavaSearchResultCollector resultCollector) throws JavaModelException { 1087 SearchPattern pattern = new DeclarationOfAccessedFieldsPattern(enclosingElement); 1088 this.basicEngine.searchDeclarations(enclosingElement, new ResultCollectorAdapter(resultCollector), pattern, resultCollector.getProgressMonitor()); 1089 } 1090 1091 1128 public void searchDeclarationsOfReferencedTypes(IJavaElement enclosingElement, SearchRequestor requestor, IProgressMonitor monitor) throws JavaModelException { 1129 this.basicEngine.searchDeclarationsOfReferencedTypes(enclosingElement, requestor, monitor); 1130 } 1131 1132 1169 public void searchDeclarationsOfReferencedTypes(IWorkspace workspace, IJavaElement enclosingElement, IJavaSearchResultCollector resultCollector) throws JavaModelException { 1170 SearchPattern pattern = new DeclarationOfReferencedTypesPattern(enclosingElement); 1171 this.basicEngine.searchDeclarations(enclosingElement, new ResultCollectorAdapter(resultCollector), pattern, resultCollector.getProgressMonitor()); 1172 } 1173 1174 1214 public void searchDeclarationsOfSentMessages(IJavaElement enclosingElement, SearchRequestor requestor, IProgressMonitor monitor) throws JavaModelException { 1215 this.basicEngine.searchDeclarationsOfSentMessages(enclosingElement, requestor, monitor); 1216 } 1217 1218 1258 public void searchDeclarationsOfSentMessages(IWorkspace workspace, IJavaElement enclosingElement, IJavaSearchResultCollector resultCollector) throws JavaModelException { 1259 SearchPattern pattern = new DeclarationOfReferencedMethodsPattern(enclosingElement); 1260 this.basicEngine.searchDeclarations(enclosingElement, new ResultCollectorAdapter(resultCollector), pattern, resultCollector.getProgressMonitor()); 1261 } 1262} 1263 | Popular Tags |