diff --git a/HMCLCore/build.gradle.kts b/HMCLCore/build.gradle.kts index 1cd003f13..c94473b50 100644 --- a/HMCLCore/build.gradle.kts +++ b/HMCLCore/build.gradle.kts @@ -13,20 +13,4 @@ dependencies { api("org.nanohttpd:nanohttpd:2.3.1") api("org.apache.commons:commons-compress:1.25.0") compileOnlyApi("org.jetbrains:annotations:24.1.0") - compileOnlyApi("com.github.burningtnt:BytecodeImplGenerator:b45b6638eeaeb903aa22ea947d37c45e5716a18c") -} - -tasks.getByName("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 }) - } - } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java index d2a3fd537..49186ec3c 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java @@ -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. *