diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JVMConfigurator.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JVMConfigurator.java index a0bdb7045f..3780ba1bdc 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JVMConfigurator.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JVMConfigurator.java @@ -16,9 +16,11 @@ import java.net.URL; import java.util.Arrays; import java.util.Hashtable; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.eclipse.core.resources.IProject; @@ -73,7 +75,7 @@ public static boolean configureDefaultVM(String javaHome) throws CoreException { return false; } - IVMInstall vm = findVM(jvmHome, null); + IVMInstall vm = findVM(jvmHome); if (vm == null) { IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE); if (installType == null || installType.getVMInstalls().length == 0) { @@ -109,13 +111,19 @@ public static boolean configureJVMs(Preferences preferences, JavaClientConnectio boolean changed = false; boolean defaultVMSet = false; Set runtimes = preferences.getRuntimes(); - for (RuntimeEnvironment runtime : runtimes) { - if (runtime.isValid()) { + Map> environments = runtimes + .stream() + .filter(RuntimeEnvironment::isValid) + .collect(Collectors.groupingBy(RuntimeEnvironment::getName)); + for (String environmentName : environments.keySet()) { + List envRuntimes = environments.get(environmentName); + for (int i = 0; i < envRuntimes.size(); i++) { + RuntimeEnvironment runtime = envRuntimes.get(i); File file = runtime.getInstallationFile(); if (file != null && file.isDirectory()) { URL javadocURL = runtime.getJavadocURL(); IPath sourcePath = runtime.getSourcePath(); - IVMInstall vm = findVM(file, runtime.getName()); + IVMInstall vm = findVM(file); IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE); if (installType == null || installType.getVMInstalls().length == 0) { // https://github.com/eclipse/eclipse.jdt.ls/issues/1646 @@ -163,13 +171,13 @@ public static boolean configureJVMs(Preferences preferences, JavaClientConnectio } boolean libChanged = false; if (libs != null) { - for (int i = 0; i < libs.length; i++) { - LibraryLocation lib = libs[i]; + for (int j = 0; j < libs.length; j++) { + LibraryLocation lib = libs[j]; IPath systemSourcePath = sourcePath != null ? sourcePath : lib.getSystemLibrarySourcePath(); URL javadocLocation = javadocURL != null ? javadocURL : lib.getJavadocLocation(); LibraryLocation newLib = new LibraryLocation(lib.getSystemLibraryPath(), systemSourcePath, lib.getPackageRootPath(), javadocLocation, lib.getIndexLocation(), lib.getExternalAnnotationsPath()); libChanged = libChanged || !newLib.equals(lib); - libs[i] = newLib; + libs[j] = newLib; } } if (libChanged) { @@ -187,7 +195,7 @@ public static boolean configureJVMs(Preferences preferences, JavaClientConnectio changed = true; } } - if (!setDefaultEnvironmentVM(vm, runtime.getName())) { + if (i == 0 && !setDefaultEnvironmentVM(vm, runtime.getName())) { sendNotification(connection, "Invalid runtime for " + runtime.getName() + ": Runtime at '" + runtime.getPath() + "' is not compatible with the '" + runtime.getName() + "' environment."); JavaLanguageServerPlugin.logError("Runtime at '" + runtime.getPath() + "' is not compatible with the '" + runtime.getName() + "' environment"); } @@ -261,14 +269,11 @@ public static IExecutionEnvironment getExecutionEnvironment(String name) { return null; } - public static IVMInstall findVM(File file, String name) { + public static IVMInstall findVM(File file) { IVMInstallType[] types = JavaRuntime.getVMInstallTypes(); for (IVMInstallType type : types) { IVMInstall[] installs = type.getVMInstalls(); for (IVMInstall install : installs) { - if (name != null && name.equals(install.getName())) { - return install; - } if (file != null && file.equals(install.getInstallLocation())) { return install; } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/RuntimeEnvironment.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/RuntimeEnvironment.java index 8587145e3b..15a652385b 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/RuntimeEnvironment.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/RuntimeEnvironment.java @@ -17,6 +17,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.util.Objects; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; @@ -109,14 +110,8 @@ public boolean equals(Object obj) { return false; } RuntimeEnvironment other = (RuntimeEnvironment) obj; - if (name == null) { - if (other.name != null) { - return false; - } - } else if (!name.equals(other.name)) { - return false; - } - return true; + return Objects.equals(name, other.name) && + Objects.equals(path, other.path); } public boolean isValid() { diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java index 11d01ea438..acc0107f6a 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java @@ -1215,7 +1215,7 @@ public static Preferences createFrom(Map configuration) { prefs.setStaticImportOnDemandThreshold(staticOnDemandThreshold); List runtimeList = getList(configuration, JAVA_CONFIGURATION_RUNTIMES, JAVA_CONFIGURATION_RUNTIMES_DEFAULT); - Set runtimes = new HashSet<>(); + Set runtimes = new LinkedHashSet<>(); boolean[] hasDefault = { false }; for (Object object : runtimeList) { if (object instanceof Map map) {