diff --git a/src/main/java/mchorse/metamorph/util/InvokeUtil.java b/src/main/java/mchorse/metamorph/util/InvokeUtil.java index f9783896..9f603ce7 100644 --- a/src/main/java/mchorse/metamorph/util/InvokeUtil.java +++ b/src/main/java/mchorse/metamorph/util/InvokeUtil.java @@ -9,23 +9,17 @@ public class InvokeUtil { - protected static final int MAX_ERRORS_PER_CLASS = 5; - protected static ThreadLocal, Integer>> classErrorCounts = new ThreadLocal<>(); - - protected static WeakHashMap, Integer> getClassBlacklist() - { - WeakHashMap, Integer> blacklist = classErrorCounts.get(); - if (blacklist == null) { - blacklist = new WeakHashMap<>(); - classErrorCounts.set(blacklist); - } - return blacklist; - } + private static final int MAX_ERRORS_PER_CLASS = 5; + private static Object classErrorCountLock = new Object(); + private static WeakHashMap, Integer> classErrorCounts = new WeakHashMap<>(); protected static boolean isClassBlacklisted(Class clazz) { - WeakHashMap, Integer> blacklist = getClassBlacklist(); - Integer count = blacklist.get(clazz); + Integer count; + synchronized(classErrorCountLock) + { + count = classErrorCounts.get(clazz); + } if (count == null) { count = 0; @@ -35,22 +29,25 @@ protected static boolean isClassBlacklisted(Class clazz) protected static void incrementClassErrors(Class clazz) { - WeakHashMap, Integer> blacklist = getClassBlacklist(); - Integer count = blacklist.get(clazz); - if (count == null) + Integer count; + synchronized(classErrorCountLock) { - count = 1; - } - else - { - ++count; + count = classErrorCounts.get(clazz); + if (count == null) + { + count = 1; + } + else + { + ++count; + } + classErrorCounts.put(clazz, count); } - blacklist.put(clazz, count); if (count == MAX_ERRORS_PER_CLASS) { Metamorph.LOGGER.error("Too many errors for class " + clazz.getName() + ". " + - "Class will be blacklisted from reflection on this thread."); + "Class will be blacklisted from reflection."); } }