Java Code Examples for org.eclipse.jdt.ls.core.internal.JDTUtils#resolveClassFile()

The following examples show how to use org.eclipse.jdt.ls.core.internal.JDTUtils#resolveClassFile() . 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: HoverHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testHoverThrowable() throws Exception {
	String uriString = ClassFileUtil.getURI(project, "java.lang.Exception");
	IClassFile classFile = JDTUtils.resolveClassFile(uriString);
	String contents = JavaLanguageServerPlugin.getContentProviderManager().getSource(classFile, monitor);
	IDocument document = new Document(contents);
	IRegion region = new FindReplaceDocumentAdapter(document).find(0, "Throwable", true, false, false, false);
	int offset = region.getOffset();
	int line = document.getLineOfOffset(offset);
	int character = offset - document.getLineOffset(line);
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uriString);
	Position position = new Position(line, character);
	TextDocumentPositionParams params = new TextDocumentPositionParams(textDocument, position);
	Hover hover = handler.hover(params, monitor);
	assertNotNull(hover);
	assertTrue("Unexpected hover ", !hover.getContents().getLeft().isEmpty());
}
 
Example 2
Source File: SourceAttachmentCommand.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static IPackageFragmentRoot getPackageFragmentRoot(String classFileUri) throws CoreException {
	IClassFile classFile = JDTUtils.resolveClassFile(classFileUri);
	if (classFile == null) {
		throw constructCoreException("Cannot find the class file " + classFileUri);
	}

	IPackageFragmentRoot root = getPackageFragmentRoot(classFile);
	if (root == null) {
		throw constructCoreException("Cannot find the JAR containing this class file " + classFileUri);
	}

	return root;
}
 
Example 3
Source File: CodeLensHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public List<CodeLens> getCodeLensSymbols(String uri, IProgressMonitor monitor) {
	if (!preferenceManager.getPreferences().isCodeLensEnabled()) {
		return Collections.emptyList();
	}
	final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
	IClassFile classFile = null;
	if (unit == null) {
		classFile = JDTUtils.resolveClassFile(uri);
		if (classFile == null) {
			return Collections.emptyList();
		}
	} else {
		if (!unit.getResource().exists() || monitor.isCanceled()) {
			return Collections.emptyList();
		}
	}
	try {
		ITypeRoot typeRoot = unit != null ? unit : classFile;
		IJavaElement[] elements = typeRoot.getChildren();
		LinkedHashSet<CodeLens> lenses = new LinkedHashSet<>(elements.length);
		collectCodeLenses(typeRoot, elements, lenses, monitor);
		if (monitor.isCanceled()) {
			lenses.clear();
		}
		return new ArrayList<>(lenses);
	} catch (JavaModelException e) {
		JavaLanguageServerPlugin.logException("Problem getting code lenses for" + unit.getElementName(), e);
	}
	return Collections.emptyList();
}
 
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;
		}
	}
}
 
Example 7
Source File: ContentProviderManagerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void createURIs() throws Exception {
	importProjects("maven/salut");
	IProject project = WorkspaceHelper.getProject("salut");
	sourcelessURI = JDTUtils.toURI(ClassFileUtil.getURI(project, "java.math.BigDecimal"));
	sourcelessClassFile = JDTUtils.resolveClassFile(sourcelessURI);
	sourceAvailableURI = JDTUtils.toURI(ClassFileUtil.getURI(project, "org.apache.commons.lang3.text.WordUtils"));
	sourceAvailableClassFile = JDTUtils.resolveClassFile(sourceAvailableURI);
}