1 11 package org.eclipse.jdt.internal.corext.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.LinkedList ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.eclipse.core.runtime.Assert; 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 import org.eclipse.core.runtime.SubProgressMonitor; 28 29 import org.eclipse.core.resources.IProject; 30 import org.eclipse.core.resources.IResource; 31 32 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 33 import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry; 34 35 import org.eclipse.jdt.core.ICompilationUnit; 36 import org.eclipse.jdt.core.IJavaElement; 37 import org.eclipse.jdt.core.IJavaProject; 38 import org.eclipse.jdt.core.JavaCore; 39 import org.eclipse.jdt.core.JavaModelException; 40 import org.eclipse.jdt.core.WorkingCopyOwner; 41 import org.eclipse.jdt.core.search.IJavaSearchScope; 42 import org.eclipse.jdt.core.search.SearchEngine; 43 import org.eclipse.jdt.core.search.SearchMatch; 44 import org.eclipse.jdt.core.search.SearchPattern; 45 import org.eclipse.jdt.core.search.SearchRequestor; 46 47 import org.eclipse.jdt.internal.corext.util.Messages; 48 import org.eclipse.jdt.internal.corext.util.SearchUtils; 49 50 55 public final class RefactoringSearchEngine2 { 56 57 58 private static class DefaultSearchRequestor implements IRefactoringSearchRequestor { 59 60 public final SearchMatch acceptSearchMatch(final SearchMatch match) { 61 return match; 62 } 63 } 64 65 66 private class RefactoringCompilationUnitCollector extends RefactoringSearchCollector { 67 68 69 private final Set fCollectedUnits= new HashSet (); 70 71 72 private final Set fInaccurateMatches= new HashSet (); 73 74 public final void acceptSearchMatch(final SearchMatch match) throws CoreException { 75 final SearchMatch accepted= fRequestor.acceptSearchMatch(match); 76 if (accepted != null) { 77 final IResource resource= accepted.getResource(); 78 if (!resource.equals(fLastResource)) { 79 final IJavaElement element= JavaCore.create(resource); 80 if (element instanceof ICompilationUnit) 81 fCollectedUnits.add(element); 82 } 83 if (fInaccurate && accepted.getAccuracy() == SearchMatch.A_INACCURATE && !fInaccurateMatches.contains(accepted)) { 84 fStatus.addEntry(fSeverity, Messages.format(RefactoringCoreMessages.RefactoringSearchEngine_inaccurate_match, accepted.getResource().getName()), null, null, RefactoringStatusEntry.NO_CODE); 85 fInaccurateMatches.add(accepted); 86 } 87 } 88 } 89 90 public final void clearResults() { 91 super.clearResults(); 92 fCollectedUnits.clear(); 93 fInaccurateMatches.clear(); 94 } 95 96 public final Collection getBinaryResources() { 97 return Collections.EMPTY_SET; 98 } 99 100 public final Collection getCollectedMatches() { 101 return fCollectedUnits; 102 } 103 104 public final Collection getInaccurateMatches() { 105 return fInaccurateMatches; 106 } 107 } 108 109 private abstract class RefactoringSearchCollector extends SearchRequestor { 110 111 protected IResource fLastResource= null; 112 113 public void clearResults() { 114 fLastResource= null; 115 } 116 117 public abstract Collection getBinaryResources(); 118 119 public abstract Collection getCollectedMatches(); 120 121 public abstract Collection getInaccurateMatches(); 122 } 123 124 125 private class RefactoringSearchMatchCollector extends RefactoringSearchCollector { 126 127 128 private final Set fBinaryResources= new HashSet (); 129 130 131 private final List fCollectedMatches= new ArrayList (); 132 133 134 private final Set fInaccurateMatches= new HashSet (); 135 136 public final void acceptSearchMatch(final SearchMatch match) throws CoreException { 137 final SearchMatch accepted= fRequestor.acceptSearchMatch(match); 138 if (accepted != null) { 139 fCollectedMatches.add(accepted); 140 final IResource resource= accepted.getResource(); 141 if (!resource.equals(fLastResource)) { 142 if (fBinary) { 143 final IJavaElement element= JavaCore.create(resource); 144 if (!(element instanceof ICompilationUnit)) { 145 final IProject project= resource.getProject(); 146 if (!fGrouping) 147 fStatus.addEntry(fSeverity, Messages.format(RefactoringCoreMessages.RefactoringSearchEngine_binary_match_ungrouped, project.getName()), null, null, RefactoringStatusEntry.NO_CODE); 148 else if (!fBinaryResources.contains(resource)) 149 fStatus.addEntry(fSeverity, Messages.format(RefactoringCoreMessages.RefactoringSearchEngine_binary_match_grouped, project.getName()), null, null, RefactoringStatusEntry.NO_CODE); 150 fBinaryResources.add(resource); 151 } 152 } 153 if (fInaccurate && accepted.getAccuracy() == SearchMatch.A_INACCURATE && !fInaccurateMatches.contains(accepted)) { 154 fStatus.addEntry(fSeverity, Messages.format(RefactoringCoreMessages.RefactoringSearchEngine_inaccurate_match, resource.getName()), null, null, RefactoringStatusEntry.NO_CODE); 155 fInaccurateMatches.add(accepted); 156 } 157 } 158 } 159 } 160 161 public final void clearResults() { 162 super.clearResults(); 163 fCollectedMatches.clear(); 164 fInaccurateMatches.clear(); 165 fBinaryResources.clear(); 166 } 167 168 public final Collection getBinaryResources() { 169 return fBinaryResources; 170 } 171 172 public final Collection getCollectedMatches() { 173 return fCollectedMatches; 174 } 175 176 public final Collection getInaccurateMatches() { 177 return fInaccurateMatches; 178 } 179 } 180 181 182 public static final int GRANULARITY_COMPILATION_UNIT= 2; 183 184 185 public static final int GRANULARITY_SEARCH_MATCH= 1; 186 187 188 private boolean fBinary= false; 189 190 191 private RefactoringSearchCollector fCollector= null; 192 193 194 private int fGranularity= GRANULARITY_SEARCH_MATCH; 195 196 197 private boolean fGrouping= true; 198 199 200 private boolean fInaccurate= true; 201 202 203 private WorkingCopyOwner fOwner= null; 204 205 206 private SearchPattern fPattern= null; 207 208 209 private IRefactoringSearchRequestor fRequestor= new DefaultSearchRequestor(); 210 211 212 private IJavaSearchScope fScope= SearchEngine.createWorkspaceScope(); 213 214 215 private int fSeverity= RefactoringStatus.WARNING; 216 217 218 private RefactoringStatus fStatus= new RefactoringStatus(); 219 220 221 private ICompilationUnit[] fWorkingCopies= {}; 222 223 226 public RefactoringSearchEngine2() { 227 } 229 230 235 public RefactoringSearchEngine2(final SearchPattern pattern) { 236 Assert.isNotNull(pattern); 237 fPattern= pattern; 238 } 239 240 243 public final void clearResults() { 244 getCollector().clearResults(); 245 fStatus= new RefactoringStatus(); 246 } 247 248 255 public final ICompilationUnit[] getAffectedCompilationUnits() { 256 if (fGranularity == GRANULARITY_COMPILATION_UNIT) { 257 final Collection collection= getCollector().getCollectedMatches(); 258 final ICompilationUnit[] units= new ICompilationUnit[collection.size()]; 259 int index= 0; 260 for (final Iterator iterator= collection.iterator(); iterator.hasNext(); index++) 261 units[index]= (ICompilationUnit) iterator.next(); 262 return units; 263 } else { 264 final SearchResultGroup[] groups= getGroupedMatches(); 265 final ICompilationUnit[] units= new ICompilationUnit[groups.length]; 266 for (int index= 0; index < groups.length; index++) 267 units[index]= groups[index].getCompilationUnit(); 268 return units; 269 } 270 } 271 272 279 public final Map getAffectedProjects() { 280 final Map map= new HashMap (); 281 IJavaProject project= null; 282 ICompilationUnit unit= null; 283 if (fGranularity == GRANULARITY_COMPILATION_UNIT) { 284 final ICompilationUnit[] units= getAffectedCompilationUnits(); 285 for (int index= 0; index < units.length; index++) { 286 unit= units[index]; 287 project= unit.getJavaProject(); 288 if (project != null) { 289 Set set= (Set ) map.get(project); 290 if (set == null) { 291 set= new HashSet (); 292 map.put(project, set); 293 } 294 set.add(unit); 295 } 296 } 297 } else { 298 final SearchResultGroup[] groups= getGroupedMatches(); 299 SearchResultGroup group= null; 300 for (int index= 0; index < groups.length; index++) { 301 group= groups[index]; 302 unit= group.getCompilationUnit(); 303 if (unit != null) { 304 project= unit.getJavaProject(); 305 if (project != null) { 306 Set set= (Set ) map.get(project); 307 if (set == null) { 308 set= new HashSet (); 309 map.put(project, set); 310 } 311 set.add(group); 312 } 313 } 314 } 315 } 316 return map; 317 } 318 319 324 private RefactoringSearchCollector getCollector() { 325 if (fCollector == null) { 326 if (fGranularity == GRANULARITY_COMPILATION_UNIT) 327 fCollector= new RefactoringCompilationUnitCollector(); 328 else if (fGranularity == GRANULARITY_SEARCH_MATCH) 329 fCollector= new RefactoringSearchMatchCollector(); 330 else 331 Assert.isTrue(false); 332 } 333 return fCollector; 334 } 335 336 341 private SearchResultGroup[] getGroupedMatches() { 342 final Map grouped= new HashMap (); 343 List matches= null; 344 IResource resource= null; 345 SearchMatch match= null; 346 for (final Iterator iterator= getSearchMatches().iterator(); iterator.hasNext();) { 347 match= (SearchMatch) iterator.next(); 348 resource= match.getResource(); 349 if (!grouped.containsKey(resource)) 350 grouped.put(resource, new ArrayList (4)); 351 matches= (List ) grouped.get(resource); 352 matches.add(match); 353 } 354 if (fBinary) { 355 final Collection collection= getCollector().getBinaryResources(); 356 for (final Iterator iterator= grouped.keySet().iterator(); iterator.hasNext();) { 357 resource= (IResource) iterator.next(); 358 if (collection.contains(resource)) 359 iterator.remove(); 360 } 361 } 362 final SearchResultGroup[] result= new SearchResultGroup[grouped.keySet().size()]; 363 int index= 0; 364 for (final Iterator iterator= grouped.keySet().iterator(); iterator.hasNext();) { 365 resource= (IResource) iterator.next(); 366 matches= (List ) grouped.get(resource); 367 result[index++]= new SearchResultGroup(resource, ((SearchMatch[]) matches.toArray(new SearchMatch[matches.size()]))); 368 } 369 return result; 370 } 371 372 377 public final SearchPattern getPattern() { 378 return fPattern; 379 } 380 381 392 public final Object [] getResults() { 393 if (fGranularity == GRANULARITY_COMPILATION_UNIT) 394 return getAffectedCompilationUnits(); 395 else { 396 if (fGrouping) 397 return getGroupedMatches(); 398 else 399 return getUngroupedMatches(); 400 } 401 } 402 403 408 private Collection getSearchMatches() { 409 Collection results= null; 410 if (fInaccurate) { 411 results= new LinkedList (getCollector().getCollectedMatches()); 412 final Collection collection= getCollector().getInaccurateMatches(); 413 SearchMatch match= null; 414 for (final Iterator iterator= results.iterator(); iterator.hasNext();) { 415 match= (SearchMatch) iterator.next(); 416 if (collection.contains(match)) 417 iterator.remove(); 418 } 419 } else 420 results= getCollector().getCollectedMatches(); 421 return results; 422 } 423 424 429 public final RefactoringStatus getStatus() { 430 return fStatus; 431 } 432 433 438 private SearchMatch[] getUngroupedMatches() { 439 Collection results= null; 440 if (fBinary) { 441 results= new LinkedList (getSearchMatches()); 442 final Collection collection= getCollector().getBinaryResources(); 443 SearchMatch match= null; 444 for (final Iterator iterator= results.iterator(); iterator.hasNext();) { 445 match= (SearchMatch) iterator.next(); 446 if (collection.contains(match.getResource())) 447 iterator.remove(); 448 } 449 } else 450 results= getSearchMatches(); 451 final SearchMatch[] matches= new SearchMatch[results.size()]; 452 results.toArray(matches); 453 return matches; 454 } 455 456 462 public final void searchPattern(final IProgressMonitor monitor) throws JavaModelException { 463 Assert.isNotNull(fPattern); 464 try { 465 monitor.beginTask("", 1); monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_occurrences); 467 try { 468 SearchEngine engine= null; 469 if (fOwner != null) 470 engine= new SearchEngine(fOwner); 471 else 472 engine= new SearchEngine(fWorkingCopies); 473 engine.search(fPattern, SearchUtils.getDefaultSearchParticipants(), fScope, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 474 } catch (CoreException exception) { 475 throw new JavaModelException(exception); 476 } 477 } finally { 478 monitor.done(); 479 } 480 } 481 482 489 public final void searchReferencedFields(final IJavaElement element, final IProgressMonitor monitor) throws JavaModelException { 490 Assert.isNotNull(element); 491 try { 492 monitor.beginTask("", 1); monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_fields); 494 try { 495 SearchEngine engine= null; 496 if (fOwner != null) 497 engine= new SearchEngine(fOwner); 498 else 499 engine= new SearchEngine(fWorkingCopies); 500 engine.searchDeclarationsOfAccessedFields(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 501 } catch (CoreException exception) { 502 throw new JavaModelException(exception); 503 } 504 } finally { 505 monitor.done(); 506 } 507 } 508 509 516 public final void searchReferencedMethods(final IJavaElement element, final IProgressMonitor monitor) throws JavaModelException { 517 Assert.isNotNull(element); 518 try { 519 monitor.beginTask("", 1); monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_methods); 521 try { 522 SearchEngine engine= null; 523 if (fOwner != null) 524 engine= new SearchEngine(fOwner); 525 else 526 engine= new SearchEngine(fWorkingCopies); 527 engine.searchDeclarationsOfSentMessages(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 528 } catch (CoreException exception) { 529 throw new JavaModelException(exception); 530 } 531 } finally { 532 monitor.done(); 533 } 534 } 535 536 543 public final void searchReferencedTypes(final IJavaElement element, final IProgressMonitor monitor) throws JavaModelException { 544 Assert.isNotNull(element); 545 try { 546 monitor.beginTask("", 1); monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_types); 548 try { 549 SearchEngine engine= null; 550 if (fOwner != null) 551 engine= new SearchEngine(fOwner); 552 else 553 engine= new SearchEngine(fWorkingCopies); 554 engine.searchDeclarationsOfReferencedTypes(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 555 } catch (CoreException exception) { 556 throw new JavaModelException(exception); 557 } 558 } finally { 559 monitor.done(); 560 } 561 } 562 563 571 public final void setAndPattern(final SearchPattern first, final SearchPattern second) { 572 Assert.isNotNull(first); 573 Assert.isNotNull(second); 574 fPattern= SearchPattern.createAndPattern(first, second); 575 } 576 577 585 public final void setFiltering(final boolean inaccurate, final boolean binary) { 586 fInaccurate= inaccurate; 587 fBinary= binary; 588 } 589 590 597 public final void setGranularity(final int granularity) { 598 Assert.isTrue(granularity == GRANULARITY_COMPILATION_UNIT || granularity == GRANULARITY_SEARCH_MATCH); 599 fGranularity= granularity; 600 } 601 602 609 public final void setWorkingCopies(final ICompilationUnit[] copies) { 610 Assert.isNotNull(copies); 611 fWorkingCopies= new ICompilationUnit[copies.length]; 612 System.arraycopy(copies, 0, fWorkingCopies, 0, copies.length); 613 } 614 615 622 public final void setGrouping(final boolean grouping) { 623 fGrouping= grouping; 624 } 625 626 634 public final void setOrPattern(final SearchPattern first, final SearchPattern second) { 635 Assert.isNotNull(first); 636 Assert.isNotNull(second); 637 fPattern= SearchPattern.createOrPattern(first, second); 638 } 639 640 647 public final void setOwner(final WorkingCopyOwner owner) { 648 fOwner= owner; 649 } 650 651 659 public final void setPattern(final IJavaElement[] elements, final int limitTo) { 660 Assert.isNotNull(elements); 661 Assert.isTrue(elements.length > 0); 662 SearchPattern pattern= SearchPattern.createPattern(elements[0], limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE); 663 IJavaElement element= null; 664 for (int index= 1; index < elements.length; index++) { 665 element= elements[index]; 666 pattern= SearchPattern.createOrPattern(pattern, SearchPattern.createPattern(element, limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE)); 667 } 668 setPattern(pattern); 669 } 670 671 678 public final void setPattern(final SearchPattern pattern) { 679 Assert.isNotNull(pattern); 680 fPattern= pattern; 681 } 682 683 690 public final void setRequestor(final IRefactoringSearchRequestor requestor) { 691 Assert.isNotNull(requestor); 692 fRequestor= requestor; 693 } 694 695 702 public final void setScope(final IJavaSearchScope scope) { 703 Assert.isNotNull(scope); 704 fScope= scope; 705 } 706 707 714 public final void setSeverity(final int severity) { 715 Assert.isTrue(severity == RefactoringStatus.WARNING || severity == RefactoringStatus.INFO || severity == RefactoringStatus.FATAL || severity == RefactoringStatus.ERROR); 716 fSeverity= severity; 717 } 718 719 726 public final void setStatus(final RefactoringStatus status) { 727 Assert.isNotNull(status); 728 fStatus= status; 729 } 730 } 731 | Popular Tags |