From 4504ab26fb369f5fc27df55f9c1115a4f437ff8b Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sun, 24 Mar 2024 17:51:39 +0100 Subject: [PATCH] Enforce encapsulation of equinox-launcher Constants and JNIBridge --- .../META-INF/MANIFEST.MF | 1 - .../{internal => }/launcher/Constants.java | 6 +- .../eclipse/equinox/launcher/JNIBridge.java | 5 +- .../org/eclipse/equinox/launcher/Main.java | 4 +- .../equinox/launcher}/TestLauncherApp.java | 9 +-- .../launcher}/TestLauncherConstants.java | 2 +- .../src/test/java/LauncherTests.java | 58 +++++++++---------- 7 files changed, 39 insertions(+), 46 deletions(-) rename bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/{internal => }/launcher/Constants.java (97%) rename features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/{main => org/eclipse/equinox/launcher}/TestLauncherApp.java (98%) rename features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/{main => org/eclipse/equinox/launcher}/TestLauncherConstants.java (96%) diff --git a/bundles/org.eclipse.equinox.launcher/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.launcher/META-INF/MANIFEST.MF index bda318f306d..d7b8d590155 100644 --- a/bundles/org.eclipse.equinox.launcher/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.launcher/META-INF/MANIFEST.MF @@ -8,6 +8,5 @@ Bundle-Vendor: %providerName Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Bundle-Localization: launcher Export-Package: org.eclipse.core.launcher;x-internal:=true, - org.eclipse.equinox.internal.launcher;x-internal:=true, org.eclipse.equinox.launcher;x-internal:=true Automatic-Module-Name: org.eclipse.equinox.launcher diff --git a/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/internal/launcher/Constants.java b/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Constants.java similarity index 97% rename from bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/internal/launcher/Constants.java rename to bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Constants.java index 5544c822bf2..a5be5611dee 100644 --- a/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/internal/launcher/Constants.java +++ b/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Constants.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2009 IBM Corporation and others. + * Copyright (c) 2006, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -11,12 +11,12 @@ * Contributors: * Andrew Niefer - IBM Corporation - initial API and implementation *******************************************************************************/ -package org.eclipse.equinox.internal.launcher; +package org.eclipse.equinox.launcher; /** * @author aniefer */ -public class Constants { +class Constants { public static final String INTERNAL_AMD64 = "amd64"; //$NON-NLS-1$ public static final String INTERNAL_OS_SUNOS = "SunOS"; //$NON-NLS-1$ public static final String INTERNAL_OS_LINUX = "Linux"; //$NON-NLS-1$ diff --git a/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/JNIBridge.java b/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/JNIBridge.java index 9eba8fd37e2..724249e24b5 100644 --- a/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/JNIBridge.java +++ b/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/JNIBridge.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2015 IBM Corporation and others. + * Copyright (c) 2006, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -23,8 +23,7 @@ * @noextend This class is not intended to be subclassed by clients. * @noinstantiate This class is not intended to be instantiated by clients. */ -public class JNIBridge { - //TODO: This class should not be public +class JNIBridge { private native void _set_exit_data(String sharedId, String data); private native void _set_launcher_info(String launcher, String name); diff --git a/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Main.java b/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Main.java index 8c51b9eb231..680bde24171 100644 --- a/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Main.java +++ b/bundles/org.eclipse.equinox.launcher/src/org/eclipse/equinox/launcher/Main.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -59,8 +59,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import org.eclipse.equinox.internal.launcher.Constants; - /** * The launcher for Eclipse. * diff --git a/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/main/TestLauncherApp.java b/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/org/eclipse/equinox/launcher/TestLauncherApp.java similarity index 98% rename from features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/main/TestLauncherApp.java rename to features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/org/eclipse/equinox/launcher/TestLauncherApp.java index 0e868cd21a1..2b691a0d6bd 100644 --- a/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/main/TestLauncherApp.java +++ b/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/org/eclipse/equinox/launcher/TestLauncherApp.java @@ -11,7 +11,7 @@ * Contributors: * Umair Sair - initial API and implementation *******************************************************************************/ -package main; +package org.eclipse.equinox.launcher; import java.io.BufferedReader; import java.io.DataOutputStream; @@ -31,11 +31,11 @@ * port and accepts following queries * - -args - returns the arguments passed to this application. Test verify if the * arguments are passed to application correctly by the launcher. - * + * * And accepts following information * - -exitdata - The exit data this application should set before exiting * - -exitcode - The exit code with which this application should exit - * + * * @author umairsair * */ @@ -98,8 +98,9 @@ private static void communicateToServer() throws Exception { out.flush(); } else if (TestLauncherConstants.EXITDATA_PARAMETER.equals(line)) { while ((line = in.readLine()) != null) { - if (TestLauncherConstants.MULTILINE_ARG_VALUE_TERMINATOR.equals(line)) + if (TestLauncherConstants.MULTILINE_ARG_VALUE_TERMINATOR.equals(line)) { break; + } exitData.add(line); } } else if (TestLauncherConstants.EXITCODE_PARAMETER.equals(line)) { diff --git a/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/main/TestLauncherConstants.java b/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/org/eclipse/equinox/launcher/TestLauncherConstants.java similarity index 96% rename from features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/main/TestLauncherConstants.java rename to features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/org/eclipse/equinox/launcher/TestLauncherConstants.java index 5bd1e0bed41..ac87e42833e 100644 --- a/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/main/TestLauncherConstants.java +++ b/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/org/eclipse/equinox/launcher/TestLauncherConstants.java @@ -11,7 +11,7 @@ * Contributors: * Umair Sair - initial API and implementation *******************************************************************************/ -package main; +package org.eclipse.equinox.launcher; public interface TestLauncherConstants { public static final String ARGS_PARAMETER = "-args"; //$NON-NLS-1$ diff --git a/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/test/java/LauncherTests.java b/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/test/java/LauncherTests.java index 037d608cbd6..859eb91b76e 100644 --- a/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/test/java/LauncherTests.java +++ b/features/org.eclipse.equinox.executable.feature/library/org.eclipse.launcher.tests/src/test/java/LauncherTests.java @@ -20,42 +20,40 @@ import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; +import org.eclipse.equinox.launcher.TestLauncherConstants; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import main.TestLauncherConstants; - public class LauncherTests { private static final String ECLIPSE_INI_PATH_KEY = "ECLIPSE_INI_PATH"; // eclipse ini file name is relative to eclipse binary. e.g., in mac, it is ../Eclipse/eclipse.ini // and on other hosts, it is present in same directory as eclipse binary private static final String ECLIPSE_INI_FILE_NAME = System.getProperty(ECLIPSE_INI_PATH_KEY, System.getenv(ECLIPSE_INI_PATH_KEY) == null ? "eclipse.ini" : System.getenv(ECLIPSE_INI_PATH_KEY)); - // @formatter:off - private static final String DEFAULT_ECLIPSE_INI_CONTENT = "-startup\n" - + "../test.launcher.jar\n" - + "--launcher.library\n" - + "plugins/org.eclipse.equinox.launcher\n" - + "-vmargs\n" - + "-Xms40m\n" - + ""; - // @formatter:on - public static final Integer EXIT_OK = Integer.valueOf(0); - public static final Integer EXIT_RESTART = Integer.valueOf(23); - public static final Integer EXIT_RELAUNCH = Integer.valueOf(24); + private static final String DEFAULT_ECLIPSE_INI_CONTENT = """ + -startup + ../test.launcher.jar + --launcher.library + plugins/org.eclipse.equinox.launcher + -Xms40m + -vmargs + """; + public static final Integer EXIT_OK = 0; + public static final Integer EXIT_RESTART = 23; + public static final Integer EXIT_RELAUNCH = 24; private ServerSocket server; @@ -121,8 +119,8 @@ void test_eclipseIniChangesShouldBePickedOnRestart() throws IOException { appArgs2.remove(appArgs2.indexOf("-Dtest")); // Convert backslashes to forward slashes before comparison so that all paths are consistent - assertEquals(appArgs1.stream().map(s -> s.replace('\\', '/')).collect(Collectors.toList()), - appArgs2.stream().map(s -> s.replace('\\', '/')).collect(Collectors.toList())); + assertEquals(appArgs1.stream().map(s -> s.replace('\\', '/')).toList(), + appArgs2.stream().map(s -> s.replace('\\', '/')).toList()); } @Test @@ -157,8 +155,8 @@ void test_eclipseIniChangesShouldBePickedOnRelaunch() throws IOException { appArgs2.remove(appArgs2.indexOf("dir1")); // Convert backslashes to forward slashes before comparison so that all paths are consistent - assertEquals(appArgs1.stream().map(s -> s.replace('\\', '/')).collect(Collectors.toList()), - appArgs2.stream().map(s -> s.replace('\\', '/')).collect(Collectors.toList())); + assertEquals(appArgs1.stream().map(s -> s.replace('\\', '/')).toList(), + appArgs2.stream().map(s -> s.replace('\\', '/')).toList()); } @Test @@ -490,8 +488,8 @@ void test_ArgsRemainSameOverRestarts() throws IOException { appArgs2.remove(appArgs2.indexOf(TestLauncherConstants.EXITDATA_PARAMETER) + 1); // Convert backslashes to forward slashes before comparison so that all paths are consistent - assertEquals(appArgs1.stream().map(s -> s.replace('\\', '/')).collect(Collectors.toList()), - appArgs2.stream().map(s -> s.replace('\\', '/')).collect(Collectors.toList())); + assertEquals(appArgs1.stream().map(s -> s.replace('\\', '/')).toList(), + appArgs2.stream().map(s -> s.replace('\\', '/')).toList()); } } @@ -516,8 +514,8 @@ void test_ArgsRemainSameOverRelaunches() throws IOException { appArgs2.remove(appArgs2.indexOf(TestLauncherConstants.EXITDATA_PARAMETER) + 1); // Convert backslashes to forward slashes before comparison so that all paths are consistent - assertEquals(appArgs1.stream().map(s -> s.replace('\\', '/')).collect(Collectors.toList()), - appArgs2.stream().map(s -> s.replace('\\', '/')).collect(Collectors.toList())); + assertEquals(appArgs1.stream().map(s -> s.replace('\\', '/')).toList(), + appArgs2.stream().map(s -> s.replace('\\', '/')).toList()); } } @@ -530,16 +528,18 @@ private void analyzeLaunchedTestApp(Socket socket, List appArgs, String String line = null; System.out.println("--- start ----"); while ((line = in.readLine()) != null) { - if (TestLauncherConstants.MULTILINE_ARG_VALUE_TERMINATOR.equals(line)) + if (TestLauncherConstants.MULTILINE_ARG_VALUE_TERMINATOR.equals(line)) { break; + } System.out.println(line); appArgs.add(line); } System.out.println("--- end ----"); { out.writeBytes(TestLauncherConstants.EXITDATA_PARAMETER + "\n"); - if (restartArgs != null && !restartArgs.isBlank()) + if (restartArgs != null && !restartArgs.isBlank()) { out.writeBytes(restartArgs + "\n"); + } out.writeBytes(TestLauncherConstants.MULTILINE_ARG_VALUE_TERMINATOR + "\n"); out.flush(); @@ -562,11 +562,7 @@ private Process startEclipseLauncher(List args) throws IOException { } private void writeEclipseIni(String content) throws IOException { - File iniFile = new File(ECLIPSE_INI_FILE_NAME); - iniFile.createNewFile(); - FileWriter myWriter = new FileWriter(ECLIPSE_INI_FILE_NAME); - myWriter.write(content); - myWriter.close(); + Files.writeString(Path.of(ECLIPSE_INI_FILE_NAME), content); } }