清理 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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 36 deletions

View File

@ -13,20 +13,4 @@ dependencies {
api("org.nanohttpd:nanohttpd:2.3.1") api("org.nanohttpd:nanohttpd:2.3.1")
api("org.apache.commons:commons-compress:1.25.0") api("org.apache.commons:commons-compress:1.25.0")
compileOnlyApi("org.jetbrains:annotations:24.1.0") compileOnlyApi("org.jetbrains:annotations:24.1.0")
compileOnlyApi("com.github.burningtnt:BytecodeImplGenerator:b45b6638eeaeb903aa22ea947d37c45e5716a18c")
}
tasks.getByName<JavaCompile>("compileJava") {
val bytecodeClasses = listOf(
"org/jackhuang/hmcl/util/platform/ManagedProcess"
)
doLast {
javaexec {
classpath(project.sourceSets["main"].compileClasspath)
mainClass.set("net.burningtnt.bcigenerator.BytecodeImplGenerator")
System.getProperty("bci.debug.address")?.let { address -> jvmArgs("-agentlib:jdwp=transport=dt_socket,server=n,address=$address,suspend=y") }
args(bytecodeClasses.map { s -> project.layout.buildDirectory.file("classes/java/main/$s.class").get().asFile.path })
}
}
} }

View File

@ -17,12 +17,12 @@
*/ */
package org.jackhuang.hmcl.util.platform; 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.launch.StreamPump;
import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Lang;
import java.io.IOException; import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.*;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -92,7 +92,13 @@ public class ManagedProcess {
public long getPID() throws UnsupportedOperationException { public long getPID() throws UnsupportedOperationException {
if (JavaVersion.CURRENT_JAVA.getParsedVersion() >= 9) { if (JavaVersion.CURRENT_JAVA.getParsedVersion() >= 9) {
// Method Process.pid() is provided (Java 9 or later). Invoke it to get the pid. // 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 { } else {
// Method Process.pid() is not provided. (Java 8). // Method Process.pid() is not provided. (Java 8).
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) { 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. * The command line.
* *