Java Code Examples for org.eclipse.jdt.core.IClassFile#getBuffer()

The following examples show how to use org.eclipse.jdt.core.IClassFile#getBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdtSourceLookUpProvider.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public String getSourceFileURI(String fullyQualifiedName, String sourcePath) {
    if (sourcePath == null) {
        return null;
    }

    Object sourceElement = JdtUtils.findSourceElement(sourcePath, getSourceContainers());
    if (sourceElement instanceof IResource) {
        return getFileURI((IResource) sourceElement);
    } else if (sourceElement instanceof IClassFile) {
        try {
            IClassFile file = (IClassFile) sourceElement;
            if (file.getBuffer() != null) {
                return getFileURI(file);
            }
        } catch (JavaModelException e) {
            // do nothing.
        }
    }
    return null;
}
 
Example 2
Source File: JdtSourceLookUpProvider.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
private String getContents(IClassFile cf) {
    String source = null;
    if (cf != null) {
        try {
            IBuffer buffer = cf.getBuffer();
            if (buffer != null) {
                source = buffer.getContents();
            }
        } catch (JavaModelException e) {
            logger.log(Level.SEVERE, String.format("Failed to parse the source contents of the class file: %s", e.toString()), e);
        }
        if (source == null) {
            source = "";
        }
    }
    return source;
}
 
Example 3
Source File: SourceContentProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String getSource(IClassFile classFile, IProgressMonitor monitor) throws CoreException {
	String source = null;
	try {
		IBuffer buffer = classFile.getBuffer();
		if (buffer == null) {
			ProjectsManager projectsManager = JavaLanguageServerPlugin.getProjectsManager();
			if (projectsManager != null) {
				Optional<IBuildSupport> bs = projectsManager.getBuildSupport(classFile.getJavaProject().getProject());
				if (bs.isPresent()) {
					bs.get().discoverSource(classFile, monitor);
				}
			}
			buffer = classFile.getBuffer();
		}
		if (buffer != null) {
			source = buffer.getContents();
			JavaLanguageServerPlugin.logInfo("ClassFile contents request completed");
		}
	} catch (JavaModelException e) {
		JavaLanguageServerPlugin.logException("Exception getting java element ", e);
	}
	return source;
}
 
Example 4
Source File: SourceAttachmentCommandTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testUpdateSourceAttachment_EmptySourceAttachmentPath() throws Exception {
	SourceAttachmentAttribute attributes = new SourceAttachmentAttribute(null, null, "UTF-8");
	SourceAttachmentRequest request = new SourceAttachmentRequest(classFileUri, attributes);
	String arguments = new Gson().toJson(request, SourceAttachmentRequest.class);
	SourceAttachmentResult updateResult = SourceAttachmentCommand.updateSourceAttachment(Arrays.asList(arguments), new NullProgressMonitor());
	assertNotNull(updateResult);
	assertNull(updateResult.errorMessage);

	// Verify no source is attached to the classfile.
	IClassFile classfile = JDTUtils.resolveClassFile(classFileUri);
	IBuffer buffer = classfile.getBuffer();
	assertNull(buffer);
}
 
Example 5
Source File: SourceAttachmentCommandTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testUpdateSourceAttachmentFromProjectJar() throws Exception {
	IResource source = project.findMember("foo-sources.jar");
	assertNotNull(source);
	IPath sourcePath = source.getLocation();
	SourceAttachmentAttribute attributes = new SourceAttachmentAttribute(null, sourcePath.toOSString(), "UTF-8");
	SourceAttachmentRequest request = new SourceAttachmentRequest(classFileUri, attributes);
	String arguments = new Gson().toJson(request, SourceAttachmentRequest.class);
	SourceAttachmentResult updateResult = SourceAttachmentCommand.updateSourceAttachment(Arrays.asList(arguments), new NullProgressMonitor());
	assertNotNull(updateResult);
	assertNull(updateResult.errorMessage);

	// Verify the source is attached to the classfile.
	IClassFile classfile = JDTUtils.resolveClassFile(classFileUri);
	IBuffer buffer = classfile.getBuffer();
	assertNotNull(buffer);
	assertTrue(buffer.getContents().indexOf("return sum;") >= 0);

	// Verify whether project inside jar attachment is saved with project relative path.
	IJavaProject javaProject = JavaCore.create(project);
	IPath relativePath = source.getFullPath();
	IPath absolutePath = source.getLocation();
	for (IClasspathEntry entry : javaProject.getRawClasspath()) {
		if (Objects.equals("foo.jar", entry.getPath().lastSegment())) {
			assertNotNull(entry.getSourceAttachmentPath());
			assertEquals(relativePath, entry.getSourceAttachmentPath());
			assertNotEquals(absolutePath, entry.getSourceAttachmentPath());
			break;
		}
	}
}
 
Example 6
Source File: SourceAttachmentCommandTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testUpdateSourceAttachmentFromExternalJar() throws Exception {
	File file = copyFiles("eclipse/external/foo-sources.jar", false);
	IPath sourcePath = Path.fromOSString(file.getAbsolutePath());
	SourceAttachmentAttribute attributes = new SourceAttachmentAttribute(null, sourcePath.toOSString(), "UTF-8");
	SourceAttachmentRequest request = new SourceAttachmentRequest(classFileUri, attributes);
	String arguments = new Gson().toJson(request, SourceAttachmentRequest.class);
	SourceAttachmentResult updateResult = SourceAttachmentCommand.updateSourceAttachment(Arrays.asList(arguments), new NullProgressMonitor());
	assertNotNull(updateResult);
	assertNull(updateResult.errorMessage);

	// Verify the source is attached to the classfile.
	IClassFile classfile = JDTUtils.resolveClassFile(classFileUri);
	IBuffer buffer = classfile.getBuffer();
	assertNotNull(buffer);
	assertTrue(buffer.getContents().indexOf("return sum;") >= 0);

	// Verify whether external jar attachment is saved with absolute path.
	IJavaProject javaProject = JavaCore.create(project);
	IPath relativePath = project.findMember("foo-sources.jar").getFullPath();
	IPath absolutePath = sourcePath;
	for (IClasspathEntry entry : javaProject.getRawClasspath()) {
		if (Objects.equals("foo.jar", entry.getPath().lastSegment())) {
			assertNotNull(entry.getSourceAttachmentPath());
			assertEquals(absolutePath, entry.getSourceAttachmentPath());
			assertNotEquals(relativePath, entry.getSourceAttachmentPath());
			break;
		}
	}
}