Skip to content

Commit

Permalink
Can get VM installation path through ProjectCommand.getProjectSetting…
Browse files Browse the repository at this point in the history
…s() (#1454)

Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo authored May 26, 2020
1 parent b3e92e1 commit 6181a85
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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";

/**
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6181a85

Please sign in to comment.