private RefactoringStatus analyzeRenameChanges(IProgressMonitor pm) throws CoreException {
    ICompilationUnit[] newDeclarationWCs = null;
    try {
      pm.beginTask("", 4); // $NON-NLS-1$
      RefactoringStatus result = new RefactoringStatus();
      ICompilationUnit[] declarationCUs = getDeclarationCUs();
      newDeclarationWCs =
          RenameAnalyzeUtil.createNewWorkingCopies(
              declarationCUs, fChangeManager, fWorkingCopyOwner, new SubProgressMonitor(pm, 1));

      IMethod[] wcOldMethods = new IMethod[fMethodsToRename.size()];
      IMethod[] wcNewMethods = new IMethod[fMethodsToRename.size()];
      int i = 0;
      for (Iterator<IMethod> iter = fMethodsToRename.iterator(); iter.hasNext(); i++) {
        IMethod method = iter.next();
        ICompilationUnit newCu =
            RenameAnalyzeUtil.findWorkingCopyForCu(newDeclarationWCs, method.getCompilationUnit());
        IType typeWc =
            (IType) JavaModelUtil.findInCompilationUnit(newCu, method.getDeclaringType());
        if (typeWc == null) {
          // should not happen
          i--;
          wcOldMethods =
              CollectionsUtil.toArray(
                  Arrays.asList(wcOldMethods).subList(0, wcOldMethods.length - 1), IMethod.class);
          wcNewMethods =
              CollectionsUtil.toArray(
                  Arrays.asList(wcNewMethods).subList(0, wcNewMethods.length - 1), IMethod.class);
          continue;
        }
        wcOldMethods[i] = getMethodInWorkingCopy(method, getCurrentElementName(), typeWc);
        wcNewMethods[i] = getMethodInWorkingCopy(method, getNewElementName(), typeWc);
      }

      //			SearchResultGroup[] newOccurrences= findNewOccurrences(newMethods, newDeclarationWCs, new
      // SubProgressMonitor(pm, 3));
      SearchResultGroup[] newOccurrences =
          batchFindNewOccurrences(
              wcNewMethods, wcOldMethods, newDeclarationWCs, new SubProgressMonitor(pm, 3), result);

      result.merge(
          RenameAnalyzeUtil.analyzeRenameChanges2(
              fChangeManager, fOccurrences, newOccurrences, getNewElementName()));
      return result;
    } finally {
      pm.done();
      if (newDeclarationWCs != null) {
        for (int i = 0; i < newDeclarationWCs.length; i++) {
          newDeclarationWCs[i].discardWorkingCopy();
        }
      }
    }
  }
Esempio n. 2
0
  private SearchResultGroup[] getNewReferences(
      IProgressMonitor pm,
      RefactoringStatus status,
      WorkingCopyOwner owner,
      ICompilationUnit[] newWorkingCopies)
      throws CoreException {
    pm.beginTask("", 2); // $NON-NLS-1$
    ICompilationUnit declaringCuWorkingCopy =
        RenameAnalyzeUtil.findWorkingCopyForCu(newWorkingCopies, fField.getCompilationUnit());
    if (declaringCuWorkingCopy == null) return new SearchResultGroup[0];

    IField field = getFieldInWorkingCopy(declaringCuWorkingCopy, getNewElementName());
    if (field == null || !field.exists()) return new SearchResultGroup[0];

    CollectingSearchRequestor requestor = null;
    if (fDelegateUpdating
        && RefactoringAvailabilityTester.isDelegateCreationAvailable(getField())) {
      // There will be two new matches inside the delegate (the invocation
      // and the javadoc) which are OK and must not be reported.
      final IField oldField =
          getFieldInWorkingCopy(declaringCuWorkingCopy, getCurrentElementName());
      requestor =
          new CollectingSearchRequestor() {
            public void acceptSearchMatch(SearchMatch match) throws CoreException {
              if (!oldField.equals(match.getElement())) super.acceptSearchMatch(match);
            }
          };
    } else requestor = new CollectingSearchRequestor();

    SearchPattern newPattern = SearchPattern.createPattern(field, IJavaSearchConstants.REFERENCES);
    IJavaSearchScope scope = RefactoringScopeFactory.create(fField, true, true);
    return RefactoringSearchEngine.search(
        newPattern, owner, scope, requestor, new SubProgressMonitor(pm, 1), status);
  }
Esempio n. 3
0
  // ----------------
  private RefactoringStatus analyzeRenameChanges(IProgressMonitor pm) throws CoreException {
    ICompilationUnit[] newWorkingCopies = null;
    WorkingCopyOwner newWCOwner = new WorkingCopyOwner() {
          /* must subclass */
        };
    try {
      pm.beginTask("", 2); // $NON-NLS-1$
      RefactoringStatus result = new RefactoringStatus();
      SearchResultGroup[] oldReferences = fReferences;

      List compilationUnitsToModify = new ArrayList();
      if (fIsComposite) {
        // limited change set, no accessors.
        for (int i = 0; i < oldReferences.length; i++)
          compilationUnitsToModify.add(oldReferences[i].getCompilationUnit());
        compilationUnitsToModify.add(fField.getCompilationUnit());
      } else {
        // include all cus, including accessors
        compilationUnitsToModify.addAll(Arrays.asList(fChangeManager.getAllCompilationUnits()));
      }

      newWorkingCopies =
          RenameAnalyzeUtil.createNewWorkingCopies(
              (ICompilationUnit[])
                  compilationUnitsToModify.toArray(
                      new ICompilationUnit[compilationUnitsToModify.size()]),
              fChangeManager,
              newWCOwner,
              new SubProgressMonitor(pm, 1));

      SearchResultGroup[] newReferences =
          getNewReferences(new SubProgressMonitor(pm, 1), result, newWCOwner, newWorkingCopies);
      result.merge(
          RenameAnalyzeUtil.analyzeRenameChanges2(
              fChangeManager, oldReferences, newReferences, getNewElementName()));
      return result;
    } finally {
      pm.done();
      if (newWorkingCopies != null) {
        for (int i = 0; i < newWorkingCopies.length; i++) {
          newWorkingCopies[i].discardWorkingCopy();
        }
      }
    }
  }
 static RefactoringStatus analyzeRenameChanges(
     TextChangeManager manager,
     SearchResultGroup[] oldOccurrences,
     SearchResultGroup[] newOccurrences) {
   RefactoringStatus result = new RefactoringStatus();
   for (int i = 0; i < oldOccurrences.length; i++) {
     SearchResultGroup oldGroup = oldOccurrences[i];
     SearchMatch[] oldSearchResults = oldGroup.getSearchResults();
     ICompilationUnit cunit = oldGroup.getCompilationUnit();
     if (cunit == null) continue;
     for (int j = 0; j < oldSearchResults.length; j++) {
       SearchMatch oldSearchResult = oldSearchResults[j];
       if (!RenameAnalyzeUtil.existsInNewOccurrences(oldSearchResult, newOccurrences, manager)) {
         addShadowsError(cunit, oldSearchResult, result);
       }
     }
   }
   return result;
 }
  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;
  }