From f5858024eba5ad5723dfc074b9933bca1355caa7 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Mon, 28 Aug 2023 16:15:14 +0800 Subject: [PATCH] Try to read the version of fallback jdk (#1393) Signed-off-by: Sheng Chen --- .../gradle/bs/importer/EclipseVmUtil.java | 39 +++++-------------- .../GradleBuildServerBuildSupport.java | 4 +- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/EclipseVmUtil.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/EclipseVmUtil.java index 487b7d1cd..3a6fa21f2 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/EclipseVmUtil.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/EclipseVmUtil.java @@ -26,6 +26,7 @@ import org.apache.commons.lang3.StringUtils; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.launching.StandardVMType; import org.eclipse.jdt.launching.AbstractVMInstall; import org.eclipse.jdt.launching.IVMInstall; @@ -59,7 +60,7 @@ public class EclipseVmUtil { public static IVMInstall findOrRegisterStandardVM(String expectedVersion, String lowestVersion, String highestVersion, File location) { Optional vm = findRegisteredVM(expectedVersion, lowestVersion, highestVersion); - return vm.isPresent() ? vm.get() : registerNewVM("Java SE", location); + return vm.isPresent() ? vm.get() : registerNewVM(location); } private static Optional findRegisteredVM(String expectedVersion, String lowestVersion, @@ -150,7 +151,7 @@ public static Map getAllVmInstalls() { for (IVMInstall vmInstall : vmList) { if (vmInstall instanceof AbstractVMInstall vm) { String javaVersion = getMajorJavaVersion(vm.getJavaVersion()); - if (javaVersion == null || vm.getInstallLocation() == null + if (StringUtils.isBlank(javaVersion) || vm.getInstallLocation() == null || isEmbeddedJre(vm.getInstallLocation().getAbsolutePath())) { continue; } @@ -189,35 +190,9 @@ public static Map getAllVmInstalls() { private static String getMajorJavaVersion(String version) { if (version == null) { return null; - } else if (version.startsWith(JavaCore.VERSION_1_8)) { - return JavaCore.VERSION_1_8; - } else if (version.startsWith(JavaCore.VERSION_9)) { - return JavaCore.VERSION_9; - } else if (version.startsWith(JavaCore.VERSION_10)) { - return JavaCore.VERSION_10; - } else if (version.startsWith(JavaCore.VERSION_11)) { - return JavaCore.VERSION_11; - } else if (version.startsWith(JavaCore.VERSION_12)) { - return JavaCore.VERSION_12; - } else if (version.startsWith(JavaCore.VERSION_13)) { - return JavaCore.VERSION_13; - } else if (version.startsWith(JavaCore.VERSION_14)) { - return JavaCore.VERSION_14; - } else if (version.startsWith(JavaCore.VERSION_15)) { - return JavaCore.VERSION_15; - } else if (version.startsWith(JavaCore.VERSION_16)) { - return JavaCore.VERSION_16; - } else if (version.startsWith(JavaCore.VERSION_17)) { - return JavaCore.VERSION_17; - } else if (version.startsWith(JavaCore.VERSION_18)) { - return JavaCore.VERSION_18; - } else if (version.startsWith(JavaCore.VERSION_19)) { - return JavaCore.VERSION_19; - } else if (version.startsWith(JavaCore.VERSION_20)) { - return JavaCore.VERSION_20; } - return null; + return CompilerOptions.versionFromJdkLevel(CompilerOptions.versionToJdkLevel(version)); } private static String getExecutionEnvironmentId(String version) { @@ -232,7 +207,7 @@ private static String getExecutionEnvironmentId(String version) { } } - private static IVMInstall registerNewVM(String name, File location) { + private static IVMInstall registerNewVM(File location) { // use the 'Standard VM' type to register a new VM IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE); @@ -241,6 +216,10 @@ private static IVMInstall registerNewVM(String name, File location) { // create the VM without firing events on individual method calls VMStandin vm = new VMStandin(installType, vmId); + + String javaVersion = new StandardVMType().readReleaseVersion(location); + String name = StringUtils.isBlank(javaVersion) ? "Java SE" + : "JavaSE-" + getMajorJavaVersion(javaVersion); vm.setName(name); vm.setInstallLocation(location); return vm.convertToRealVM(); diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java index 821addcf1..96a6e0a90 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java @@ -563,7 +563,9 @@ private List getDependencyJars(DependencyModulesResult dependen */ private String getHighestCompatibleJavaVersion(String gradleVersion) { GradleVersion version = GradleVersion.version(gradleVersion); - if (version.compareTo(GradleVersion.version("7.6")) >= 0) { + if (version.compareTo(GradleVersion.version("8.3")) >= 0) { + return JavaCore.VERSION_20; + } else if (version.compareTo(GradleVersion.version("7.6")) >= 0) { return JavaCore.VERSION_19; } else if (version.compareTo(GradleVersion.version("7.5")) >= 0) { return JavaCore.VERSION_18;