Fix NullPointerException in SwingUtils.invokeAndWait

This commit is contained in:
huangyuhui
2016-01-01 10:51:44 +08:00
parent 6968809499
commit 1f7eb04215
6 changed files with 51 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
*
*
* 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
@@ -87,6 +87,13 @@ public final class StrUtils {
return false;
}
public static boolean containsOne(String base, String... match) {
for (String s : match)
if (base.contains(s))
return true;
return false;
}
public static boolean containsOne(List<String> base, List<String> match) {
for (String a : base)
for (String b : match)

View File

@@ -26,6 +26,7 @@ import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.util.StringTokenizer;
import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.utils.StrUtils;
/**
* @author huangyuhui
@@ -108,4 +109,18 @@ public enum OS {
return result;
}
public static String getLinuxReleaseVersion() throws IOException {
return FileUtils.readFileToString(new File("/etc/issue"));
}
public static String getSystemVersion() {
if (os() == LINUX)
try {
return getLinuxReleaseVersion();
} catch (IOException e) {
HMCLog.warn("Failed to catch /etc/issue");
}
return System.getProperty("os.name") + " (" + System.getProperty("os.arch") + "), " + System.getProperty("os.version");
}
}

View File

@@ -22,6 +22,9 @@ import java.awt.FontMetrics;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
@@ -222,12 +225,19 @@ public class SwingUtils {
return builder.toString();
}
private static final ThreadLocal<Object> THREAD_LOCAL = new ThreadLocal<>();
private static final Map<Integer, Object> INVOKE_AND_WAIT_MAP = Collections.synchronizedMap(new HashMap<>());
private static int INVOKE_AND_WAIT_ID = 0;
private static final Object INVOKE_AND_WAIT_LOCK = new Object();
public static <T> T invokeAndWait(NonFunction<T> x) {
Runnable r = () -> THREAD_LOCAL.set(x.apply());
int id;
synchronized (INVOKE_AND_WAIT_LOCK) {
id = ++INVOKE_AND_WAIT_ID;
}
int fuck = id;
Runnable r = () -> INVOKE_AND_WAIT_MAP.put(fuck, x.apply());
invokeAndWait(r);
return (T) THREAD_LOCAL.get();
return (T) INVOKE_AND_WAIT_MAP.remove(id);
}
public static void invokeAndWait(Runnable r) {