Example #1
0
 private RefactoringStatus checkAccessorDeclarations(IProgressMonitor pm, IMethod existingAccessor)
     throws CoreException {
   RefactoringStatus result = new RefactoringStatus();
   SearchPattern pattern =
       SearchPattern.createPattern(
           existingAccessor,
           IJavaSearchConstants.DECLARATIONS,
           SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
   IJavaSearchScope scope = SearchEngine.createHierarchyScope(fField.getDeclaringType());
   SearchResultGroup[] groupDeclarations =
       RefactoringSearchEngine.search(pattern, scope, pm, result);
   Assert.isTrue(groupDeclarations.length > 0);
   if (groupDeclarations.length != 1) {
     String message =
         Messages.format(
             RefactoringCoreMessages.RenameFieldRefactoring_overridden,
             JavaElementUtil.createMethodSignature(existingAccessor));
     result.addError(message);
   } else {
     SearchResultGroup group = groupDeclarations[0];
     Assert.isTrue(group.getSearchResults().length > 0);
     if (group.getSearchResults().length != 1) {
       String message =
           Messages.format(
               RefactoringCoreMessages.RenameFieldRefactoring_overridden_or_overrides,
               JavaElementUtil.createMethodSignature(existingAccessor));
       result.addError(message);
     }
   }
   return result;
 }
 private void removeReferences(ICompilationUnit cu, Set<TextMatch> matches) {
   for (int i = 0; i < fReferences.length; i++) {
     SearchResultGroup group = fReferences[i];
     if (cu.equals(group.getCompilationUnit())) {
       removeReferences(matches, group);
     }
   }
 }
  private static boolean existsInNewOccurrences(
      SearchMatch searchResult, SearchResultGroup[] newOccurrences, TextChangeManager manager) {
    SearchResultGroup newGroup = findOccurrenceGroup(searchResult.getResource(), newOccurrences);
    if (newGroup == null) return false;

    IRegion oldEditRange = getCorrespondingEditChangeRange(searchResult, manager);
    if (oldEditRange == null) return false;

    SearchMatch[] newSearchResults = newGroup.getSearchResults();
    int oldRangeOffset = oldEditRange.getOffset();
    for (int i = 0; i < newSearchResults.length; i++) {
      if (newSearchResults[i].getOffset() == oldRangeOffset) return true;
    }
    return false;
  }
  // TODO: Currently filters out declarations (MethodDeclarationMatch, FieldDeclarationMatch).
  // Long term solution: only pass reference search results in.
  static RefactoringStatus analyzeRenameChanges2(
      TextChangeManager manager,
      SearchResultGroup[] oldReferences,
      SearchResultGroup[] newReferences,
      String newElementName) {
    RefactoringStatus result = new RefactoringStatus();

    HashMap<ICompilationUnit, SearchMatch[]> cuToNewResults = new HashMap<>(newReferences.length);
    for (int i1 = 0; i1 < newReferences.length; i1++) {
      ICompilationUnit cu = newReferences[i1].getCompilationUnit();
      if (cu != null) cuToNewResults.put(cu.getPrimary(), newReferences[i1].getSearchResults());
    }

    for (int i = 0; i < oldReferences.length; i++) {
      SearchResultGroup oldGroup = oldReferences[i];
      SearchMatch[] oldMatches = oldGroup.getSearchResults();
      ICompilationUnit cu = oldGroup.getCompilationUnit();
      if (cu == null) continue;

      SearchMatch[] newSearchMatches = cuToNewResults.remove(cu);
      if (newSearchMatches == null) {
        for (int j = 0; j < oldMatches.length; j++) {
          SearchMatch oldMatch = oldMatches[j];
          addShadowsError(cu, oldMatch, result);
        }
      } else {
        analyzeChanges(cu, manager.get(cu), oldMatches, newSearchMatches, newElementName, result);
      }
    }

    for (Iterator<Entry<ICompilationUnit, SearchMatch[]>> iter =
            cuToNewResults.entrySet().iterator();
        iter.hasNext(); ) {
      Entry<ICompilationUnit, SearchMatch[]> entry = iter.next();
      ICompilationUnit cu = entry.getKey();
      SearchMatch[] newSearchMatches = entry.getValue();
      for (int i = 0; i < newSearchMatches.length; i++) {
        SearchMatch newMatch = newSearchMatches[i];
        addReferenceShadowedError(cu, newMatch, newElementName, result);
      }
    }
    return result;
  }
 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 void removeReferences(Set<TextMatch> matches, SearchResultGroup group) {
   SearchMatch[] searchResults = group.getSearchResults();
   for (int r = 0; r < searchResults.length; r++) {
     // int start= searchResults[r].getStart(); // doesn't work for pack.ReferencedType
     int unqualifiedStart =
         searchResults[r].getOffset() + searchResults[r].getLength() - fCurrentNameLength;
     for (Iterator<TextMatch> iter = matches.iterator(); iter.hasNext(); ) {
       TextMatch element = iter.next();
       if (element.getStartPosition() == unqualifiedStart) iter.remove();
     }
   }
 }