Skip to content

Commit

Permalink
Set default JDK to the lowest configured for managed projects.
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
rgrunber committed Apr 26, 2024
1 parent 199f5f2 commit 6c65ca2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Hashtable;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -41,6 +42,7 @@
import org.eclipse.jdt.launching.VMStandin;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
import org.eclipse.jdt.ls.core.internal.managers.IBuildSupport;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
Expand Down Expand Up @@ -108,6 +110,37 @@ public static boolean configureJVMs(Preferences preferences) throws CoreExceptio
public static boolean configureJVMs(Preferences preferences, JavaClientConnection connection) throws CoreException {
boolean changed = false;
boolean defaultVMSet = false;

IVMInstall minJDK = null;
ProjectsManager projectsManager = JavaLanguageServerPlugin.getProjectsManager();
for (IProject project : ProjectUtils.getAllProjects(false)) {
Optional<IBuildSupport> bs = projectsManager.getBuildSupport(project);
if (bs.isPresent() && !ProjectsManager.DEFAULT_PROJECT_NAME.equals(project.getName())) {
IJavaProject javaProject = ProjectUtils.getJavaProject(project);
if (javaProject != null) {
try {
IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
if (vmInstall instanceof AbstractVMInstall javaVMInstall) {
if (minJDK == null || JavaRuntime.compareJavaVersions(minJDK, javaVMInstall.getJavaVersion()) > 0) {
minJDK = vmInstall;
}
}
} catch (CoreException e) {
// ignore
}
}
}
}
if (minJDK != null && !Objects.equals(minJDK, JavaRuntime.getDefaultVMInstall())) {
try {
JavaRuntime.setDefaultVMInstall(minJDK, new NullProgressMonitor());
defaultVMSet = true;
changed = true;
} catch (CoreException e) {
// continue
}
}

Set<RuntimeEnvironment> runtimes = preferences.getRuntimes();
for (RuntimeEnvironment runtime : runtimes) {
if (runtime.isValid()) {
Expand Down Expand Up @@ -180,8 +213,8 @@ public static boolean configureJVMs(Preferences preferences, JavaClientConnectio
}
vm = vmStandin.convertToRealVM();
if (runtime.isDefault()) {
defaultVMSet = true;
if (!Objects.equals(vm, JavaRuntime.getDefaultVMInstall())) {
if (!defaultVMSet && !Objects.equals(vm, JavaRuntime.getDefaultVMInstall())) {
defaultVMSet = true;
JavaLanguageServerPlugin.logInfo("Setting runtime " + runtime.getName() + "-" + runtime.getInstallationFile() + " as default global VM");
JavaRuntime.setDefaultVMInstall(vm, new NullProgressMonitor());
changed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal;

import static org.eclipse.jdt.ls.core.internal.ProjectUtils.getJavaSourceLevel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -30,6 +31,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -42,11 +44,13 @@
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.LibraryLocation;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jdt.ls.core.internal.handlers.WorkspaceSymbolHandler;
import org.eclipse.jdt.ls.core.internal.managers.AbstractInvisibleProjectBasedTest;
import org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.SymbolInformation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -246,6 +250,19 @@ public void testInvalidRuntime() throws Exception {
assertEquals("Invalid runtime for " + runtime.getName() + ": 'bin' should be removed from the path (" + runtime.getPath() + ").", notification.getMessage());
}

// https://github.com/redhat-developer/vscode-java/issues/3452
@Test
public void testJavaRuntimesDoNotLeak() throws Exception {
importProjects("maven/salut-java11");
IProject project = WorkspaceHelper.getProject("salut-java11");
assertIsJavaProject(project);
assertEquals("11", getJavaSourceLevel(project));
JVMConfigurator.configureJVMs(preferences, javaClient);
List<SymbolInformation> results = WorkspaceSymbolHandler.search("java.lang.Object", new NullProgressMonitor());
int numOfObjectSymbols = results.stream().filter(s -> "java.lang".equals(s.getContainerName()) && "Object".equals(s.getName())).toList().size();
assertEquals(1, numOfObjectSymbols);
}

private void assertComplianceAndPreviewSupport(IJavaProject javaProject, String compliance, boolean previewEnabled) {
assertEquals(previewEnabled ? JavaCore.ENABLED : JavaCore.DISABLED, javaProject.getOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, true));
assertEquals(compliance, javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true));
Expand Down

0 comments on commit 6c65ca2

Please sign in to comment.