Example usage for org.eclipse.jdt.core IBuffer close

List of usage examples for org.eclipse.jdt.core IBuffer close

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IBuffer close.

Prototype

public void close();

Source Link

Document

Closes the buffer.

Usage

From source file:net.sf.commonclipse.CompareToGenerator.java

License:Apache License

/**
 * Adds "implements Comparable" to class declaration.
 * @param type IType/*from  w  w w .  ja  v  a  2 s .c o  m*/
 * @throws JavaModelException model exception
 */
private void addImplementsComparable(IType type) throws JavaModelException {

    // does class already implements comparable?
    IType[] interfaces = type.newSupertypeHierarchy(null).getAllInterfaces();
    for (int j = 0, size = interfaces.length; j < size; j++) {
        if (interfaces[j].getFullyQualifiedName().equals("java.lang.Comparable")) //$NON-NLS-1$
        {
            return;
        }
    }

    // find class declaration
    ISourceRange nameRange = type.getNameRange();

    // no declaration??
    if (nameRange == null) {
        return;
    }

    // offset for END of class name
    int offset = nameRange.getOffset() + nameRange.getLength();

    IBuffer buffer = type.getCompilationUnit().getBuffer();
    String contents = buffer.getText(offset, buffer.getLength() - offset);

    // warning, this doesn't handle "implements" and "{" contained in comments in the middle of the declaration!
    int indexOfPar = contents.indexOf("{"); //$NON-NLS-1$

    contents = contents.substring(0, indexOfPar);

    int indexOfImplements = contents.indexOf("implements"); //$NON-NLS-1$
    if (indexOfImplements > -1) {
        buffer.replace(offset + indexOfImplements + "implements".length()//$NON-NLS-1$
                , 0, " Comparable,"); //$NON-NLS-1$
    } else {
        buffer.replace(offset, 0, " implements Comparable"); //$NON-NLS-1$
    }

    buffer.save(null, false);
    buffer.close();

}

From source file:net.sf.guavaeclipse.creator.CompareMethodCreator.java

License:Apache License

private void addImplementsComparable(IType type) throws JavaModelException {

    // does class already implements comparable?
    IType[] interfaces = type.newSupertypeHierarchy(null).getAllInterfaces();
    for (int j = 0, size = interfaces.length; j < size; j++) {
        if (interfaces[j].getFullyQualifiedName().equals("java.lang.Comparable")) //$NON-NLS-1$
        {//from w  ww.j  a  va 2  s . com
            return;
        }
    }

    // find class declaration
    ISourceRange nameRange = type.getNameRange();

    // no declaration??
    if (nameRange == null) {
        return;
    }

    // offset for END of class name
    int offset = nameRange.getOffset() + nameRange.getLength();

    IBuffer buffer = type.getCompilationUnit().getBuffer();
    String contents = buffer.getText(offset, buffer.getLength() - offset);

    // warning, this doesn't handle "implements" and "{" contained in
    // comments in the middle of the declaration!
    int indexOfPar = contents.indexOf("{"); //$NON-NLS-1$

    contents = contents.substring(0, indexOfPar);

    int indexOfImplements = contents.indexOf("implements"); //$NON-NLS-1$
    if (indexOfImplements > -1) {
        buffer.replace(offset + indexOfImplements + "implements".length()//$NON-NLS-1$
                , 0, " Comparable<" + type.getElementName() + ">,"); //$NON-NLS-1$
    } else {
        buffer.replace(offset, 0, " implements Comparable<" + type.getElementName() + ">"); //$NON-NLS-1$
    }

    buffer.save(null, false);
    buffer.close();

}

From source file:org.eclipse.che.jdt.internal.core.Openable.java

License:Open Source License

/**
 * Close the buffer associated with this element, if any.
 *//* www  . j  a  va  2s.  c  om*/
protected void closeBuffer() {
    if (!hasBuffer())
        return; // nothing to do
    IBuffer buffer = getBufferManager().getBuffer(this);
    if (buffer != null) {
        buffer.close();
        buffer.removeBufferChangedListener(this);
    }
}

From source file:org.grails.ide.eclipse.test.GrailsSourceCodeTest.java

License:Open Source License

private void doTestType(GrailsVersion version, String typeName, String expectedSnippet) throws Exception {
    ensureDefaultGrailsVersion(version);
    project = ensureProject(emptyProjectName(version));
    IJavaProject javaProject = JavaCore.create(project);

    IType type = javaProject.findType(typeName);
    assertNotNull("Type not found on classpath: " + typeName, type);

    IClassFile classFile = type.getClassFile();
    assertNotNull("Couldn't obtain .class file for type: " + type, classFile);

    IBuffer sourceCode = classFile.getBuffer();
    assertNotNull("Couldn't obtain buffer (sourceCode) for .class file: " + classFile, sourceCode);
    try {/* w  ww. j  a  v a 2s. c  om*/
        System.out.println(sourceCode.getContents());

        assertContains(expectedSnippet, sourceCode.getContents());
    } finally {
        sourceCode.close();
    }
}

From source file:org.seasar.doma.extension.domax.ResourceFileChangeListener.java

License:Apache License

private void submitJob(final IResource resource, DaoMethod daoMethod) {
    IJavaProject javaProject = daoMethod.getJavaProject();
    try {/*w ww  . j ava 2s  .co m*/
        IType type = javaProject.findType(daoMethod.getClassName());
        if (type == null) {
            return;
        }
        final ICompilationUnit compilationUnit = type.getCompilationUnit();
        if (compilationUnit == null) {
            return;
        }

        WorkspaceJob job = new WorkspaceJob("building " + daoMethod.getClassName()) {

            @Override
            public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
                compilationUnit.becomeWorkingCopy(monitor);
                IBuffer buffer = compilationUnit.getBuffer();
                buffer.append("");
                buffer.save(monitor, true);
                buffer.close();
                compilationUnit.commitWorkingCopy(true, monitor);
                resource.getProject().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
                return Status.OK_STATUS;
            }
        };
        job.setPriority(Job.BUILD);
        job.schedule();
    } catch (JavaModelException e) {
        Logger.error(e);
    }
}