Lazy initialization CrashReporter

This commit is contained in:
Glavo 2021-10-22 09:11:26 +08:00 committed by Yuhui Huang
parent 5cab8ad41e
commit fb9a6fd314

View File

@ -32,13 +32,13 @@ import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
import static java.util.Collections.newSetFromMap; import static java.util.Collections.newSetFromMap;
import static org.jackhuang.hmcl.util.Logging.LOG; 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; 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 { public class CrashReporter implements Thread.UncaughtExceptionHandler {
private static final Map<String, String> SOURCE = new HashMap<String, String>() { // Lazy initialization resources
{ private static final class Hole {
put("javafx.fxml.LoadException", i18n("crash.NoClassDefFound")); @SuppressWarnings("unchecked")
put("Location is not set", i18n("crash.NoClassDefFound")); static final Pair<String, String>[] SOURCE = (Pair<String, String>[]) new Pair<?, ?>[]{
put("UnsatisfiedLinkError", i18n("crash.user_fault")); pair("javafx.fxml.LoadException", i18n("crash.NoClassDefFound")),
put("java.lang.NoClassDefFoundError", i18n("crash.NoClassDefFound")); pair("Location is not set", i18n("crash.NoClassDefFound")),
put("org.jackhuang.hmcl.util.ResourceNotFoundError", i18n("crash.NoClassDefFound")); pair("UnsatisfiedLinkError", i18n("crash.user_fault")),
put("java.lang.VerifyError", i18n("crash.NoClassDefFound")); pair("java.lang.NoClassDefFoundError", i18n("crash.NoClassDefFound")),
put("java.lang.NoSuchMethodError", i18n("crash.NoClassDefFound")); pair("org.jackhuang.hmcl.util.ResourceNotFoundError", i18n("crash.NoClassDefFound")),
put("java.lang.NoSuchFieldError", i18n("crash.NoClassDefFound")); pair("java.lang.VerifyError", i18n("crash.NoClassDefFound")),
put("javax.imageio.IIOException", i18n("crash.NoClassDefFound")); pair("java.lang.NoSuchMethodError", i18n("crash.NoClassDefFound")),
put("netscape.javascript.JSException", i18n("crash.NoClassDefFound")); pair("java.lang.NoSuchFieldError", i18n("crash.NoClassDefFound")),
put("java.lang.IncompatibleClassChangeError", i18n("crash.NoClassDefFound")); pair("javax.imageio.IIOException", i18n("crash.NoClassDefFound")),
put("java.lang.ClassFormatError", i18n("crash.NoClassDefFound")); pair("netscape.javascript.JSException", i18n("crash.NoClassDefFound")),
put("com.sun.javafx.css.StyleManager.findMatchingStyles", i18n("launcher.update_java")); pair("java.lang.IncompatibleClassChangeError", i18n("crash.NoClassDefFound")),
put("NoSuchAlgorithmException", "Has your operating system been installed completely or is a ghost system?"); 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<String> CAUGHT_EXCEPTIONS = newSetFromMap(new ConcurrentHashMap<>());
}
private boolean checkThrowable(Throwable e) { private boolean checkThrowable(Throwable e) {
String s = StringUtils.getStackTrace(e); String s = StringUtils.getStackTrace(e);
for (HashMap.Entry<String, String> entry : SOURCE.entrySet()) for (Pair<String, String> entry : Hole.SOURCE)
if (s.contains(entry.getKey())) { if (s.contains(entry.getKey())) {
if (StringUtils.isNotBlank(entry.getValue())) { if (StringUtils.isNotBlank(entry.getValue())) {
String info = entry.getValue(); String info = entry.getValue();
@ -86,8 +90,6 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
return true; return true;
} }
private static Set<String> CAUGHT_EXCEPTIONS = newSetFromMap(new ConcurrentHashMap<>());
private final boolean showCrashWindow; private final boolean showCrashWindow;
public CrashReporter(boolean showCrashWindow) { public CrashReporter(boolean showCrashWindow) {
@ -103,9 +105,9 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
if (!stackTrace.contains("org.jackhuang")) if (!stackTrace.contains("org.jackhuang"))
return; return;
if (CAUGHT_EXCEPTIONS.contains(stackTrace)) if (Hole.CAUGHT_EXCEPTIONS.contains(stackTrace))
return; return;
CAUGHT_EXCEPTIONS.add(stackTrace); Hole.CAUGHT_EXCEPTIONS.add(stackTrace);
String text = "---- Hello Minecraft! Crash Report ----\n" + String text = "---- Hello Minecraft! Crash Report ----\n" +
" Version: " + Metadata.VERSION + "\n" + " Version: " + Metadata.VERSION + "\n" +