Skip to content

Commit 96f68e1

Browse files
authored
不再使用反射调用 Java 9~11 中引入的 API (#4114)
1 parent 24d3bbf commit 96f68e1

File tree

23 files changed

+60
-350
lines changed

23 files changed

+60
-350
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/EntryPoint.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
import org.jackhuang.hmcl.util.FileSaver;
2121
import org.jackhuang.hmcl.ui.AwtUtils;
22-
import org.jackhuang.hmcl.util.ModuleHelper;
2322
import org.jackhuang.hmcl.util.SelfDependencyPatcher;
2423
import org.jackhuang.hmcl.util.SwingUtils;
2524
import org.jackhuang.hmcl.java.JavaRuntime;
2625
import org.jackhuang.hmcl.util.platform.OperatingSystem;
2726

2827
import java.io.IOException;
29-
import java.lang.reflect.Method;
28+
import java.lang.invoke.MethodHandle;
29+
import java.lang.invoke.MethodHandles;
30+
import java.lang.invoke.MethodType;
3031
import java.nio.file.Files;
3132
import java.util.concurrent.CancellationException;
3233

@@ -140,7 +141,18 @@ private static void verifyJavaFX() {
140141
private static void addEnableNativeAccess() {
141142
if (JavaRuntime.CURRENT_VERSION > 21) {
142143
try {
143-
ModuleHelper.addEnableNativeAccess(Class.forName("javafx.stage.Stage")); // javafx.graphics
144+
// javafx.graphics
145+
Module module = Class.forName("javafx.stage.Stage").getModule();
146+
if (module.isNamed()) {
147+
try {
148+
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(Module.class, MethodHandles.lookup());
149+
MethodHandle implAddEnableNativeAccess = lookup.findVirtual(Module.class,
150+
"implAddEnableNativeAccess", MethodType.methodType(Module.class));
151+
Module ignored = (Module) implAddEnableNativeAccess.invokeExact(module);
152+
} catch (Throwable e) {
153+
e.printStackTrace(System.err);
154+
}
155+
}
144156
} catch (ClassNotFoundException e) {
145157
LOG.error("Failed to add enable native access for JavaFX", e);
146158
showErrorAndExit(i18n("fatal.javafx.incomplete"));
@@ -153,9 +165,9 @@ private static void enableUnsafeMemoryAccess() {
153165
if (JavaRuntime.CURRENT_VERSION == 24 || JavaRuntime.CURRENT_VERSION == 25) {
154166
try {
155167
Class<?> clazz = Class.forName("sun.misc.Unsafe");
156-
Method trySetMemoryAccessWarned = clazz.getDeclaredMethod("trySetMemoryAccessWarned");
157-
trySetMemoryAccessWarned.setAccessible(true);
158-
trySetMemoryAccessWarned.invoke(null);
168+
boolean ignored = (boolean) MethodHandles.privateLookupIn(clazz, MethodHandles.lookup())
169+
.findStatic(clazz, "trySetMemoryAccessWarned", MethodType.methodType(boolean.class))
170+
.invokeExact();
159171
} catch (Throwable e) {
160172
LOG.warning("Failed to enable unsafe memory access", e);
161173
}

HMCL/src/main/java/org/jackhuang/hmcl/setting/FontManager.java

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@
2121
import javafx.beans.property.SimpleObjectProperty;
2222
import javafx.scene.text.Font;
2323
import org.jackhuang.hmcl.Metadata;
24-
import org.jackhuang.hmcl.java.JavaRuntime;
2524
import org.jackhuang.hmcl.util.Lazy;
2625
import org.jackhuang.hmcl.util.io.JarUtils;
2726
import org.jackhuang.hmcl.util.platform.OperatingSystem;
2827
import org.jackhuang.hmcl.util.platform.SystemUtils;
2928
import org.jetbrains.annotations.NotNull;
3029
import org.jetbrains.annotations.Nullable;
3130

32-
import java.lang.invoke.MethodHandle;
33-
import java.lang.invoke.MethodHandles;
34-
import java.lang.invoke.MethodType;
3531
import java.net.MalformedURLException;
3632
import java.nio.file.Files;
3733
import java.nio.file.Path;
@@ -152,46 +148,33 @@ public static Font findByFcMatch() {
152148
return null;
153149
}
154150

155-
if (JavaRuntime.CURRENT_VERSION >= 9) {
156-
try {
157-
MethodHandle methodHandle = MethodHandles.publicLookup().findStatic(Font.class, "loadFonts",
158-
MethodType.methodType(Font[].class, String.class, double.class));
159-
160-
Font[] fonts = (Font[]) methodHandle.invokeExact(file.toUri().toURL().toExternalForm(), DEFAULT_FONT_SIZE);
161-
if (fonts == null) {
162-
LOG.warning("Failed to load font from " + path);
163-
return null;
164-
} else if (fonts.length == 0) {
165-
LOG.warning("No fonts loaded from " + path);
166-
return null;
167-
}
151+
Font[] fonts = Font.loadFonts(file.toUri().toURL().toExternalForm(), DEFAULT_FONT_SIZE);
152+
if (fonts == null) {
153+
LOG.warning("Failed to load font from " + path);
154+
return null;
155+
} else if (fonts.length == 0) {
156+
LOG.warning("No fonts loaded from " + path);
157+
return null;
158+
}
168159

160+
for (Font font : fonts) {
161+
if (font.getFamily().equalsIgnoreCase(family)) {
162+
return font;
163+
}
164+
}
165+
166+
if (family.indexOf(',') >= 0) {
167+
for (String candidateFamily : family.split(",")) {
169168
for (Font font : fonts) {
170-
if (font.getFamily().equalsIgnoreCase(family)) {
169+
if (font.getFamily().equalsIgnoreCase(candidateFamily)) {
171170
return font;
172171
}
173172
}
174-
175-
if (family.indexOf(',') >= 0) {
176-
for (String candidateFamily : family.split(",")) {
177-
for (Font font : fonts) {
178-
if (font.getFamily().equalsIgnoreCase(candidateFamily)) {
179-
return font;
180-
}
181-
}
182-
}
183-
}
184-
185-
LOG.warning(String.format("Family '%s' not found in font file '%s'", family, path));
186-
return fonts[0];
187-
} catch (NoSuchMethodException | IllegalAccessException ignored) {
188173
}
189174
}
190175

191-
Font font = Font.loadFont(file.toUri().toURL().toExternalForm(), DEFAULT_FONT_SIZE);
192-
if (font == null)
193-
LOG.warning("Failed to load font from " + path);
194-
return font;
176+
LOG.warning(String.format("Family '%s' not found in font file '%s'", family, path));
177+
return fonts[0];
195178
} catch (Throwable e) {
196179
LOG.warning("Failed to get default font with fc-match", e);
197180
return null;

HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,23 @@ public static void smoothScrolling(ScrollPane scrollPane) {
328328
private static final Duration TOOLTIP_SLOW_SHOW_DELAY = Duration.millis(500);
329329
private static final Duration TOOLTIP_SHOW_DURATION = Duration.millis(5000);
330330

331+
public static void installTooltip(Node node, Duration showDelay, Duration showDuration, Duration hideDelay, Tooltip tooltip) {
332+
tooltip.setShowDelay(showDelay);
333+
tooltip.setShowDuration(showDuration);
334+
tooltip.setHideDelay(hideDelay);
335+
Tooltip.install(node, tooltip);
336+
}
337+
331338
public static void installFastTooltip(Node node, Tooltip tooltip) {
332-
runInFX(() -> TooltipInstaller.INSTALLER.installTooltip(node, TOOLTIP_FAST_SHOW_DELAY, TOOLTIP_SHOW_DURATION, Duration.ZERO, tooltip));
339+
runInFX(() -> installTooltip(node, TOOLTIP_FAST_SHOW_DELAY, TOOLTIP_SHOW_DURATION, Duration.ZERO, tooltip));
333340
}
334341

335342
public static void installFastTooltip(Node node, String tooltip) {
336343
installFastTooltip(node, new Tooltip(tooltip));
337344
}
338345

339346
public static void installSlowTooltip(Node node, Tooltip tooltip) {
340-
runInFX(() -> TooltipInstaller.INSTALLER.installTooltip(node, TOOLTIP_SLOW_SHOW_DELAY, TOOLTIP_SHOW_DURATION, Duration.ZERO, tooltip));
347+
runInFX(() -> installTooltip(node, TOOLTIP_SLOW_SHOW_DELAY, TOOLTIP_SHOW_DURATION, Duration.ZERO, tooltip));
341348
}
342349

343350
public static void installSlowTooltip(Node node, String tooltip) {

HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private void onExportDump(SpinnerPane pane) {
224224

225225
try {
226226
if (gameProcess.isRunning()) {
227-
GameDumpGenerator.writeDumpTo(gameProcess.getPID(), dumpFile);
227+
GameDumpGenerator.writeDumpTo(gameProcess.getProcess().pid(), dumpFile);
228228
FXUtils.showFileInExplorer(dumpFile);
229229
}
230230
} catch (Throwable e) {

HMCL/src/main/java/org/jackhuang/hmcl/ui/TooltipInstaller.java

Lines changed: 0 additions & 123 deletions
This file was deleted.

HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.jackhuang.hmcl.ui.export.ExportWizardProvider;
4444
import org.jackhuang.hmcl.util.StringUtils;
4545
import org.jackhuang.hmcl.util.TaskCancellationAction;
46-
import org.jackhuang.hmcl.util.io.FileUtils;
4746
import org.jackhuang.hmcl.util.platform.OperatingSystem;
4847

4948
import java.io.File;
@@ -110,10 +109,8 @@ public static void downloadModpackImpl(Profile profile, String version, RemoteMo
110109

111110
public static void deleteVersion(Profile profile, String version) {
112111
boolean isIndependent = profile.getVersionSetting(version).getGameDirType() == GameDirectoryType.VERSION_FOLDER;
113-
boolean isMovingToTrashSupported = FileUtils.isMovingToTrashSupported();
114112
String message = isIndependent ? i18n("version.manage.remove.confirm.independent", version) :
115-
isMovingToTrashSupported ? i18n("version.manage.remove.confirm.trash", version, version + "_removed") :
116-
i18n("version.manage.remove.confirm", version);
113+
i18n("version.manage.remove.confirm.trash", version, version + "_removed");
117114

118115
JFXButton deleteButton = new JFXButton(i18n("button.delete"));
119116
deleteButton.getStyleClass().add("dialog-error");

HMCL/src/main/java/org/jackhuang/hmcl/util/ModuleHelper.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

HMCL/src/main/resources/assets/lang/I18N.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,6 @@ version.manage.manage=Edit Instance
14331433
version.manage.manage.title=Edit Instance - %1s
14341434
version.manage.redownload_assets_index=Update Game Assets
14351435
version.manage.remove=Delete Instance
1436-
version.manage.remove.confirm=Are you sure you want to permanently remove the instance "%s"? This operation cannot be undone!!!
14371436
version.manage.remove.confirm.trash=Are you sure you want to remove the instance "%s"? You can still find its files in your recycle bin by the name of "%s".
14381437
version.manage.remove.confirm.independent=Since this instance is stored in an isolated directory, deleting it will also delete its saves and other data. Do you still want to delete the instance "%s"?
14391438
version.manage.remove_assets=Delete All Assets

HMCL/src/main/resources/assets/lang/I18N_es.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,6 @@ version.manage.manage=Editar instancia
14061406
version.manage.manage.title=Editar instancia - %1s
14071407
version.manage.redownload_assets_index=Actualizar activos del juego
14081408
version.manage.remove=Eliminar instancia
1409-
version.manage.remove.confirm=¿Está seguro de que quiere eliminar permanentemente la instancia «%s»? ¡Esta operación no se puede deshacer!
14101409
version.manage.remove.confirm.trash=¿Estás seguro de que quieres eliminar la instancia «%s»? Todavía puedes encontrar sus archivos en tu papelera de reciclaje con el nombre de «%s».
14111410
version.manage.remove.confirm.independent=Dado que esta instancia está almacenada en un directorio aislado, al eliminarla también se eliminarán sus guardados y otros datos. ¿Aún quieres borrar la instancia %s?
14121411
version.manage.remove_assets=Borrar todas las activos del juego

HMCL/src/main/resources/assets/lang/I18N_ja.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,6 @@ version.manage.manage=バージョンの管理
949949
version.manage.manage.title=バージョンの管理 - %1s
950950
version.manage.redownload_assets_index=ゲームアセットファイルの更新
951951
version.manage.remove=このバージョンを削除します
952-
version.manage.remove.confirm=このバージョン %s を削除してもよろしいですか?このバージョンを再度復元することはできません。
953952
version.manage.remove.confirm.trash=このバージョン %s を削除してもよろしいですか?このバージョンは、システムのゴミ箱に %s という名前で復元できます。
954953
version.manage.remove.confirm.independent=このバージョンは独立モードであるため、このバージョンを削除すると、このバージョンに属するすべての保存済みワールドも削除されます。このバージョン %s を削除しますか?
955954
version.manage.remove_assets=すべてのゲームリソースファイルの削除

0 commit comments

Comments
 (0)