Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can get VM installation path through ProjectCommand.getProjectSettings() #1454

Merged
merged 2 commits into from
May 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@

package org.eclipse.jdt.ls.core.internal.commands;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
@@ -41,7 +42,9 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.ClasspathEntry;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaLaunchDelegate;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.ls.core.internal.IConstants;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
@@ -51,6 +54,7 @@

public class ProjectCommand {

public static final String VM_LOCATION = IConstants.PLUGIN_ID + ".vm.location";
private static final String TEST_SCOPE_VALUE = "test";

/**
@@ -61,7 +65,9 @@ public class ProjectCommand {
* @param settingKeys
* the settings we want to query, for example:
* ["org.eclipse.jdt.core.compiler.compliance",
* "org.eclipse.jdt.core.compiler.source"]
* "org.eclipse.jdt.core.compiler.source"].
* Besides the options defined in JavaCore, the following keys can also be used:
* - "org.eclipse.jdt.ls.core.vm.location": Get the location of the VM assigned to build the given Java project
* @return A <code>Map<string, string></code> with all the setting keys and
* their values.
* @throws CoreException
@@ -71,7 +77,22 @@ public static Map<String, String> getProjectSettings(String uri, List<String> se
IJavaProject javaProject = getJavaProjectFromUri(uri);
Map<String, String> settings = new HashMap<>();
for (String key : settingKeys) {
settings.putIfAbsent(key, javaProject.getOption(key, true));
switch(key) {
case VM_LOCATION:
IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
if (vmInstall == null) {
continue;
}
File location = vmInstall.getInstallLocation();
if (location == null) {
continue;
}
settings.putIfAbsent(key, location.getAbsolutePath());
break;
default:
settings.putIfAbsent(key, javaProject.getOption(key, true));
break;
}
}
return settings;
}
Original file line number Diff line number Diff line change
@@ -18,13 +18,16 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.ls.core.internal.WorkspaceHelper;
import org.eclipse.jdt.ls.core.internal.commands.ProjectCommand.ClasspathOptions;
import org.eclipse.jdt.ls.core.internal.commands.ProjectCommand.ClasspathResult;
@@ -62,11 +65,26 @@ public void testGetProjectSettingsForMavenJava8() throws Exception {
assertEquals("1.8", options.get("org.eclipse.jdt.core.compiler.source"));
}

@Test
public void testGetProjectVMInstallation() throws Exception {
importProjects("maven/salut2");
IProject project = WorkspaceHelper.getProject("salut2");
String uriString = project.getFile("src/main/java/foo/Bar.java").getLocationURI().toString();
List<String> settingKeys = Arrays.asList(ProjectCommand.VM_LOCATION);
Map<String, String> options = ProjectCommand.getProjectSettings(uriString, settingKeys);

IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(uriString);
IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
assertNotNull(vmInstall);
File location = vmInstall.getInstallLocation();
assertNotNull(location);
assertEquals(location.getAbsolutePath(), options.get(ProjectCommand.VM_LOCATION));
}

@Test
public void testGetProjectFromUri() throws Exception {
importProjects("maven/salut");
IProject project = WorkspaceHelper.getProject("salut");

String javaSource = project.getFile("src/main/java/Foo.java").getLocationURI().toString();
IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(javaSource);
assertNotNull("Can get project from java file uri", javaProject);