Skip to content

Commit

Permalink
Process.toHandle and such
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Aug 20, 2024
1 parent ba20926 commit 28cf672
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<J_L_ProcessHandle> children(Process process) throws Throwable {
return toHandle(process).children();
}

@Stub
public static Stream<J_L_ProcessHandle> descendants(Process process) throws Throwable {
return toHandle(process).descendants();
}

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

0 comments on commit 28cf672

Please sign in to comment.