Skip to content

Commit

Permalink
cache ReflectionUtil#getClass
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Jun 26, 2024
1 parent ace065d commit d1f2353
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/main/java/me/fallenbreath/tweakermore/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,33 @@

package me.fallenbreath.tweakermore.util;

import me.fallenbreath.tweakermore.TweakerMoreMod;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public class ReflectionUtil
{
private static final Map<String, Optional<Class<?>>> classCache = new ConcurrentHashMap<>();

public static Optional<Class<?>> getClass(String className)
{
try
{
return Optional.of(Class.forName(className));
}
catch (ClassNotFoundException e)
{
return Optional.empty();
}
return classCache.computeIfAbsent(className, k -> {
try
{
return Optional.of(Class.forName(className));
}
catch (ClassNotFoundException e)
{
TweakerMoreMod.LOGGER.debug("ReflectionUtil.getClass '{}' not found", className);
return Optional.empty();
}
});
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit d1f2353

Please sign in to comment.