From 52d1162a149356cf411960cdb2ce4911f6d346c4 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sun, 21 Jul 2024 07:10:13 +0200 Subject: [PATCH] Simplify and clean-up VMUtil This also leverages the changs from https://github.com/eclipse-equinox/equinox/pull/655 --- .../pde/internal/core/util/VMUtil.java | 49 +++++-------------- .../launcher/PDEMigrationDelegate.java | 3 +- .../internal/launching/launcher/VMHelper.java | 15 +----- .../pde/internal/ui/launcher/JREBlock.java | 4 +- 4 files changed, 19 insertions(+), 52 deletions(-) diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/VMUtil.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/VMUtil.java index 9ebeacb0f9..fc2ba518b8 100755 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/VMUtil.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/VMUtil.java @@ -14,20 +14,19 @@ *******************************************************************************/ package org.eclipse.pde.internal.core.util; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Comparator; import java.util.Map; +import java.util.Optional; import java.util.Properties; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Status; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.launching.IVMInstall; -import org.eclipse.jdt.launching.IVMInstallType; import org.eclipse.jdt.launching.JavaRuntime; import org.eclipse.jdt.launching.environments.IExecutionEnvironment; import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; @@ -35,23 +34,12 @@ public class VMUtil { - public static IVMInstall[] getAllVMInstances() { - ArrayList res = new ArrayList<>(); - IVMInstallType[] types = JavaRuntime.getVMInstallTypes(); - for (IVMInstallType type : types) { - IVMInstall[] installs = type.getVMInstalls(); - Collections.addAll(res, installs); - } - return res.toArray(new IVMInstall[res.size()]); + public static Stream getAllVMInstances() { + return Arrays.stream(JavaRuntime.getVMInstallTypes()).flatMap(type -> Arrays.stream(type.getVMInstalls())); } public static String[] getVMInstallNames() { - IVMInstall[] installs = getAllVMInstances(); - String[] names = new String[installs.length]; - for (int i = 0; i < installs.length; i++) { - names[i] = installs[i].getName(); - } - return names; + return getAllVMInstances().map(IVMInstall::getName).toArray(String[]::new); } /** @@ -77,11 +65,9 @@ public static String getDefaultVMInstallLocation() { public static IVMInstall getVMInstall(String name) { if (name != null) { - IVMInstall[] installs = getAllVMInstances(); - for (IVMInstall install : installs) { - if (install.getName().equals(name)) { - return install; - } + Optional install = getAllVMInstances().filter(i -> i.getName().equals(name)).findFirst(); + if (install.isPresent()) { + return install.get(); } } return JavaRuntime.getDefaultVMInstall(); @@ -114,20 +100,11 @@ public static String getVMInstallName(IExecutionEnvironment ee) throws CoreExcep .thenComparing(Comparator.naturalOrder()); public static double getJavaTargetVersion(IExecutionEnvironment ee) { - return switch (ee.getId()) { - // The EE's handled explicitly have unexpected java targets. - // See https://github.com/eclipse-equinox/equinox/pull/655 - case "J2SE-1.2" -> 1.2; //$NON-NLS-1$ - case "J2SE-1.3" -> 1.3; //$NON-NLS-1$ - case "J2SE-1.4" -> 1.4; //$NON-NLS-1$ - default -> { - Properties properties = ee.getProfileProperties(); - Object target = properties != null // - ? properties.get(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM) - : null; - yield target instanceof String version ? Double.parseDouble(version) : 0.0; - } - }; + Properties properties = ee.getProfileProperties(); + Object target = properties != null // + ? properties.get(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM) + : null; + return target instanceof String version ? Double.parseDouble(version) : 0.0; } } diff --git a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/PDEMigrationDelegate.java b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/PDEMigrationDelegate.java index cbb2d90a64..4bac8e7eaf 100644 --- a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/PDEMigrationDelegate.java +++ b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/PDEMigrationDelegate.java @@ -21,6 +21,7 @@ import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; import org.eclipse.jdt.launching.IVMInstall; import org.eclipse.jdt.launching.JavaRuntime; +import org.eclipse.pde.internal.core.util.VMUtil; import org.eclipse.pde.internal.launching.IPDEConstants; public class PDEMigrationDelegate implements ILaunchConfigurationMigrationDelegate { @@ -51,7 +52,7 @@ public void migrate(ILaunchConfigurationWorkingCopy candidate) throws CoreExcept if (candidate.hasAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH)) { String name = candidate.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String) null); if (name != null) { - IVMInstall vm = VMHelper.getVMInstall(name); + IVMInstall vm = VMUtil.getVMInstall(name); if (vm != null) { IPath path = JavaRuntime.newJREContainerPath(vm); candidate.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, path.toPortableString()); diff --git a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/VMHelper.java b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/VMHelper.java index 90d0298b75..2a801c2ee8 100644 --- a/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/VMHelper.java +++ b/ui/org.eclipse.pde.launching/src/org/eclipse/pde/internal/launching/launcher/VMHelper.java @@ -188,7 +188,7 @@ public static IVMInstall getVMInstall(ILaunchConfiguration configuration) throws IExecutionEnvironment ee = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment(eeId); String vmName = VMUtil.getVMInstallName(ee); if (ee != null) { - IVMInstall vm = getVMInstall(vmName); + IVMInstall vm = VMUtil.getVMInstall(vmName); if (vm != null) { return vm; } @@ -197,7 +197,7 @@ public static IVMInstall getVMInstall(ILaunchConfiguration configuration) throws // Find a default JRE String defaultVMName = VMHelper.getDefaultVMInstallName(configuration); - IVMInstall vm = getVMInstall(defaultVMName); + IVMInstall vm = VMUtil.getVMInstall(defaultVMName); if (vm != null) { return vm; } @@ -207,17 +207,6 @@ public static IVMInstall getVMInstall(ILaunchConfiguration configuration) throws } - public static IVMInstall getVMInstall(String name) { - if (name != null) { - IVMInstall[] installs = VMUtil.getAllVMInstances(); - for (IVMInstall install : installs) { - if (install.getName().equals(name)) - return install; - } - } - return JavaRuntime.getDefaultVMInstall(); - } - public static IVMInstall createLauncher(ILaunchConfiguration configuration) throws CoreException { IVMInstall launcher = getVMInstall(configuration); if (!launcher.getInstallLocation().exists()) diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/JREBlock.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/JREBlock.java index 39d46adee7..beb247ab4c 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/JREBlock.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/launcher/JREBlock.java @@ -252,7 +252,7 @@ private void updateBootstrapEnablement() { if (fJreButton.getSelection()) { if (fJreCombo.getSelectionIndex() != -1) { String jreName = fJreCombo.getText(); - IVMInstall install = VMHelper.getVMInstall(jreName); + IVMInstall install = VMUtil.getVMInstall(jreName); // remove the name to make portable jrePath = JavaRuntime.newJREContainerPath(install); } @@ -324,7 +324,7 @@ protected void saveJRESection(ILaunchConfigurationWorkingCopy config) { if (fJreButton.getSelection()) { if (fJreCombo.getSelectionIndex() != -1) { String jreName = fJreCombo.getText(); - IVMInstall install = VMHelper.getVMInstall(jreName); + IVMInstall install = VMUtil.getVMInstall(jreName); // remove the name to make portable jrePath = JavaRuntime.newJREContainerPath(install); }