Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify and clean-up VMUtil #1344

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,32 @@
*******************************************************************************/
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;
import org.eclipse.osgi.util.NLS;

public class VMUtil {

public static IVMInstall[] getAllVMInstances() {
ArrayList<IVMInstall> 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<IVMInstall> 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);
}

/**
Expand All @@ -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<IVMInstall> install = getAllVMInstances().filter(i -> i.getName().equals(name)).findFirst();
if (install.isPresent()) {
return install.get();
}
}
return JavaRuntime.getDefaultVMInstall();
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
Loading