コード例 #1
0
  /** @see Openable#openBuffer(IProgressMonitor, Object) */
  protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException {

    // create buffer
    IBuffer buffer = this.owner.createBuffer(this);
    if (buffer == null) return null;

    // set the buffer source
    if (buffer.getCharacters() == null) {
      IBuffer classFileBuffer = this.classFile.getBuffer();
      if (classFileBuffer != null) {
        buffer.setContents(classFileBuffer.getCharacters());
      } else {
        // Disassemble
        IClassFileReader reader =
            ToolFactory.createDefaultClassFileReader(this.classFile, IClassFileReader.ALL);
        Disassembler disassembler = new Disassembler();
        String contents =
            disassembler.disassemble(
                reader,
                Util.getLineSeparator("", getJavaProject()),
                ClassFileBytesDisassembler.WORKING_COPY); // $NON-NLS-1$
        buffer.setContents(contents);
      }
    }

    // add buffer to buffer cache
    BufferManager bufManager = getBufferManager();
    bufManager.addBuffer(buffer);

    // listen to buffer changes
    buffer.addBufferChangedListener(this);

    return buffer;
  }
コード例 #2
0
  /**
   * Determines is the java element contains a type with a specific annotation.
   *
   * <p>The syntax for the property tester is of the form: qualified or unqualified annotation name
   * <li>qualified or unqualified annotation name, required. For example, <code>org.junit.JUnit
   *     </code>.
   * </ol>
   *
   * @param element the element to check for the method
   * @param annotationName the qualified or unqualified name of the annotation to look for
   * @return true if the type is found in the element, false otherwise
   */
  private boolean hasTypeWithAnnotation(IJavaElement element, String annotationType) {
    try {
      IType type = getType(element);
      if (type == null || !type.exists()) {
        return false;
      }

      IBuffer buffer = null;
      IOpenable openable = type.getOpenable();
      if (openable instanceof ICompilationUnit) {
        buffer = ((ICompilationUnit) openable).getBuffer();
      } else if (openable instanceof IClassFile) {
        buffer = ((IClassFile) openable).getBuffer();
      }
      if (buffer == null) {
        return false;
      }

      ISourceRange sourceRange = type.getSourceRange();
      ISourceRange nameRange = type.getNameRange();
      if (sourceRange != null && nameRange != null) {
        IScanner scanner = ToolFactory.createScanner(false, false, true, false);
        scanner.setSource(buffer.getCharacters());
        scanner.resetTo(sourceRange.getOffset(), nameRange.getOffset());
        if (findAnnotation(scanner, annotationType)) {
          return true;
        }
      }
    } catch (JavaModelException e) {
    } catch (InvalidInputException e) {
    }
    return false;
  }
コード例 #3
0
ファイル: ClassFile.java プロジェクト: felixdo/groovy-eclipse
  /** Loads the buffer via SourceMapper, and maps it in SourceMapper */
  private IBuffer mapSource(SourceMapper mapper, IBinaryType info, IClassFile bufferOwner) {
    char[] contents = mapper.findSource(getType(), info);
    if (contents != null) {
      // create buffer
      IBuffer buffer = BufferManager.createBuffer(bufferOwner);
      if (buffer == null) return null;
      BufferManager bufManager = getBufferManager();
      bufManager.addBuffer(buffer);

      // set the buffer source
      if (buffer.getCharacters() == null) {
        buffer.setContents(contents);
      }

      // listen to buffer changes
      buffer.addBufferChangedListener(this);

      // do the source mapping
      mapper.mapSource(getOuterMostEnclosingType(), contents, info);

      return buffer;
    } else {
      // create buffer
      IBuffer buffer = BufferManager.createNullBuffer(bufferOwner);
      if (buffer == null) return null;
      BufferManager bufManager = getBufferManager();
      bufManager.addBuffer(buffer);

      // listen to buffer changes
      buffer.addBufferChangedListener(this);
      return buffer;
    }
  }
コード例 #4
0
ファイル: ClassFile.java プロジェクト: felixdo/groovy-eclipse
  /** @see ICodeAssist#codeSelect(int, int, WorkingCopyOwner) */
  public IJavaElement[] codeSelect(int offset, int length, WorkingCopyOwner owner)
      throws JavaModelException {
    IBuffer buffer = getBuffer();
    char[] contents;
    if (buffer != null && (contents = buffer.getCharacters()) != null) {
      BinaryType type = (BinaryType) getType();
      // GROOVY start
      /*old{
      BasicCompilationUnit cu = new BasicCompilationUnit(contents, null, type.sourceFileName((IBinaryType) type.getElementInfo()));
      }new*/

      // handle code select for Groovy files differently
      IBinaryType typeInfo = (IBinaryType) type.getElementInfo();
      if (LanguageSupportFactory.isInterestingBinary(type, typeInfo)) {
        return LanguageSupportFactory.binaryCodeSelect(this, offset, length, owner);
      }
      BasicCompilationUnit cu =
          new BasicCompilationUnit(contents, null, type.sourceFileName(typeInfo));
      // GROOVY end
      return super.codeSelect(cu, offset, length, owner);
    } else {
      // has no associated souce
      return new IJavaElement[] {};
    }
  }
コード例 #5
0
 public char[] getContents() {
   try {
     IBuffer buffer = getBuffer();
     if (buffer == null) return CharOperation.NO_CHAR;
     char[] characters = buffer.getCharacters();
     if (characters == null) return CharOperation.NO_CHAR;
     return characters;
   } catch (JavaModelException e) {
     return CharOperation.NO_CHAR;
   }
 }
コード例 #6
0
  /**
   * Determines is the java element contains a method with a specific annotation.
   *
   * <p>The syntax for the property tester is of the form: qualified or unqualified annotation name,
   * modifiers
   * <li>qualified or unqualified annotation name, required. For example, <code>org.junit.JUnit
   *     </code>.
   * <li>modifiers - optional space separated list of modifiers, for example, <code>public static
   *     </code>.
   * </ol>
   *
   * @param element the element to check for the method
   * @param annotationName the qualified or unqualified name of the annotation to look for
   * @return true if the method is found in the element, false otherwise
   */
  private boolean hasMethodWithAnnotation(IJavaElement element, Object[] args) {
    try {
      String annotationType = (String) args[0];
      int flags = 0;
      if (args.length > 1) {
        String[] modifiers = ((String) args[1]).split(" "); // $NON-NLS-1$
        for (int j = 0; j < modifiers.length; j++) {
          String modifier = modifiers[j];
          Integer flag = (Integer) fgModifiers.get(modifier);
          if (flag != null) {
            flags = flags | flag.intValue();
          }
        }
      } else {
        flags = -1;
      }

      IType type = getType(element);
      if (type == null || !type.exists()) {
        return false;
      }
      IMethod[] methods = type.getMethods();
      if (methods.length == 0) {
        return false;
      }

      IBuffer buffer = null;
      IOpenable openable = type.getOpenable();
      if (openable instanceof ICompilationUnit) {
        buffer = ((ICompilationUnit) openable).getBuffer();
      } else if (openable instanceof IClassFile) {
        buffer = ((IClassFile) openable).getBuffer();
      }
      if (buffer == null) {
        return false;
      }
      IScanner scanner = null; // delay initialization

      for (int i = 0; i < methods.length; i++) {
        IMethod curr = methods[i];
        if (curr.isConstructor() || (flags != -1 && flags != (curr.getFlags() & FLAGS_MASK))) {
          continue;
        }

        ISourceRange sourceRange = curr.getSourceRange();
        ISourceRange nameRange = curr.getNameRange();
        if (sourceRange != null && nameRange != null) {
          if (scanner == null) {
            scanner = ToolFactory.createScanner(false, false, true, false);
            scanner.setSource(buffer.getCharacters());
          }
          scanner.resetTo(sourceRange.getOffset(), nameRange.getOffset());
          if (findAnnotation(scanner, annotationType)) {
            return true;
          }
        }
      }
    } catch (JavaModelException e) {
    } catch (InvalidInputException e) {
    }
    return false;
  }
コード例 #7
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();
    }
  }