Skip to content

Commit 6c65ca2

Browse files
committed
Set default JDK to the lowest configured for managed projects.
- The default project is configured with a (default) JDK, that will produce unwanted symbols when other projects don't use the default JRE - Add testcase Signed-off-by: Roland Grunberg <[email protected]>
1 parent 199f5f2 commit 6c65ca2

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JVMConfigurator.java

+35-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Hashtable;
1919
import java.util.Map;
2020
import java.util.Objects;
21+
import java.util.Optional;
2122
import java.util.Set;
2223

2324
import org.apache.commons.lang3.StringUtils;
@@ -41,6 +42,7 @@
4142
import org.eclipse.jdt.launching.VMStandin;
4243
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
4344
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
45+
import org.eclipse.jdt.ls.core.internal.managers.IBuildSupport;
4446
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
4547
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
4648
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
@@ -108,6 +110,37 @@ public static boolean configureJVMs(Preferences preferences) throws CoreExceptio
108110
public static boolean configureJVMs(Preferences preferences, JavaClientConnection connection) throws CoreException {
109111
boolean changed = false;
110112
boolean defaultVMSet = false;
113+
114+
IVMInstall minJDK = null;
115+
ProjectsManager projectsManager = JavaLanguageServerPlugin.getProjectsManager();
116+
for (IProject project : ProjectUtils.getAllProjects(false)) {
117+
Optional<IBuildSupport> bs = projectsManager.getBuildSupport(project);
118+
if (bs.isPresent() && !ProjectsManager.DEFAULT_PROJECT_NAME.equals(project.getName())) {
119+
IJavaProject javaProject = ProjectUtils.getJavaProject(project);
120+
if (javaProject != null) {
121+
try {
122+
IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
123+
if (vmInstall instanceof AbstractVMInstall javaVMInstall) {
124+
if (minJDK == null || JavaRuntime.compareJavaVersions(minJDK, javaVMInstall.getJavaVersion()) > 0) {
125+
minJDK = vmInstall;
126+
}
127+
}
128+
} catch (CoreException e) {
129+
// ignore
130+
}
131+
}
132+
}
133+
}
134+
if (minJDK != null && !Objects.equals(minJDK, JavaRuntime.getDefaultVMInstall())) {
135+
try {
136+
JavaRuntime.setDefaultVMInstall(minJDK, new NullProgressMonitor());
137+
defaultVMSet = true;
138+
changed = true;
139+
} catch (CoreException e) {
140+
// continue
141+
}
142+
}
143+
111144
Set<RuntimeEnvironment> runtimes = preferences.getRuntimes();
112145
for (RuntimeEnvironment runtime : runtimes) {
113146
if (runtime.isValid()) {
@@ -180,8 +213,8 @@ public static boolean configureJVMs(Preferences preferences, JavaClientConnectio
180213
}
181214
vm = vmStandin.convertToRealVM();
182215
if (runtime.isDefault()) {
183-
defaultVMSet = true;
184-
if (!Objects.equals(vm, JavaRuntime.getDefaultVMInstall())) {
216+
if (!defaultVMSet && !Objects.equals(vm, JavaRuntime.getDefaultVMInstall())) {
217+
defaultVMSet = true;
185218
JavaLanguageServerPlugin.logInfo("Setting runtime " + runtime.getName() + "-" + runtime.getInstallationFile() + " as default global VM");
186219
JavaRuntime.setDefaultVMInstall(vm, new NullProgressMonitor());
187220
changed = true;

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/JVMConfiguratorTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*******************************************************************************/
1313
package org.eclipse.jdt.ls.core.internal;
1414

15+
import static org.eclipse.jdt.ls.core.internal.ProjectUtils.getJavaSourceLevel;
1516
import static org.junit.Assert.assertEquals;
1617
import static org.junit.Assert.assertNotEquals;
1718
import static org.junit.Assert.assertNotNull;
@@ -30,6 +31,7 @@
3031
import org.eclipse.core.runtime.CoreException;
3132
import org.eclipse.core.runtime.FileLocator;
3233
import org.eclipse.core.runtime.IStatus;
34+
import org.eclipse.core.runtime.NullProgressMonitor;
3335
import org.eclipse.core.runtime.Platform;
3436
import org.eclipse.core.runtime.URIUtil;
3537
import org.eclipse.jdt.core.IJavaProject;
@@ -42,11 +44,13 @@
4244
import org.eclipse.jdt.launching.JavaRuntime;
4345
import org.eclipse.jdt.launching.LibraryLocation;
4446
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
47+
import org.eclipse.jdt.ls.core.internal.handlers.WorkspaceSymbolHandler;
4548
import org.eclipse.jdt.ls.core.internal.managers.AbstractInvisibleProjectBasedTest;
4649
import org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences;
4750
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
4851
import org.eclipse.lsp4j.MessageParams;
4952
import org.eclipse.lsp4j.MessageType;
53+
import org.eclipse.lsp4j.SymbolInformation;
5054
import org.junit.After;
5155
import org.junit.Before;
5256
import org.junit.Test;
@@ -246,6 +250,19 @@ public void testInvalidRuntime() throws Exception {
246250
assertEquals("Invalid runtime for " + runtime.getName() + ": 'bin' should be removed from the path (" + runtime.getPath() + ").", notification.getMessage());
247251
}
248252

253+
// https://github.com/redhat-developer/vscode-java/issues/3452
254+
@Test
255+
public void testJavaRuntimesDoNotLeak() throws Exception {
256+
importProjects("maven/salut-java11");
257+
IProject project = WorkspaceHelper.getProject("salut-java11");
258+
assertIsJavaProject(project);
259+
assertEquals("11", getJavaSourceLevel(project));
260+
JVMConfigurator.configureJVMs(preferences, javaClient);
261+
List<SymbolInformation> results = WorkspaceSymbolHandler.search("java.lang.Object", new NullProgressMonitor());
262+
int numOfObjectSymbols = results.stream().filter(s -> "java.lang".equals(s.getContainerName()) && "Object".equals(s.getName())).toList().size();
263+
assertEquals(1, numOfObjectSymbols);
264+
}
265+
249266
private void assertComplianceAndPreviewSupport(IJavaProject javaProject, String compliance, boolean previewEnabled) {
250267
assertEquals(previewEnabled ? JavaCore.ENABLED : JavaCore.DISABLED, javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true));
251268
assertEquals(compliance, javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true));

0 commit comments

Comments
 (0)