From fb9a6fd3149c93263a0b1cc2155ac706cfa05761 Mon Sep 17 00:00:00 2001 From: Glavo Date: Fri, 22 Oct 2021 09:11:26 +0800 Subject: [PATCH] Lazy initialization CrashReporter --- .../jackhuang/hmcl/util/CrashReporter.java | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/CrashReporter.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/CrashReporter.java index 38929d6ee..8a7e56f79 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/CrashReporter.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/CrashReporter.java @@ -32,13 +32,13 @@ import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; -import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import static java.util.Collections.newSetFromMap; import static org.jackhuang.hmcl.util.Logging.LOG; +import static org.jackhuang.hmcl.util.Pair.pair; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; /** @@ -46,28 +46,32 @@ import static org.jackhuang.hmcl.util.i18n.I18n.i18n; */ public class CrashReporter implements Thread.UncaughtExceptionHandler { - private static final Map SOURCE = new HashMap() { - { - put("javafx.fxml.LoadException", i18n("crash.NoClassDefFound")); - put("Location is not set", i18n("crash.NoClassDefFound")); - put("UnsatisfiedLinkError", i18n("crash.user_fault")); - put("java.lang.NoClassDefFoundError", i18n("crash.NoClassDefFound")); - put("org.jackhuang.hmcl.util.ResourceNotFoundError", i18n("crash.NoClassDefFound")); - put("java.lang.VerifyError", i18n("crash.NoClassDefFound")); - put("java.lang.NoSuchMethodError", i18n("crash.NoClassDefFound")); - put("java.lang.NoSuchFieldError", i18n("crash.NoClassDefFound")); - put("javax.imageio.IIOException", i18n("crash.NoClassDefFound")); - put("netscape.javascript.JSException", i18n("crash.NoClassDefFound")); - put("java.lang.IncompatibleClassChangeError", i18n("crash.NoClassDefFound")); - put("java.lang.ClassFormatError", i18n("crash.NoClassDefFound")); - put("com.sun.javafx.css.StyleManager.findMatchingStyles", i18n("launcher.update_java")); - put("NoSuchAlgorithmException", "Has your operating system been installed completely or is a ghost system?"); - } - }; + // Lazy initialization resources + private static final class Hole { + @SuppressWarnings("unchecked") + static final Pair[] SOURCE = (Pair[]) new Pair[]{ + pair("javafx.fxml.LoadException", i18n("crash.NoClassDefFound")), + pair("Location is not set", i18n("crash.NoClassDefFound")), + pair("UnsatisfiedLinkError", i18n("crash.user_fault")), + pair("java.lang.NoClassDefFoundError", i18n("crash.NoClassDefFound")), + pair("org.jackhuang.hmcl.util.ResourceNotFoundError", i18n("crash.NoClassDefFound")), + pair("java.lang.VerifyError", i18n("crash.NoClassDefFound")), + pair("java.lang.NoSuchMethodError", i18n("crash.NoClassDefFound")), + pair("java.lang.NoSuchFieldError", i18n("crash.NoClassDefFound")), + pair("javax.imageio.IIOException", i18n("crash.NoClassDefFound")), + pair("netscape.javascript.JSException", i18n("crash.NoClassDefFound")), + pair("java.lang.IncompatibleClassChangeError", i18n("crash.NoClassDefFound")), + pair("java.lang.ClassFormatError", i18n("crash.NoClassDefFound")), + pair("com.sun.javafx.css.StyleManager.findMatchingStyles", i18n("launcher.update_java")), + pair("NoSuchAlgorithmException", "Has your operating system been installed completely or is a ghost system?") + }; + + static final Set CAUGHT_EXCEPTIONS = newSetFromMap(new ConcurrentHashMap<>()); + } private boolean checkThrowable(Throwable e) { String s = StringUtils.getStackTrace(e); - for (HashMap.Entry entry : SOURCE.entrySet()) + for (Pair entry : Hole.SOURCE) if (s.contains(entry.getKey())) { if (StringUtils.isNotBlank(entry.getValue())) { String info = entry.getValue(); @@ -86,8 +90,6 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler { return true; } - private static Set CAUGHT_EXCEPTIONS = newSetFromMap(new ConcurrentHashMap<>()); - private final boolean showCrashWindow; public CrashReporter(boolean showCrashWindow) { @@ -103,9 +105,9 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler { if (!stackTrace.contains("org.jackhuang")) return; - if (CAUGHT_EXCEPTIONS.contains(stackTrace)) + if (Hole.CAUGHT_EXCEPTIONS.contains(stackTrace)) return; - CAUGHT_EXCEPTIONS.add(stackTrace); + Hole.CAUGHT_EXCEPTIONS.add(stackTrace); String text = "---- Hello Minecraft! Crash Report ----\n" + " Version: " + Metadata.VERSION + "\n" +