From f46097a3e808a0a019bbb8c374b952be8450486d Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Tue, 16 Apr 2024 23:12:19 +0200 Subject: [PATCH] Process EE_HOME lazy in ExecutionEnvironmentDescription This avoids the need to specify the 'ee.home' property for all descriptions even if all paths are already absolute and the '${ee.home}' variable is not used and therefore ee.home is effectively unused. --- .../debug/tests/core/EEDefinitionTests.java | 41 ++++++++++++------- .../ExecutionEnvironmentDescription.java | 39 ++++++++++-------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/EEDefinitionTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/EEDefinitionTests.java index 5af953b9da..f3d2ae95de 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/EEDefinitionTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/EEDefinitionTests.java @@ -15,8 +15,12 @@ package org.eclipse.jdt.debug.tests.core; import java.io.File; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.util.Arrays; +import java.util.List; +import java.util.Map; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; @@ -42,7 +46,7 @@ public class EEDefinitionTests extends AbstractDebugTest { public static IPath TEST_EE_FILE = null; { - if (Platform.OS_WIN32.equals(Platform.getOS())) { + if (Platform.OS.isWindows()) { TEST_EE_FILE = new Path("testfiles/test-jre/bin/test-foundation11-win32.ee"); } else { TEST_EE_FILE = new Path("testfiles/test-jre/bin/test-foundation11.ee"); @@ -155,17 +159,12 @@ public void testDefaultLibraries() { /** * Tests that a javadoc location can be specified. */ - public void testJavadocLocation() throws CoreException { + public void testJavadocLocation() throws CoreException, MalformedURLException { File file = getEEFile(); assertNotNull("Missing EE file", file); ExecutionEnvironmentDescription ee = new ExecutionEnvironmentDescription(file); URL location = EEVMType.getJavadocLocation(ee); - URL expectedLocation = null; - try { - expectedLocation = new URL("http://a.javadoc.location"); - } catch (MalformedURLException e) { - fail(); - } + URL expectedLocation = new URL("http://a.javadoc.location"); assertEquals("Incorrect javadoc location", expectedLocation, location); } @@ -179,12 +178,7 @@ public void testIndexLocation() throws Exception { assertNotNull("Missing EE file", file); ExecutionEnvironmentDescription ee = new ExecutionEnvironmentDescription(file); URL location = EEVMType.getIndexLocation(ee); - URL expectedLocation = null; - try { - expectedLocation = new URL("http://a.index.location"); - } catch (MalformedURLException e) { - fail(); - } + URL expectedLocation = new URL("http://a.index.location"); assertEquals("Incorrect index location", expectedLocation, location); } @@ -292,4 +286,23 @@ public void testEmptyProperty() throws CoreException { ExecutionEnvironmentDescription ee = new ExecutionEnvironmentDescription(file); validateProperty("-Dee.empty", "", ee); } + + public void testNoEEHomeRequiredForAbsolutePaths() throws IOException { + IVMInstall jre = JavaRuntime.getDefaultVMInstall(); + IExecutionEnvironment ee = Arrays.stream(JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments()) // + .filter(e -> e.isStrictlyCompatible(jre)).findFirst().orElseThrow(); + String installLocation = jre.getInstallLocation().getCanonicalPath(); + File exePath = new File(jre.getInstallLocation(), "bin/" + (Platform.OS.isWindows() ? "java.exe" : "java")); + List libraryPaths = Arrays.stream(JavaRuntime.getLibraryLocations(jre)) // + .map(LibraryLocation::getSystemLibraryPath).map(IPath::toOSString).toList(); + + ExecutionEnvironmentDescription description = new ExecutionEnvironmentDescription(Map.of( // + ExecutionEnvironmentDescription.JAVA_HOME, installLocation, // + ExecutionEnvironmentDescription.CLASS_LIB_LEVEL, ee.getId(), // + ExecutionEnvironmentDescription.EXECUTABLE, exePath.toString(), // + ExecutionEnvironmentDescription.BOOT_CLASS_PATH, String.join(File.pathSeparator, libraryPaths))); + + assertTrue(description.getLibraryLocations().length > 0); + assertNotNull(description.getExecutable()); + } } diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/environments/ExecutionEnvironmentDescription.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/environments/ExecutionEnvironmentDescription.java index 0bab728c6c..a4d41f2f5b 100644 --- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/environments/ExecutionEnvironmentDescription.java +++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/environments/ExecutionEnvironmentDescription.java @@ -25,6 +25,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -32,7 +33,6 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Status; import org.eclipse.jdt.internal.launching.EEVMType; import org.eclipse.jdt.internal.launching.LaunchingMessages; @@ -232,6 +232,10 @@ public String getProperty(String property) { return fProperties.get(property); } + private Optional getEEHomePath() { + return Optional.ofNullable(getProperty(EE_HOME)); + } + /** * Returns the location of the system libraries defined in this execution environment. * Libraries are generated from the endorsed directories, boot class path, additional @@ -259,10 +263,10 @@ public LibraryLocation[] getLibraryLocations() { URL url = getJavadocLocation(); URL indexurl = getIndexLocation(); for (int i = 0; i < bootpath.length; i++) { - IPath path = new Path(bootpath[i]); + IPath path = IPath.fromOSString(bootpath[i]); File lib = path.toFile(); if (lib.exists() && lib.isFile()) { - LibraryLocation libraryLocation = new LibraryLocation(path, src, Path.EMPTY, url, indexurl); + LibraryLocation libraryLocation = new LibraryLocation(path, src, IPath.EMPTY, url, indexurl); boot.add(libraryLocation); } } @@ -302,7 +306,7 @@ public LibraryLocation[] getLibraryLocations() { */ public String getVMArguments() { StringBuilder arguments = new StringBuilder(); - String eeHome = fProperties.get(EE_HOME); + Optional eeHome = getEEHomePath(); fProperties.forEach((key, value) -> { boolean appendArgument = !key.startsWith(EE_ARG_FILTER); if (appendArgument) { @@ -390,7 +394,7 @@ private static Map loadProperties(File eeFile) throws CoreExcept } private static Map resolveEEHome(Map eeProperties) { // resolve things with ${ee.home} in them - String eeHome = eeProperties.get(EE_HOME); + Optional eeHome = Optional.ofNullable(eeProperties.get(EE_HOME)); eeProperties.replaceAll((k, value) -> value != null ? resolveHome(value, eeHome) : ""); //$NON-NLS-1$ return eeProperties; } @@ -402,7 +406,7 @@ private static Map resolveEEHome(Map eePropertie * @param value string to process * @return resolved string */ - private static String resolveHome(String value, String eeHome) { + private static String resolveHome(String value, Optional eeHome) { int start = 0; int index = value.indexOf(VAR_EE_HOME, start); StringBuilder replaced = null; @@ -411,7 +415,7 @@ private static String resolveHome(String value, String eeHome) { replaced = new StringBuilder(); } replaced.append(value.substring(start, index)); - replaced.append(eeHome); + replaced.append(eeHome.orElseThrow()); start = index + VAR_EE_HOME.length(); index = value.indexOf(VAR_EE_HOME, start); } @@ -431,8 +435,7 @@ private static String resolveHome(String value, String eeHome) { */ private String[] resolvePaths(String paths) { String[] strings = paths.split(File.pathSeparator, -1); - String eeHome = getProperty(EE_HOME); - IPath root = new Path(eeHome); + Optional root = getEEHomePath(); for (int i = 0; i < strings.length; i++) { strings[i] = makePathAbsolute(strings[i], root); } @@ -448,10 +451,10 @@ private String[] resolvePaths(String paths) { * @param root root to append non-absolute paths to * @return absolute, OS specific path */ - private String makePathAbsolute(String pathString, IPath root){ - IPath path = new Path(pathString.trim()); + private String makePathAbsolute(String pathString, Optional root) { + IPath path = IPath.fromOSString(pathString.trim()); if (!path.isEmpty() && !path.isAbsolute()) { - IPath filePath = root.append(path); + IPath filePath = IPath.fromOSString(root.orElseThrow()).append(path); return filePath.toOSString(); } return path.toOSString(); @@ -483,7 +486,7 @@ private Map getSourceMap(){ for (int i = 0; i < entries.length; i++) { int index = entries[i].indexOf('='); if (index > 0 && index < entries[i].length()-1){ - IPath root = new Path(getProperty(EE_HOME)); + Optional root = getEEHomePath(); String key = entries[i].substring(0,index); String value = entries[i].substring(index+1); key = makePathAbsolute(key, root); @@ -562,7 +565,7 @@ private void addSourceLocationsToLibraries(Map srcMap, List srcMap, List eeHome = getEEHomePath(); + src = makePathAbsolute(src, eeHome); + return IPath.fromOSString(src); } - return Path.EMPTY; + return IPath.EMPTY; } /**