コード例 #1
0
  /** @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();
    }
  }
 /*
  * @see JavaElement#getPrimaryElement(boolean)
  */
 public IJavaScriptElement getPrimaryElement(boolean checkOwner) {
   CompilationUnit cu = (CompilationUnit) this.parent;
   if (checkOwner && cu.isPrimary()) return this;
   return cu.getImportContainer();
 }