/**
   * @param elements
   * @param expectedInitialStatus
   * @param expectedFinalStatus
   * @return <code>true</code> iff performed
   * @throws CoreException
   */
  private boolean perform(
      IJavaElement[] elements, int expectedInitialStatus, int expectedFinalStatus)
      throws CoreException {
    InferTypeArgumentsRefactoring refactoring =
        ((RefactoringAvailabilityTester.isInferTypeArgumentsAvailable(elements))
            ? new InferTypeArgumentsRefactoring(elements)
            : null);

    NullProgressMonitor pm = new NullProgressMonitor();
    RefactoringStatus initialStatus = refactoring.checkInitialConditions(pm);
    assertEquals(
        "wrong initial condition status: " + initialStatus,
        expectedInitialStatus,
        initialStatus.getSeverity());
    if (!initialStatus.isOK()) return false;

    refactoring.setAssumeCloneReturnsSameType(fAssumeCloneReturnsSameType);
    refactoring.setLeaveUnconstrainedRaw(fLeaveUnconstrainedRaw);

    PerformRefactoringOperation op =
        new PerformRefactoringOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
    JavaCore.run(op, new NullProgressMonitor());
    RefactoringStatus finalStatus = op.getConditionStatus();
    assertEquals(
        "wrong final condition status: " + finalStatus,
        expectedFinalStatus,
        finalStatus.getSeverity());
    if (finalStatus.getSeverity() == RefactoringStatus.FATAL) return false;

    assertTrue(
        "Validation check failed: " + op.getValidationStatus(),
        !op.getValidationStatus().hasFatalError());
    assertNotNull("No Undo", op.getUndoChange());
    return true;
  }
 public void testCheckAmbiguityInRoleMethodSpec3() throws Exception {
   // check ambiguous role method specifier in callout binding of T3.R2
   RefactoringStatus expected =
       RefactoringUtil.addAmbiguityFatalError(null, new ExtractMethodAmbuguityMsgCreator());
   RefactoringStatus actual =
       RefactoringUtil.checkForAmbiguousRoleMethodSpecs(
           _t3r2, "m", new ExtractMethodAmbuguityMsgCreator());
   assertEquals(expected.getSeverity(), actual.getSeverity());
   assertNotNull(
       expected.getEntryMatchingCode(
           Corext.getPluginId(), OTRefactoringStatusCodes.AMBIGUOUS_METHOD_SPECIFIER));
   assertNotNull(
       actual.getEntryMatchingCode(
           Corext.getPluginId(), OTRefactoringStatusCodes.AMBIGUOUS_METHOD_SPECIFIER));
 }
  private void failHelper1(
      int startLine,
      int startColumn,
      int endLine,
      int endColumn,
      boolean makeFinal,
      String className,
      int visibility,
      int expectedSeverity)
      throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), false, true);
    ISourceRange selection =
        TextRangeUtil.getSelection(cu, startLine, startColumn, endLine, endColumn);
    ConvertAnonymousToNestedRefactoring ref =
        new ConvertAnonymousToNestedRefactoring(cu, selection.getOffset(), selection.getLength());

    RefactoringStatus preconditionResult = ref.checkInitialConditions(new NullProgressMonitor());
    if (preconditionResult.isOK()) preconditionResult = null;
    assertEquals("activation was supposed to be successful", null, preconditionResult);

    ref.setClassName(className);
    ref.setDeclareFinal(makeFinal);
    ref.setVisibility(visibility);

    if (preconditionResult == null)
      preconditionResult = ref.checkFinalConditions(new NullProgressMonitor());
    else preconditionResult.merge(ref.checkFinalConditions(new NullProgressMonitor()));
    if (preconditionResult.isOK()) preconditionResult = null;
    assertNotNull("precondition was supposed to fail", preconditionResult);

    assertEquals("incorrect severity:", expectedSeverity, preconditionResult.getSeverity());
  }
 public void testCheckAmbiguityInBaseMethodSpec2() throws Exception {
   // check ambiguous base method specifier in callout binding of T3.R2
   RefactoringStatus expected =
       RefactoringUtil.addAmbiguityFatalError(null, new ExtractMethodAmbuguityMsgCreator());
   ArrayList<IRoleType> boundRole = new ArrayList<IRoleType>();
   boundRole.add(_t3r2);
   RefactoringStatus actual =
       RefactoringUtil.checkForAmbiguousBaseMethodSpecs(
           boundRole, "f", "", new ExtractMethodAmbuguityMsgCreator());
   assertEquals(expected.getSeverity(), actual.getSeverity());
   assertNotNull(
       expected.getEntryMatchingCode(
           Corext.getPluginId(), OTRefactoringStatusCodes.AMBIGUOUS_METHOD_SPECIFIER));
   assertNotNull(
       actual.getEntryMatchingCode(
           Corext.getPluginId(), OTRefactoringStatusCodes.AMBIGUOUS_METHOD_SPECIFIER));
 }
  private void failActivationHelper(
      int startLine, int startColumn, int endLine, int endColumn, int expectedSeverity)
      throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), false, true);
    ISourceRange selection =
        TextRangeUtil.getSelection(cu, startLine, startColumn, endLine, endColumn);
    ConvertAnonymousToNestedRefactoring ref =
        new ConvertAnonymousToNestedRefactoring(cu, selection.getOffset(), selection.getLength());

    RefactoringStatus preconditionResult = ref.checkInitialConditions(new NullProgressMonitor());
    assertEquals(
        "activation was supposed to fail", expectedSeverity, preconditionResult.getSeverity());
  }
 /* test overloading of private base method */
 public void testCheckOverloading9() throws Exception {
   // focus type -> C
   ICompilationUnit cu = _c.getCompilationUnit();
   int[] selection = getSelection(cu.getSource());
   _refactoring = new ExtractMethodRefactoring(cu, selection[0], selection[1]);
   _refactoring.setMethodName("n");
   _refactoring.setVisibility(Modifier.PRIVATE);
   _refactoring.checkInitialConditions(new NullProgressMonitor());
   IMethod[] inheritedMethods = RefactoringUtil.getInheritedMethods(_c, true, true, false, null);
   RefactoringStatus expected =
       RefactoringUtil.addOverloadingWarning(new ExtractMethodOverloadingMsgCreator());
   RefactoringStatus actual =
       RefactoringUtil.checkOverloading(
           inheritedMethods,
           _refactoring.getMethodName(),
           fetchNewParameterTypes(),
           new ExtractMethodOverloadingMsgCreator());
   assertEquals(expected.getSeverity(), actual.getSeverity());
   assertNotNull(
       expected.getEntryMatchingCode(Corext.getPluginId(), OTRefactoringStatusCodes.OVERLOADING));
   assertNotNull(
       actual.getEntryMatchingCode(Corext.getPluginId(), OTRefactoringStatusCodes.OVERLOADING));
 }
  public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
    pm.beginTask("", 20); // $NON-NLS-1$
    fChangeManager = new TextChangeManager();
    RefactoringStatus result = new RefactoringStatus();
    fSourceProvider.initialize();
    fTargetProvider.initialize();

    pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_searching);
    RefactoringStatus searchStatus = new RefactoringStatus();
    String binaryRefsDescription =
        Messages.format(
            RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description,
            BasicElementLabels.getJavaElementName(fSourceProvider.getMethodName()));
    ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
    ICompilationUnit[] units =
        fTargetProvider.getAffectedCompilationUnits(
            searchStatus, binaryRefs, new SubProgressMonitor(pm, 1));
    binaryRefs.addErrorIfNecessary(searchStatus);
    if (searchStatus.hasFatalError()) {
      result.merge(searchStatus);
      return result;
    }

    IFile[] filesToBeModified = getFilesToBeModified(units);
    result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
    if (result.hasFatalError()) return result;
    result.merge(
        ResourceChangeChecker.checkFilesToBeChanged(
            filesToBeModified, new SubProgressMonitor(pm, 1)));
    checkOverridden(result, new SubProgressMonitor(pm, 4));
    IProgressMonitor sub = new SubProgressMonitor(pm, 15);
    sub.beginTask("", units.length * 3); // $NON-NLS-1$
    for (int c = 0; c < units.length; c++) {
      ICompilationUnit unit = units[c];
      sub.subTask(
          Messages.format(
              RefactoringCoreMessages.InlineMethodRefactoring_processing,
              BasicElementLabels.getFileName(unit)));
      CallInliner inliner = null;
      try {
        boolean added = false;
        MultiTextEdit root = new MultiTextEdit();
        CompilationUnitChange change = (CompilationUnitChange) fChangeManager.get(unit);
        change.setEdit(root);
        BodyDeclaration[] bodies =
            fTargetProvider.getAffectedBodyDeclarations(unit, new SubProgressMonitor(pm, 1));
        if (bodies.length == 0) continue;
        inliner = new CallInliner(unit, (CompilationUnit) bodies[0].getRoot(), fSourceProvider);
        for (int b = 0; b < bodies.length; b++) {
          BodyDeclaration body = bodies[b];
          inliner.initialize(body);
          RefactoringStatus nestedInvocations = new RefactoringStatus();
          ASTNode[] invocations =
              removeNestedCalls(
                  nestedInvocations,
                  unit,
                  fTargetProvider.getInvocations(body, new SubProgressMonitor(sub, 2)));
          for (int i = 0; i < invocations.length; i++) {
            ASTNode invocation = invocations[i];
            result.merge(inliner.initialize(invocation, fTargetProvider.getStatusSeverity()));
            if (result.hasFatalError()) break;
            if (result.getSeverity() < fTargetProvider.getStatusSeverity()) {
              added = true;
              TextEditGroup group =
                  new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_inline);
              change.addTextEditGroup(group);
              result.merge(inliner.perform(group));
            } else {
              fDeleteSource = false;
            }
          }
          // do this after we have inlined the method calls. We still want
          // to generate the modifications.
          if (!nestedInvocations.isOK()) {
            result.merge(nestedInvocations);
            fDeleteSource = false;
          }
        }
        if (!added) {
          fChangeManager.remove(unit);
        } else {
          root.addChild(inliner.getModifications());
          ImportRewrite rewrite = inliner.getImportEdit();
          if (rewrite.hasRecordedChanges()) {
            TextEdit edit = rewrite.rewriteImports(null);
            if (edit instanceof MultiTextEdit
                ? ((MultiTextEdit) edit).getChildrenSize() > 0
                : true) {
              root.addChild(edit);
              change.addTextEditGroup(
                  new TextEditGroup(
                      RefactoringCoreMessages.InlineMethodRefactoring_edit_import,
                      new TextEdit[] {edit}));
            }
          }
        }
      } finally {
        if (inliner != null) inliner.dispose();
      }
      sub.worked(1);
      if (sub.isCanceled()) throw new OperationCanceledException();
    }
    result.merge(searchStatus);
    sub.done();
    pm.done();
    return result;
  }