private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm)
     throws CoreException {
   final List<IMethod> results = new ArrayList<>();
   SearchPattern pattern = createNewMethodPattern();
   IJavaSearchScope scope = RefactoringScopeFactory.create(getMethod().getJavaProject());
   SearchRequestor requestor =
       new SearchRequestor() {
         @Override
         public void acceptSearchMatch(SearchMatch match) throws CoreException {
           Object method = match.getElement();
           if (method
               instanceof
               IMethod) // check for bug 90138: [refactoring] [rename] Renaming method throws
                        // internal exception
           results.add((IMethod) method);
           else
             JavaPlugin.logErrorMessage(
                 "Unexpected element in search match: " + match.toString()); // $NON-NLS-1$
         }
       };
   new SearchEngine()
       .search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
   return results.toArray(new IMethod[results.size()]);
 }
 private IType[] searchForOuterTypesOfReferences(IMethod[] newNameMethods, IProgressMonitor pm)
     throws CoreException {
   final Set<IType> outerTypesOfReferences = new HashSet<>();
   SearchPattern pattern =
       RefactoringSearchEngine.createOrPattern(newNameMethods, IJavaSearchConstants.REFERENCES);
   IJavaSearchScope scope = createRefactoringScope(getMethod());
   SearchRequestor requestor =
       new SearchRequestor() {
         @Override
         public void acceptSearchMatch(SearchMatch match) throws CoreException {
           Object element = match.getElement();
           if (!(element instanceof IMember))
             return; // e.g. an IImportDeclaration for a static method import
           IMember member = (IMember) element;
           IType declaring = member.getDeclaringType();
           if (declaring == null) return;
           IType outer = declaring.getDeclaringType();
           if (outer != null) outerTypesOfReferences.add(declaring);
         }
       };
   new SearchEngine()
       .search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
   return outerTypesOfReferences.toArray(new IType[outerTypesOfReferences.size()]);
 }
Exemplo n.º 3
0
 private static TextChange getTextChange(SearchMatch searchResult, TextChangeManager manager) {
   ICompilationUnit cu = SearchUtils.getCompilationUnit(searchResult);
   if (cu == null) return null;
   return manager.get(cu);
 }
  private SearchResultGroup[] batchFindNewOccurrences(
      IMethod[] wcNewMethods,
      final IMethod[] wcOldMethods,
      ICompilationUnit[] newDeclarationWCs,
      IProgressMonitor pm,
      RefactoringStatus status)
      throws CoreException {
    pm.beginTask("", 2); // $NON-NLS-1$

    SearchPattern refsPattern =
        RefactoringSearchEngine.createOrPattern(wcNewMethods, IJavaSearchConstants.REFERENCES);
    SearchParticipant[] searchParticipants = SearchUtils.getDefaultSearchParticipants();
    IJavaSearchScope scope = RefactoringScopeFactory.create(wcNewMethods);

    MethodOccurenceCollector requestor;
    if (getDelegateUpdating()) {
      // There will be two new matches inside the delegate(s) (the invocation
      // and the javadoc) which are OK and must not be reported.
      // Note that except these ocurrences, the delegate bodies are empty
      // (as they were created this way).
      requestor =
          new MethodOccurenceCollector(getNewElementName()) {
            @Override
            public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match)
                throws CoreException {
              for (int i = 0; i < wcOldMethods.length; i++)
                if (wcOldMethods[i].equals(match.getElement())) return;
              super.acceptSearchMatch(unit, match);
            }
          };
    } else requestor = new MethodOccurenceCollector(getNewElementName());

    SearchEngine searchEngine = new SearchEngine(fWorkingCopyOwner);

    ArrayList<ICompilationUnit> needWCs = new ArrayList<>();
    HashSet<ICompilationUnit> declaringCUs = new HashSet<>(newDeclarationWCs.length);
    for (int i = 0; i < newDeclarationWCs.length; i++)
      declaringCUs.add(newDeclarationWCs[i].getPrimary());
    for (int i = 0; i < fOccurrences.length; i++) {
      ICompilationUnit cu = fOccurrences[i].getCompilationUnit();
      if (!declaringCUs.contains(cu)) needWCs.add(cu);
    }
    ICompilationUnit[] otherWCs = null;
    try {
      otherWCs =
          RenameAnalyzeUtil.createNewWorkingCopies(
              needWCs.toArray(new ICompilationUnit[needWCs.size()]),
              fChangeManager,
              fWorkingCopyOwner,
              new SubProgressMonitor(pm, 1));
      searchEngine.search(
          refsPattern, searchParticipants, scope, requestor, new SubProgressMonitor(pm, 1));
    } finally {
      pm.done();
      if (otherWCs != null) {
        for (int i = 0; i < otherWCs.length; i++) {
          otherWCs[i].discardWorkingCopy();
        }
      }
    }
    SearchResultGroup[] newResults =
        RefactoringSearchEngine.groupByCu(requestor.getResults(), status);
    return newResults;
  }