From e4c7b137f186d6a7f11493f13a18ee5733b7fdd4 Mon Sep 17 00:00:00 2001 From: Glavo Date: Thu, 20 Mar 2025 16:21:17 +0800 Subject: [PATCH] Add --enable-native-access for javafx.graphics (#3749) * Add --enable-native-access for javafx.graphics * fix checkstyle * update --- HMCL/build.gradle.kts | 5 +- .../main/java/org/jackhuang/hmcl/Main.java | 14 ++++++ .../org/jackhuang/hmcl/util/ModuleHelper.java | 31 +++++++++++++ .../org/jackhuang/hmcl/util/ModuleHelper.java | 46 +++++++++++++++++++ 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 HMCL/src/main/java/org/jackhuang/hmcl/util/ModuleHelper.java create mode 100644 HMCL/src/main/java11/org/jackhuang/hmcl/util/ModuleHelper.java diff --git a/HMCL/build.gradle.kts b/HMCL/build.gradle.kts index 7bc6f9369..a53d87750 100644 --- a/HMCL/build.gradle.kts +++ b/HMCL/build.gradle.kts @@ -144,8 +144,9 @@ tasks.getByName("sha "javafx.controls/com.sun.javafx.scene.control", "javafx.controls/com.sun.javafx.scene.control.behavior", "javafx.controls/javafx.scene.control.skin", - "jdk.attach/sun.tools.attach" - ).joinToString(" ") + "jdk.attach/sun.tools.attach", + ).joinToString(" "), + "Enable-Native-Access" to "ALL-UNNAMED" ) System.getenv("GITHUB_SHA")?.also { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/Main.java b/HMCL/src/main/java/org/jackhuang/hmcl/Main.java index ff39601f7..c1d9c0ad8 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/Main.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/Main.java @@ -20,6 +20,7 @@ package org.jackhuang.hmcl; import javafx.application.Platform; import javafx.scene.control.Alert; import org.jackhuang.hmcl.ui.AwtUtils; +import org.jackhuang.hmcl.util.ModuleHelper; import org.jackhuang.hmcl.util.SelfDependencyPatcher; import org.jackhuang.hmcl.ui.SwingUtils; import org.jackhuang.hmcl.java.JavaRuntime; @@ -69,6 +70,7 @@ public final class Main { checkJavaFX(); verifyJavaFX(); + addEnableNativeAccess(); Launcher.main(args); } @@ -116,10 +118,22 @@ public final class Main { Class.forName("javafx.stage.Stage"); // javafx.graphics Class.forName("javafx.scene.control.Skin"); // javafx.controls } catch (Exception e) { + e.printStackTrace(System.err); showErrorAndExit(i18n("fatal.javafx.incomplete")); } } + private static void addEnableNativeAccess() { + if (JavaRuntime.CURRENT_VERSION > 21) { + try { + ModuleHelper.addEnableNativeAccess(Class.forName("javafx.stage.Stage")); // javafx.graphics + } catch (ClassNotFoundException e) { + e.printStackTrace(System.err); + showErrorAndExit(i18n("fatal.javafx.incomplete")); + } + } + } + /** * Indicates that a fatal error has occurred, and that the application cannot start. */ diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/ModuleHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/ModuleHelper.java new file mode 100644 index 000000000..6df44a17c --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/ModuleHelper.java @@ -0,0 +1,31 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jackhuang.hmcl.util; + +/** + * @author Glavo + */ +public final class ModuleHelper { + + public static void addEnableNativeAccess(Class clazzInModule) { + // do nothing + } + + private ModuleHelper() { + } +} diff --git a/HMCL/src/main/java11/org/jackhuang/hmcl/util/ModuleHelper.java b/HMCL/src/main/java11/org/jackhuang/hmcl/util/ModuleHelper.java new file mode 100644 index 000000000..f578d11dc --- /dev/null +++ b/HMCL/src/main/java11/org/jackhuang/hmcl/util/ModuleHelper.java @@ -0,0 +1,46 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jackhuang.hmcl.util; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; + +/** + * @author Glavo + */ +public final class ModuleHelper { + + public static void addEnableNativeAccess(Class clazzInModule) { + Module module = clazzInModule.getModule(); + if (module.isNamed()) { + try { + MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(Module.class, MethodHandles.lookup()); + MethodHandle implAddEnableNativeAccess = lookup.findVirtual(Module.class, "implAddEnableNativeAccess", MethodType.methodType(Module.class)); + Module ignored = (Module) implAddEnableNativeAccess.invokeExact(module); + } catch (Throwable e) { + e.printStackTrace(System.err); + } + } else { + System.err.println("TODO: Add enable native access for anonymous modules is not yet supported"); + } + } + + private ModuleHelper() { + } +}