From 28cf672673553973f64b613babd6612dba314a2d Mon Sep 17 00:00:00 2001 From: Wagyourtail Date: Tue, 20 Aug 2024 12:08:17 -0500 Subject: [PATCH] Process.toHandle and such --- .../jvmdg/j9/stub/java_base/J_L_Process.java | 33 +++++++++++++++++++ .../jvmdg/providers/Java9Downgrader.java | 2 +- .../downgradetest/TestProcessHandle.java | 15 ++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 java-api/src/java9/java/xyz/wagyourtail/jvmdg/j9/stub/java_base/J_L_Process.java diff --git a/java-api/src/java9/java/xyz/wagyourtail/jvmdg/j9/stub/java_base/J_L_Process.java b/java-api/src/java9/java/xyz/wagyourtail/jvmdg/j9/stub/java_base/J_L_Process.java new file mode 100644 index 00000000..734f2e4c --- /dev/null +++ b/java-api/src/java9/java/xyz/wagyourtail/jvmdg/j9/stub/java_base/J_L_Process.java @@ -0,0 +1,33 @@ +package xyz.wagyourtail.jvmdg.j9.stub.java_base; + +import xyz.wagyourtail.jvmdg.util.Utils; +import xyz.wagyourtail.jvmdg.version.Stub; + +import java.lang.invoke.MethodHandles; +import java.util.stream.Stream; + +public class J_L_Process { + private static final MethodHandles.Lookup IMPL_LOOKUP = Utils.getImplLookup(); + + @Stub + public static J_L_ProcessHandle toHandle(Process process) throws Throwable { + long pid = (long) IMPL_LOOKUP.findGetter(process.getClass(), "pid", int.class).invoke(process); + return J_L_ProcessHandle.of(pid).get(); + } + + @Stub + public static J_L_ProcessHandle.Info info(Process process) throws Throwable { + return toHandle(process).info(); + } + + @Stub + public static Stream children(Process process) throws Throwable { + return toHandle(process).children(); + } + + @Stub + public static Stream descendants(Process process) throws Throwable { + return toHandle(process).descendants(); + } + +} diff --git a/java-api/src/main/java/xyz/wagyourtail/jvmdg/providers/Java9Downgrader.java b/java-api/src/main/java/xyz/wagyourtail/jvmdg/providers/Java9Downgrader.java index 85ea20e9..d3661ec9 100644 --- a/java-api/src/main/java/xyz/wagyourtail/jvmdg/providers/Java9Downgrader.java +++ b/java-api/src/main/java/xyz/wagyourtail/jvmdg/providers/Java9Downgrader.java @@ -36,7 +36,7 @@ public void init() { stub(J_L_Math.class); stub(J_L_Module.class); stub(J_L_ModuleLayer.class); - // Process + stub(J_L_Process.class); // ProcessBuilder stub(J_L_ProcessHandle.class); // ProcessHandleImpl diff --git a/testing/downgrade/src/main/java/xyz/wagyourtail/downgradetest/TestProcessHandle.java b/testing/downgrade/src/main/java/xyz/wagyourtail/downgradetest/TestProcessHandle.java index 0bbeca8d..5845cf11 100644 --- a/testing/downgrade/src/main/java/xyz/wagyourtail/downgradetest/TestProcessHandle.java +++ b/testing/downgrade/src/main/java/xyz/wagyourtail/downgradetest/TestProcessHandle.java @@ -1,15 +1,28 @@ package xyz.wagyourtail.downgradetest; +import java.io.IOException; import java.lang.management.ManagementFactory; import java.util.Arrays; import java.util.List; public class TestProcessHandle { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { ProcessHandle currentProcessHandle = ProcessHandle.current(); System.out.println(currentProcessHandle.pid() == ManagementFactory.getRuntimeMXBean().getPid()); List lst = Arrays.asList(currentProcessHandle.info().arguments().get()); System.out.println(lst.subList(lst.size() - 3, lst.size())); + + ProcessBuilder pb; + // check if windows + if (System.getProperty("os.name").toLowerCase().contains("windows")) { + pb = new ProcessBuilder("timeout", "1"); + } else { + pb = new ProcessBuilder("sleep", "1"); + } + Process p = pb.start(); + currentProcessHandle.children().map(e -> String.join(" ", e.info().arguments().get())).forEach(System.out::println); + p.toHandle().onExit().thenAccept(e -> System.out.println(e.info().commandLine())); +// Thread.sleep(1); } }