清理 ManagedProcess::getPid() (#2660)

* Delete getPID0

* Delete blank line
This commit is contained in:
Glavo
2024-01-20 10:57:46 +08:00
committed by GitHub
parent 96270b1706
commit 59a9b3a926
2 changed files with 9 additions and 36 deletions

View File

@@ -17,12 +17,12 @@
*/
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.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Consumer;
@@ -92,7 +92,13 @@ public class ManagedProcess {
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.
return getPID0(process);
try {
return (long) MethodHandles.publicLookup()
.findVirtual(Process.class, "pid", MethodType.methodType(long.class))
.invokeExact(process);
} catch (Throwable e) {
throw new UnsupportedOperationException("Cannot get the pid", e);
}
} else {
// Method Process.pid() is not provided. (Java 8).
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
@@ -118,23 +124,6 @@ public class ManagedProcess {
}
}
/**
* 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.
*