private InlineMethodRefactoring(
     ICompilationUnit unit, ConstructorInvocation node, int offset, int length) {
   this(unit, (ASTNode) node, offset, length);
   fTargetProvider = TargetProvider.create(unit, node);
   fInitialMode = fCurrentMode = Mode.INLINE_SINGLE;
   fDeleteSource = false;
 }
 private InlineMethodRefactoring(
     ITypeRoot typeRoot, MethodDeclaration node, int offset, int length) {
   this(typeRoot, (ASTNode) node, offset, length);
   fSourceProvider = new SourceProvider(typeRoot, node);
   fTargetProvider = TargetProvider.create(node);
   fInitialMode = fCurrentMode = Mode.INLINE_ALL;
   fDeleteSource = canEnableDeleteSource();
 }
 public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
   RefactoringStatus result = new RefactoringStatus();
   if (fSourceProvider == null && Invocations.isInvocation(fInitialNode)) {
     fSourceProvider = resolveSourceProvider(result, fInitialTypeRoot, fInitialNode);
     if (result.hasFatalError()) return result;
   }
   result.merge(fSourceProvider.checkActivation());
   result.merge(fTargetProvider.checkActivation());
   return result;
 }
 public RefactoringStatus setCurrentMode(Mode mode) throws JavaModelException {
   if (fCurrentMode == mode) return new RefactoringStatus();
   Assert.isTrue(getInitialMode() == Mode.INLINE_SINGLE);
   fCurrentMode = mode;
   if (mode == Mode.INLINE_SINGLE) {
     if (fInitialNode instanceof MethodInvocation)
       fTargetProvider =
           TargetProvider.create(
               (ICompilationUnit) fInitialTypeRoot, (MethodInvocation) fInitialNode);
     else if (fInitialNode instanceof SuperMethodInvocation)
       fTargetProvider =
           TargetProvider.create(
               (ICompilationUnit) fInitialTypeRoot, (SuperMethodInvocation) fInitialNode);
     else if (fInitialNode instanceof ConstructorInvocation)
       fTargetProvider =
           TargetProvider.create(
               (ICompilationUnit) fInitialTypeRoot, (ConstructorInvocation) fInitialNode);
     else throw new IllegalStateException(String.valueOf(fInitialNode));
   } else {
     fTargetProvider = TargetProvider.create(fSourceProvider.getDeclaration());
   }
   return fTargetProvider.checkActivation();
 }
  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;
  }