Skip to content

Commit

Permalink
Use BCIG to get PID.
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Dec 9, 2023
1 parent 74b1738 commit 7cc5e7d
Showing 1 changed file with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
*/
package org.jackhuang.hmcl.util.platform;

import net.burningtnt.bcigenerator.api.BytecodeImpl;
import net.burningtnt.bcigenerator.api.BytecodeImplError;
import org.jackhuang.hmcl.launch.StreamPump;
import org.jackhuang.hmcl.util.Lang;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
Expand Down Expand Up @@ -92,13 +92,7 @@ public Process getProcess() {
public long getPID() throws UnsupportedOperationException {
if (JavaVersion.CURRENT_JAVA.getParsedVersion() >= 9) {
// Method Process.pid() is provided (Java 9 or later). Invoke it to get the pid.
try {
// Current Java Compilation Environment is Java 8, directly invoking process.pid() will cause an error.
Method pidMethod = Process.class.getMethod("pid");
return (long) pidMethod.invoke(process);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new UnsupportedOperationException("Cannot get the pid of a Process on Java 9.", e);
}
return getPID0(process);
} else {
// Method Process.pid() is not provided. (Java 8).
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
Expand All @@ -124,6 +118,23 @@ public long getPID() throws UnsupportedOperationException {
}
}

/**
* Get the PID of a process with BytecodeImplGenerator
*/
@BytecodeImpl({
"LABEL METHOD_HEAD",
"ALOAD 0",
"INVOKEVIRTUAL Ljava/lang/Process;pid()J",
"LABEL RELEASE_PARAMETER",
"LRETURN",
"LOCALVARIABLE process [Ljava/lang/Process; METHOD_HEAD RELEASE_PARAMETER 0",
"MAXS 2 1"
})
@SuppressWarnings("unused")
private static long getPID0(Process process) {
throw new BytecodeImplError();
}

/**
* The command line.
*
Expand Down

0 comments on commit 7cc5e7d

Please sign in to comment.