/**
   * Ensures that creating a DOM AST and computing the bindings takes the owner's working copies
   * into account. (regression test for bug 39533 Working copy with no corresponding file not
   * considered by NameLookup)
   *
   * @deprecated using deprecated code
   */
  public void testParseCompilationUnit1() throws CoreException {
    ICompilationUnit workingCopy1 = null;
    ICompilationUnit workingCopy2 = null;
    try {
      TestWorkingCopyOwner owner = new TestWorkingCopyOwner();
      workingCopy1 = getCompilationUnit("P/X.java").getWorkingCopy(owner, null);
      workingCopy1.getBuffer().setContents("public class X implements I {\n" + "}");
      workingCopy1.makeConsistent(null);

      workingCopy2 = getCompilationUnit("P/I.java").getWorkingCopy(owner, null);
      workingCopy2.getBuffer().setContents("public interface I {\n" + "}");
      workingCopy2.makeConsistent(null);

      ASTParser parser = ASTParser.newParser(AST.JLS2);
      parser.setSource(workingCopy1);
      parser.setResolveBindings(true);
      parser.setWorkingCopyOwner(owner);
      CompilationUnit cu = (CompilationUnit) parser.createAST(null);
      List types = cu.types();
      assertEquals("Unexpected number of types in AST", 1, types.size());
      TypeDeclaration type = (TypeDeclaration) types.get(0);
      ITypeBinding typeBinding = type.resolveBinding();
      assertTypeBindingsEqual("Unexpected interfaces", "I", typeBinding.getInterfaces());
    } finally {
      if (workingCopy1 != null) {
        workingCopy1.discardWorkingCopy();
      }
      if (workingCopy2 != null) {
        workingCopy2.discardWorkingCopy();
      }
    }
  }
Example #2
0
 /**
  * Gets the IDocument for the supplied src file.
  *
  * <p>Code borrowed from org.eclipse.jdt.internal.core.JavaModelOperation.
  *
  * @param src The src file.
  * @return The IDocument.
  */
 public static IDocument getDocument(ICompilationUnit src) throws Exception {
   IBuffer buffer = src.getBuffer();
   if (buffer instanceof IDocument) {
     return (IDocument) buffer;
   }
   return new DocumentAdapter(buffer);
 }
  private void deleteAccessor(NLSSubstitution substitution, TextChange change, ICompilationUnit cu)
      throws CoreException {
    AccessorClassReference accessorClassRef = substitution.getAccessorClassReference();
    if (accessorClassRef != null) {
      Region region = accessorClassRef.getRegion();
      String[] args = {
        substitution.getValueNonEmpty(),
        BasicElementLabels.getJavaElementName(substitution.getKey())
      };
      String label = Messages.format(NLSMessages.NLSSourceModifier_remove_accessor, args);
      String replaceString = '\"' + unwindEscapeChars(substitution.getValueNonEmpty()) + '\"';
      TextChangeCompatibility.addTextEdit(
          change, label, new ReplaceEdit(region.getOffset(), region.getLength(), replaceString));
      if (fIsEclipseNLS && substitution.getState() != NLSSubstitution.INTERNALIZED) {

        Region position = substitution.getNLSElement().getPosition();
        int lineStart = getLineStart(cu.getBuffer(), position.getOffset());
        int lineEnd = getLineEnd(cu.getBuffer(), position.getOffset());
        String cuLine = cu.getBuffer().getText(lineStart, lineEnd - lineStart);
        StringBuffer buf = new StringBuffer(cuLine);
        buf.replace(
            region.getOffset() - lineStart,
            region.getOffset() + region.getLength() - lineStart,
            replaceString);
        try {
          NLSLine[] allLines = NLSScanner.scan(buf.toString());

          NLSLine nlsLine = allLines[0];
          NLSElement element =
              findElement(
                  nlsLine,
                  position.getOffset() - lineStart - accessorClassRef.getName().length() - 1);
          if (element == null || element.hasTag()) return;

          NLSElement[] elements = nlsLine.getElements();
          int indexInElementList = Arrays.asList(elements).indexOf(element);
          String editText =
              ' ' + NLSElement.createTagText(indexInElementList + 1); // tags are 1-based
          TextChangeCompatibility.addTextEdit(change, label, new InsertEdit(lineEnd, editText));

        } catch (InvalidInputException e) {
        } catch (BadLocationException e) {
        }
      }
    }
  }
 static ICompilationUnit createNewWorkingCopy(
     ICompilationUnit cu, TextChangeManager manager, WorkingCopyOwner owner, SubProgressMonitor pm)
     throws CoreException {
   ICompilationUnit newWc = cu.getWorkingCopy(owner, null);
   String previewContent = manager.get(cu).getPreviewContent(new NullProgressMonitor());
   newWc.getBuffer().setContents(previewContent);
   newWc.reconcile(ICompilationUnit.NO_AST, false, owner, pm);
   return newWc;
 }
  /* non Java-doc
   * @see IRefactoring#createChange(IProgressMonitor)
   */
  @Override
  public Change createChange(IProgressMonitor pm) throws CoreException {
    final String NN = ""; // $NON-NLS-1$
    if (pm == null) pm = new NullProgressMonitor();
    pm.beginTask(NN, 2);
    try {
      final CompilationUnitChange result = new CompilationUnitChange(getName(), fCUnit);
      if (fLeaveDirty) result.setSaveMode(TextFileChange.LEAVE_DIRTY);
      MultiTextEdit root = new MultiTextEdit();
      result.setEdit(root);
      fRewriter = ASTRewrite.create(fAnalyzer.getEnclosingBodyDeclaration().getAST());
      fRewriter.setTargetSourceRangeComputer(
          new SelectionAwareSourceRangeComputer(
              fAnalyzer.getSelectedNodes(),
              fCUnit.getBuffer(),
              fSelection.getOffset(),
              fSelection.getLength()));
      fImportRewrite = StubUtility.createImportRewrite(fRootNode, true);

      fLinkedProposalModel = new LinkedProposalModel();

      fScope =
          CodeScopeBuilder.perform(fAnalyzer.getEnclosingBodyDeclaration(), fSelection)
              .findScope(fSelection.getOffset(), fSelection.getLength());
      fScope.setCursor(fSelection.getOffset());

      fSelectedNodes = fAnalyzer.getSelectedNodes();

      createTryCatchStatement(fCUnit.getBuffer(), fCUnit.findRecommendedLineSeparator());

      if (fImportRewrite.hasRecordedChanges()) {
        TextEdit edit = fImportRewrite.rewriteImports(null);
        root.addChild(edit);
        result.addTextEditGroup(new TextEditGroup(NN, new TextEdit[] {edit}));
      }
      TextEdit change = fRewriter.rewriteAST();
      root.addChild(change);
      result.addTextEditGroup(new TextEditGroup(NN, new TextEdit[] {change}));
      return result;
    } finally {
      pm.done();
    }
  }
 private static int moveBack(int offset, int start, String ignoreCharacters, ICompilationUnit cu) {
   try {
     IBuffer buf = cu.getBuffer();
     while (offset >= start) {
       if (ignoreCharacters.indexOf(buf.getChar(offset - 1)) == -1) {
         return offset;
       }
       offset--;
     }
   } catch (JavaModelException e) {
   }
   return start;
 }
  private void setContentsToCU(ICompilationUnit unit, String value) {
    if (unit == null) {
      return;
    }

    synchronized (unit) {
      IBuffer buffer;
      try {
        buffer = unit.getBuffer();
        if (buffer != null) {
          buffer.setContents(value);
        }
      } catch (JavaModelException e) {
        e.printStackTrace();
      }
    }
  }
Example #8
0
  /** 取得Quick Fix後改變的程式碼 */
  private Change getChange(CompilationUnit actRoot, ASTRewrite rewrite) {
    try {
      ICompilationUnit cu = (ICompilationUnit) actOpenable;
      Document document = new Document(cu.getBuffer().getContents());

      TextEdit edits = null;
      if (rewrite != null) edits = rewrite.rewriteAST(document, null);
      else edits = actRoot.rewrite(document, cu.getJavaProject().getOptions(true));

      TextFileChange textFileChange =
          new TextFileChange(cu.getElementName(), (IFile) cu.getResource());
      textFileChange.setEdit(edits);

      return textFileChange;
    } catch (JavaModelException e) {
      logger.error("[Apply Change Rethrow Unchecked Exception] EXCEPTION ", e);
    }
    return null;
  }
  /*
   * Ensures that searching takes the primary owner's working copies and the given working copies into account.
   * (regression test for bug 43300 SearchEngine(IWorkingCopy[] workingCopies) not backward compatible)
   */
  public void testSearch4() throws CoreException {
    ICompilationUnit primaryWorkingCopy = null;
    try {
      createFolder("P/p");
      createFile("/P/p/Y.java", "");
      primaryWorkingCopy = getCompilationUnit("P/p/Y.java");
      primaryWorkingCopy.becomeWorkingCopy(null);

      // create type Y in working copy
      primaryWorkingCopy.getBuffer().setContents("package p;\n" + "public class Y {\n" + "}");
      primaryWorkingCopy.makeConsistent(null);

      // create new working copy on X.java and add type X
      this.workingCopy = getCompilationUnit("P/p/X.java").getWorkingCopy(null);
      this.workingCopy.getBuffer().setContents("package p;\n" + "public class X {\n" + "}");
      this.workingCopy.makeConsistent(null);

      JavaSearchTests.JavaSearchResultCollector resultCollector =
          new JavaSearchTests.JavaSearchResultCollector();
      IJavaSearchScope scope =
          SearchEngine.createJavaSearchScope(new IJavaElement[] {primaryWorkingCopy.getParent()});
      SearchPattern pattern =
          SearchPattern.createPattern(
              "*",
              IJavaSearchConstants.TYPE,
              IJavaSearchConstants.DECLARATIONS,
              SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);
      new SearchEngine(new ICompilationUnit[] {this.workingCopy})
          .search(
              pattern,
              new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
              scope,
              resultCollector,
              null);
      assertEquals("p/X.java p.X [X]\n" + "p/Y.java p.Y [Y]", resultCollector.toString());

    } finally {
      if (primaryWorkingCopy != null) {
        primaryWorkingCopy.discardWorkingCopy();
      }
      deleteFile("/P/p/Y.java");
    }
  }
  private void helper1(
      String[] methodNames,
      String[][] signatures,
      boolean deleteAllInSourceType,
      boolean deleteAllMatchingMethods,
      boolean replaceOccurences)
      throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType type = getType(cu, "B");
    IMethod[] methods = getMethods(type, methodNames, signatures);

    ExtractSupertypeProcessor processor = createRefactoringProcessor(methods);
    Refactoring refactoring = processor.getRefactoring();
    processor.setMembersToMove(methods);

    assertTrue("activation", refactoring.checkInitialConditions(new NullProgressMonitor()).isOK());

    processor.setTypesToExtract(new IType[] {type});
    processor.setTypeName("Z");
    processor.setCreateMethodStubs(true);
    processor.setInstanceOf(false);
    processor.setReplace(replaceOccurences);
    if (deleteAllInSourceType) processor.setDeletedMethods(methods);
    if (deleteAllMatchingMethods)
      processor.setDeletedMethods(
          getMethods(processor.getMatchingElements(new NullProgressMonitor(), false)));

    RefactoringStatus status = refactoring.checkFinalConditions(new NullProgressMonitor());
    assertTrue("precondition was supposed to pass", !status.hasError());
    performChange(refactoring, false);

    String expected = getFileContents(getOutputTestFileName("A"));
    String actual = cu.getSource();
    assertEqualLines(expected, actual);

    expected = getFileContents(getOutputTestFileName("Z"));
    ICompilationUnit unit = getPackageP().getCompilationUnit("Z.java");
    if (!unit.exists()) assertTrue("extracted compilation unit does not exist", false);
    actual = unit.getBuffer().getContents();
    assertEqualLines(expected, actual);
  }
  public void testASTRewriteExample() throws Exception {
    // create a new project
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("Test");
    project.create(null);
    project.open(null);
    try {
      // set the Java nature and Java build path
      IProjectDescription description = project.getDescription();
      description.setNatureIds(new String[] {JavaCore.NATURE_ID});
      project.setDescription(description, null);

      IJavaProject javaProject = JavaCore.create(project);

      // build path is: project as source folder and JRE container
      IClasspathEntry[] cpentry =
          new IClasspathEntry[] {
            JavaCore.newSourceEntry(javaProject.getPath()),
            JavaRuntime.getDefaultJREContainerEntry()
          };
      javaProject.setRawClasspath(cpentry, javaProject.getPath(), null);
      Map<String, String> options = new HashMap<>();
      options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE);
      options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4");
      javaProject.setOptions(options);

      // create a test file
      IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(project);
      IPackageFragment pack1 = root.createPackageFragment("test1", false, null);
      StringBuffer buf = new StringBuffer();
      buf.append("package test1;\n");
      buf.append("public class E {\n");
      buf.append("    public void foo(int i) {\n");
      buf.append("        while (--i > 0) {\n");
      buf.append("            System.beep();\n");
      buf.append("        }\n");
      buf.append("    }\n");
      buf.append("}\n");
      ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

      // create an AST
      ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
      parser.setSource(cu);
      parser.setResolveBindings(false);
      CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
      AST ast = astRoot.getAST();

      // create the descriptive ast rewriter
      ASTRewrite rewrite = ASTRewrite.create(ast);

      // get the block node that contains the statements in the method body
      TypeDeclaration typeDecl = (TypeDeclaration) astRoot.types().get(0);
      MethodDeclaration methodDecl = typeDecl.getMethods()[0];
      Block block = methodDecl.getBody();

      // create new statements to insert
      MethodInvocation newInv1 = ast.newMethodInvocation();
      newInv1.setName(ast.newSimpleName("bar1"));
      Statement newStatement1 = ast.newExpressionStatement(newInv1);

      MethodInvocation newInv2 = ast.newMethodInvocation();
      newInv2.setName(ast.newSimpleName("bar2"));
      Statement newStatement2 = ast.newExpressionStatement(newInv2);

      // describe that the first node is inserted as first statement in block, the other one as last
      // statement
      // note: AST is not modified by this
      ListRewrite listRewrite = rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
      listRewrite.insertFirst(newStatement1, null);
      listRewrite.insertLast(newStatement2, null);

      // evaluate the text edits corresponding to the described changes. AST and CU still
      // unmodified.
      TextEdit res = rewrite.rewriteAST();

      // apply the text edits to the compilation unit
      Document document = new Document(cu.getSource());
      res.apply(document);
      cu.getBuffer().setContents(document.get());

      // test result
      String preview = cu.getSource();

      buf = new StringBuffer();
      buf.append("package test1;\n");
      buf.append("public class E {\n");
      buf.append("    public void foo(int i) {\n");
      buf.append("        bar1();\n");
      buf.append("        while (--i > 0) {\n");
      buf.append("            System.beep();\n");
      buf.append("        }\n");
      buf.append("        bar2();\n");
      buf.append("    }\n");
      buf.append("}\n");
      assertEquals(preview, buf.toString());
    } finally {
      project.delete(true, null);
    }
  }
  /**
   * Copies/moves a compilation unit with the name <code>newCUName</code> to the destination
   * package.<br>
   * The package statement in the compilation unit is updated if necessary. The main type of the
   * compilation unit is renamed if necessary.
   *
   * @exception JavaModelException if the operation is unable to complete
   */
  private void processCompilationUnitResource(ICompilationUnit source, PackageFragment dest)
      throws JavaModelException {
    String newCUName = getNewNameFor(source);
    String destName = (newCUName != null) ? newCUName : source.getElementName();
    TextEdit edit = updateContent(source, dest, newCUName); // null if unchanged

    // TODO (frederic) remove when bug 67606 will be fixed (bug 67823)
    // store encoding (fix bug 66898)
    IFile sourceResource = (IFile) source.getResource();
    String sourceEncoding = null;
    try {
      sourceEncoding = sourceResource.getCharset(false);
    } catch (CoreException ce) {
      // no problem, use default encoding
    }
    // end todo
    // copy resource
    IContainer destFolder = (IContainer) dest.getResource(); // can be an IFolder or an IProject
    IFile destFile = destFolder.getFile(new Path(destName));
    org.eclipse.jdt.internal.core.CompilationUnit destCU =
        new org.eclipse.jdt.internal.core.CompilationUnit(
            dest, destName, DefaultWorkingCopyOwner.PRIMARY);
    if (!destFile.equals(sourceResource)) {
      try {
        if (!destCU.isWorkingCopy()) {
          if (destFile.exists()) {
            if (this.force) {
              // we can remove it
              deleteResource(destFile, IResource.KEEP_HISTORY);
              destCU.close(); // ensure the in-memory buffer for the dest CU is closed
            } else {
              // abort
              throw new JavaModelException(
                  new JavaModelStatus(
                      IJavaModelStatusConstants.NAME_COLLISION,
                      Messages.bind(
                          Messages.status_nameCollision, destFile.getFullPath().toString())));
            }
          }
          int flags = this.force ? IResource.FORCE : IResource.NONE;
          if (isMove()) {
            flags |= IResource.KEEP_HISTORY;
            sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1));
          } else {
            if (edit != null) flags |= IResource.KEEP_HISTORY;
            sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1));
          }
          setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
        } else {
          destCU.getBuffer().setContents(source.getBuffer().getContents());
        }
      } catch (JavaModelException e) {
        throw e;
      } catch (CoreException e) {
        throw new JavaModelException(e);
      }

      // update new resource content
      if (edit != null) {
        boolean wasReadOnly = destFile.isReadOnly();
        try {
          saveContent(dest, destName, edit, sourceEncoding, destFile);
        } catch (CoreException e) {
          if (e instanceof JavaModelException) throw (JavaModelException) e;
          throw new JavaModelException(e);
        } finally {
          Util.setReadOnly(destFile, wasReadOnly);
        }
      }

      // register the correct change deltas
      prepareDeltas(source, destCU, isMove());
      if (newCUName != null) {
        // the main type has been renamed
        String oldName = Util.getNameWithoutJavaLikeExtension(source.getElementName());
        String newName = Util.getNameWithoutJavaLikeExtension(newCUName);
        prepareDeltas(source.getType(oldName), destCU.getType(newName), isMove());
      }
    } else {
      if (!this.force) {
        throw new JavaModelException(
            new JavaModelStatus(
                IJavaModelStatusConstants.NAME_COLLISION,
                Messages.bind(Messages.status_nameCollision, destFile.getFullPath().toString())));
      }
      // update new resource content
      // in case we do a saveas on the same resource we have to simply update the contents
      // see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351
      if (edit != null) {
        saveContent(dest, destName, edit, sourceEncoding, destFile);
      }
    }
  }
  public void run(IMarker marker) {
    IFile file = (IFile) javaDeclaration.getResource();
    try {
      ICompilationUnit original = EclipseUtil.getCompilationUnit(file);
      if (original == null) {
        return;
      }
      ICompilationUnit compilationUnit = original.getWorkingCopy(new NullProgressMonitor());

      String lineDelim = JavaPropertyGenerator.getLineDelimiterUsed(compilationUnit);

      Hashtable<String, String> options = JavaCore.getOptions();

      int tabSize = new Integer(options.get("org.eclipse.jdt.core.formatter.tabulation.size"));

      StringBuffer tabBuf = new StringBuffer();

      for (int i = 0; i < tabSize; i++) tabBuf.append(" ");

      String tab = tabBuf.toString();

      IType type = compilationUnit.findPrimaryType();

      IField field = type.getField(property.getName());

      String propertyType = "";
      if (field != null && field.exists()) {
        propertyType = field.getTypeSignature();
      } else {
        propertyType = "String"; // $NON-NLS-1$

        StringBuffer buf = new StringBuffer();

        buf.append(
            tab
                + "private "
                + propertyType
                + " "
                + property.getName()
                + ";"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        buf.append(lineDelim);

        field = type.createField(buf.toString(), null, false, new NullProgressMonitor());
        if (field != null) {
          IBuffer buffer = compilationUnit.getBuffer();

          buffer.replace(
              field.getSourceRange().getOffset(),
              field.getSourceRange().getLength(),
              buf.toString());
          synchronized (compilationUnit) {
            compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
          }
        }
      }

      IMethod oldMethod = GetterSetterUtil.getSetter(field);
      if (oldMethod == null || !oldMethod.exists()) {
        String setterName = GetterSetterUtil.getSetterName(field, null);
        // JavaPropertyGenerator.createSetter(compilationUnit, type, "public",
        // field.getTypeSignature(), setterName, lineDelim);

        String stub = GetterSetterUtil.getSetterStub(field, setterName, true, Flags.AccPublic);
        IMethod newMethod = type.createMethod(stub, null, false, new NullProgressMonitor());
        if (newMethod != null) {
          IBuffer buffer = compilationUnit.getBuffer();
          // format
          StringBuffer buf = new StringBuffer();

          buf.append(lineDelim);
          buf.append(
              tab
                  + "public void "
                  + setterName
                  + "("
                  + propertyType
                  + " "
                  + property.getName()
                  + "){"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
          buf.append(lineDelim);
          buf.append(tab + tab + "this." + property.getName() + " = " + property.getName() + ";");
          buf.append(lineDelim);
          buf.append(tab + "}");
          buf.append(lineDelim);

          buffer.replace(
              newMethod.getSourceRange().getOffset(),
              newMethod.getSourceRange().getLength(),
              buf.toString());
        }
      }

      compilationUnit.commitWorkingCopy(false, new NullProgressMonitor());
      compilationUnit.discardWorkingCopy();

    } catch (CoreException ex) {
      SeamGuiPlugin.getPluginLog().logError(ex);
    }
  }
  /** @exception JavaModelException if setting the source of the original compilation unit fails */
  protected void executeOperation() throws JavaModelException {
    try {
      beginTask(Messages.workingCopy_commit, 2);
      CompilationUnit workingCopy = getCompilationUnit();

      if (ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(
          workingCopy.getJavaProject().getElementName())) {
        // case of a working copy without a resource
        workingCopy.getBuffer().save(this.progressMonitor, this.force);
        return;
      }

      ICompilationUnit primary = workingCopy.getPrimary();
      boolean isPrimary = workingCopy.isPrimary();

      JavaElementDeltaBuilder deltaBuilder = null;
      PackageFragmentRoot root =
          (PackageFragmentRoot) workingCopy.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
      boolean isIncluded = !Util.isExcluded(workingCopy);
      IFile resource = (IFile) workingCopy.getResource();
      IJavaProject project = root.getJavaProject();
      if (isPrimary
          || (root.validateOnClasspath().isOK()
              && isIncluded
              && resource.isAccessible()
              && Util.isValidCompilationUnitName(
                  workingCopy.getElementName(),
                  project.getOption(JavaCore.COMPILER_SOURCE, true),
                  project.getOption(JavaCore.COMPILER_COMPLIANCE, true)))) {

        // force opening so that the delta builder can get the old info
        if (!isPrimary && !primary.isOpen()) {
          primary.open(null);
        }

        // creates the delta builder (this remembers the content of the cu) if:
        // - it is not excluded
        // - and it is not a primary or it is a non-consistent primary
        if (isIncluded && (!isPrimary || !workingCopy.isConsistent())) {
          deltaBuilder = new JavaElementDeltaBuilder(primary);
        }

        // save the cu
        IBuffer primaryBuffer = primary.getBuffer();
        if (!isPrimary) {
          if (primaryBuffer == null) return;
          char[] primaryContents = primaryBuffer.getCharacters();
          boolean hasSaved = false;
          try {
            IBuffer workingCopyBuffer = workingCopy.getBuffer();
            if (workingCopyBuffer == null) return;
            primaryBuffer.setContents(workingCopyBuffer.getCharacters());
            primaryBuffer.save(this.progressMonitor, this.force);
            primary.makeConsistent(this);
            hasSaved = true;
          } finally {
            if (!hasSaved) {
              // restore original buffer contents since something went wrong
              primaryBuffer.setContents(primaryContents);
            }
          }
        } else {
          // for a primary working copy no need to set the content of the buffer again
          primaryBuffer.save(this.progressMonitor, this.force);
          primary.makeConsistent(this);
        }
      } else {
        // working copy on cu outside classpath OR resource doesn't exist yet
        String encoding = null;
        try {
          encoding = resource.getCharset();
        } catch (CoreException ce) {
          // use no encoding
        }
        String contents = workingCopy.getSource();
        if (contents == null) return;
        try {
          byte[] bytes = encoding == null ? contents.getBytes() : contents.getBytes(encoding);
          ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
          if (resource.exists()) {
            resource.setContents(
                stream,
                this.force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
                null);
          } else {
            resource.create(stream, this.force, this.progressMonitor);
          }
        } catch (CoreException e) {
          throw new JavaModelException(e);
        } catch (UnsupportedEncodingException e) {
          throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
        }
      }

      setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);

      // make sure working copy is in sync
      workingCopy.updateTimeStamp((CompilationUnit) primary);
      workingCopy.makeConsistent(this);
      worked(1);

      // build the deltas
      if (deltaBuilder != null) {
        deltaBuilder.buildDeltas();

        // add the deltas to the list of deltas created during this operation
        if (deltaBuilder.delta != null) {
          addDelta(deltaBuilder.delta);
        }
      }
      worked(1);
    } finally {
      done();
    }
  }