Exemplo n.º 1
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);
  }
Exemplo n.º 2
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;
 }
Exemplo n.º 3
0
  private void addAccessorOccurrences(
      IProgressMonitor pm,
      IMethod accessor,
      String editName,
      String newAccessorName,
      RefactoringStatus status)
      throws CoreException {
    Assert.isTrue(accessor.exists());

    IJavaSearchScope scope = RefactoringScopeFactory.create(accessor);
    SearchPattern pattern =
        SearchPattern.createPattern(
            accessor,
            IJavaSearchConstants.ALL_OCCURRENCES,
            SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    SearchResultGroup[] groupedResults =
        RefactoringSearchEngine.search(
            pattern, scope, new MethodOccurenceCollector(accessor.getElementName()), pm, status);

    for (int i = 0; i < groupedResults.length; i++) {
      ICompilationUnit cu = groupedResults[i].getCompilationUnit();
      if (cu == null) continue;
      SearchMatch[] results = groupedResults[i].getSearchResults();
      for (int j = 0; j < results.length; j++) {
        SearchMatch searchResult = results[j];
        TextEdit edit =
            new ReplaceEdit(searchResult.getOffset(), searchResult.getLength(), newAccessorName);
        addTextEdit(fChangeManager.get(cu), editName, edit);
      }
    }
  }
 private SearchResultGroup[] getOccurrences(
     IProgressMonitor pm, RefactoringStatus status, ReferencesInBinaryContext binaryRefs)
     throws CoreException {
   SearchPattern pattern = createOccurrenceSearchPattern();
   return RefactoringSearchEngine.search(
       pattern,
       createRefactoringScope(),
       new MethodOccurenceCollector(getMethod().getElementName(), binaryRefs),
       pm,
       status);
 }
Exemplo n.º 5
0
  private SearchResultGroup[] getReferences(IProgressMonitor pm, RefactoringStatus status)
      throws CoreException {
    String binaryRefsDescription =
        Messages.format(
            RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description,
            BasicElementLabels.getJavaElementName(getCurrentElementName()));
    ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);

    SearchResultGroup[] result =
        RefactoringSearchEngine.search(
            createSearchPattern(),
            createRefactoringScope(),
            new CuCollectingSearchRequestor(binaryRefs),
            pm,
            status);
    binaryRefs.addErrorIfNecessary(status);

    return result;
  }
 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()]);
 }
  @SuppressWarnings({"unchecked"})
  @Override
  public RefactoringStatus checkFinalConditions(IProgressMonitor pm)
      throws CoreException, OperationCanceledException {
    RefactoringStatus result = new RefactoringStatus();
    fChangeManager.clear();
    pm.beginTask("", 12);
    pm.setTaskName("Convert to AtomicInteger checking preconditions");
    pm.worked(1);
    if (result.hasFatalError()) return result;
    pm.setTaskName("ConvertToAtomicInteger searching for cunits");
    final SubProgressMonitor subPm = new SubProgressMonitor(pm, 5);
    ICompilationUnit[] affectedCUs =
        RefactoringSearchEngine.findAffectedCompilationUnits(
            SearchPattern.createPattern(targetClass, IJavaSearchConstants.ALL_OCCURRENCES),
            RefactoringScopeFactory.create(targetClass, true),
            subPm,
            result,
            true);

    if (result.hasFatalError()) return result;

    pm.setTaskName("Analyzing the field");
    IProgressMonitor sub = new SubProgressMonitor(pm, 5);
    sub.beginTask("", affectedCUs.length);

    List ownerDescriptions = new ArrayList();
    ICompilationUnit owner = targetClass.getCompilationUnit();

    for (int i = 0; i < affectedCUs.length; i++) {
      ICompilationUnit unit = affectedCUs[i];
      sub.subTask(unit.getElementName());
      CompilationUnit root = null;
      ASTRewrite rewriter = null;
      List descriptions;
      if (owner.equals(unit)) {
        root = fRoot;
        rewriter = fRewriter;
        descriptions = ownerDescriptions;
      } else {
        root = new RefactoringASTParser(AST.JLS3).parse(unit, true);
        rewriter = ASTRewrite.create(root.getAST());
        descriptions = new ArrayList();
      }
      checkCompileErrors(result, root, unit);

      // We will perform a different set of analysis and rewrites for the compilation unit
      // containing the
      // target class and other compilation units
      if (owner.equals(unit)) {

        // Analysis passes
        ClassMutatorAnalysis mutatorAnalysis = new ClassMutatorAnalysis(targetClass, pm);
        mutatorAnalysis.findMutators();

        ClassConstructorAnalysis constructorAnalysis =
            new ClassConstructorAnalysis(targetClassDeclaration);
        targetClassDeclaration.accept(constructorAnalysis);

        // Rewrite pass
        MakeClassImmutableRewriter immutableRewriter =
            new MakeClassImmutableRewriter(this, unit, rewriter);
        immutableRewriter.rewrite(targetClassDeclaration, mutatorAnalysis, constructorAnalysis);

        result.merge(immutableRewriter.getStatus());
        if (result.hasFatalError()) {
          fChangeManager.clear();
          return result;
        }
      } else {

        // descriptions.addAll(immutableRewriter.getGroupDescriptions());
        createEdits(unit, rewriter, descriptions);
      }

      sub.worked(1);
      if (pm.isCanceled()) throw new OperationCanceledException();
    }

    createEdits(owner, fRewriter, ownerDescriptions);

    sub.done();
    IFile[] filesToBeModified = ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits());
    result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
    if (result.hasFatalError()) return result;
    ResourceChangeChecker.checkFilesToBeChanged(filesToBeModified, new SubProgressMonitor(pm, 1));
    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;
  }
 private SearchPattern createOccurrenceSearchPattern() {
   HashSet<IMethod> methods = new HashSet<>(fMethodsToRename);
   methods.add(fMethod);
   IMethod[] ms = methods.toArray(new IMethod[methods.size()]);
   return RefactoringSearchEngine.createOrPattern(ms, IJavaSearchConstants.ALL_OCCURRENCES);
 }