Esempio n. 1
0
  private RefactoringStatus addDelegates() throws JavaModelException, CoreException {

    RefactoringStatus status = new RefactoringStatus();
    CompilationUnitRewrite rewrite = new CompilationUnitRewrite(fField.getCompilationUnit());
    rewrite.setResolveBindings(true);

    // add delegate for the field
    if (RefactoringAvailabilityTester.isDelegateCreationAvailable(fField)) {
      FieldDeclaration fieldDeclaration =
          ASTNodeSearchUtil.getFieldDeclarationNode(fField, rewrite.getRoot());
      if (fieldDeclaration.fragments().size() > 1) {
        status.addWarning(
            Messages.format(
                RefactoringCoreMessages
                    .DelegateCreator_cannot_create_field_delegate_more_than_one_fragment,
                BasicElementLabels.getJavaElementName(fField.getElementName())),
            JavaStatusContext.create(fField));
      } else if (((VariableDeclarationFragment) fieldDeclaration.fragments().get(0))
              .getInitializer()
          == null) {
        status.addWarning(
            Messages.format(
                RefactoringCoreMessages.DelegateCreator_cannot_create_field_delegate_no_initializer,
                BasicElementLabels.getJavaElementName(fField.getElementName())),
            JavaStatusContext.create(fField));
      } else {
        DelegateFieldCreator creator = new DelegateFieldCreator();
        creator.setDeclareDeprecated(fDelegateDeprecation);
        creator.setDeclaration(fieldDeclaration);
        creator.setNewElementName(getNewElementName());
        creator.setSourceRewrite(rewrite);
        creator.prepareDelegate();
        creator.createEdit();
      }
    }

    // add delegates for getter and setter methods
    // there may be getters even if the field is static final
    if (getGetter() != null && fRenameGetter)
      addMethodDelegate(getGetter(), getNewGetterName(), rewrite);
    if (getSetter() != null && fRenameSetter)
      addMethodDelegate(getSetter(), getNewSetterName(), rewrite);

    final CompilationUnitChange change = rewrite.createChange(true);
    if (change != null) {
      change.setKeepPreviewEdits(true);
      fChangeManager.manage(fField.getCompilationUnit(), change);
    }

    return status;
  }
  /**
   * Add occurrences
   *
   * @param manager the text change manager
   * @param pm the progress monitor
   * @param status the status
   * @throws CoreException if change creation failed
   */
  protected void addOccurrences(
      TextChangeManager manager, IProgressMonitor pm, RefactoringStatus status)
      throws CoreException /*thrown in subtype*/ {
    pm.beginTask("", fOccurrences.length); // $NON-NLS-1$
    for (int i = 0; i < fOccurrences.length; i++) {
      ICompilationUnit cu = fOccurrences[i].getCompilationUnit();
      if (cu == null) continue;

      SearchMatch[] results = fOccurrences[i].getSearchResults();

      // Split matches into declaration and non-declaration matches

      List<SearchMatch> declarationsInThisCu = new ArrayList<>();
      List<SearchMatch> referencesInThisCu = new ArrayList<>();

      for (int j = 0; j < results.length; j++) {
        if (results[j] instanceof MethodDeclarationMatch) declarationsInThisCu.add(results[j]);
        else referencesInThisCu.add(results[j]);
      }

      // First, handle the declarations
      if (declarationsInThisCu.size() > 0) {

        if (fDelegateUpdating) {
          // Update with delegates
          CompilationUnitRewrite rewrite = new CompilationUnitRewrite(cu);
          rewrite.setResolveBindings(true);

          for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) {
            SearchMatch element = iter.next();
            MethodDeclaration method =
                ASTNodeSearchUtil.getMethodDeclarationNode(
                    (IMethod) element.getElement(), rewrite.getRoot());
            DelegateCreator creator = new DelegateMethodCreator();
            creator.setDeclareDeprecated(fDelegateDeprecation);
            creator.setDeclaration(method);
            creator.setSourceRewrite(rewrite);
            creator.setNewElementName(getNewElementName());
            creator.prepareDelegate();
            creator.createEdit();
          }
          // Need to handle all delegates first as this
          // creates a completely new change object.
          TextChange changeForThisCu = rewrite.createChange(true);
          changeForThisCu.setKeepPreviewEdits(true);
          manager.manage(cu, changeForThisCu);
        }

        // Update the normal methods
        for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) {
          SearchMatch element = iter.next();
          simpleUpdate(element, cu, manager.get(cu));
        }
      }

      // Second, handle references
      if (fUpdateReferences) {
        for (Iterator<SearchMatch> iter = referencesInThisCu.iterator(); iter.hasNext(); ) {
          SearchMatch element = iter.next();
          simpleUpdate(element, cu, manager.get(cu));
        }
      }

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