Skip to content

Commit

Permalink
Make reflection class blacklist work across threads
Browse files Browse the repository at this point in the history
  • Loading branch information
asanetargoss committed Jan 12, 2021
1 parent 2c6c28c commit d07f8a3
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions src/main/java/mchorse/metamorph/util/InvokeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@

public class InvokeUtil
{
protected static final int MAX_ERRORS_PER_CLASS = 5;
protected static ThreadLocal<WeakHashMap<Class<?>, Integer>> classErrorCounts = new ThreadLocal<>();

protected static WeakHashMap<Class<?>, Integer> getClassBlacklist()
{
WeakHashMap<Class<?>, 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<Class<?>, Integer> classErrorCounts = new WeakHashMap<>();

protected static boolean isClassBlacklisted(Class<?> clazz)
{
WeakHashMap<Class<?>, Integer> blacklist = getClassBlacklist();
Integer count = blacklist.get(clazz);
Integer count;
synchronized(classErrorCountLock)
{
count = classErrorCounts.get(clazz);
}
if (count == null)
{
count = 0;
Expand All @@ -35,22 +29,25 @@ protected static boolean isClassBlacklisted(Class<?> clazz)

protected static void incrementClassErrors(Class<?> clazz)
{
WeakHashMap<Class<?>, 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.");
}
}

Expand Down

0 comments on commit d07f8a3

Please sign in to comment.