Initialize fieldCache and methodCache before unsafe instance in ReflectionUtil

This commit is contained in:
Marc Baloup 2021-12-27 23:56:21 +01:00
parent a82820cc15
commit 2a6654746e

View File

@ -22,16 +22,25 @@ import io.github.classgraph.ScanResult;
import sun.misc.Unsafe;
public class ReflectionUtil {
private record MethodCacheKey(Class<?> clazz, String methodName, List<Class<?>> parameters) { }
private static final Map<MethodCacheKey, Method> methodCache;
private record FieldCacheKey(Class<?> clazz, String fieldName) { };
private static final Map<FieldCacheKey, Field> fieldCache;
private static final Unsafe sunMiscUnsafeInstance;
static {
methodCache = new HashMap<>();
fieldCache = new HashMap<>();
try {
sunMiscUnsafeInstance = (Unsafe) field("theUnsafe")
.inClass(Unsafe.class)
.getStaticValue();
} catch (Exception e) {
throw new RuntimeException(e);
throw new RuntimeException("Cannot access to " + Unsafe.class + ".theUnsafe value.", e);
}
}
@ -40,12 +49,6 @@ public class ReflectionUtil {
private record MethodCacheKey(Class<?> clazz, String methodName, List<Class<?>> parameters) { }
private static final Map<MethodCacheKey, Method> methodCache = new HashMap<>();
private record FieldCacheKey(Class<?> clazz, String fieldName) { };
private static final Map<FieldCacheKey, Field> fieldCache = new HashMap<>();