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; import sun.misc.Unsafe;
public class ReflectionUtil { 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; private static final Unsafe sunMiscUnsafeInstance;
static { static {
methodCache = new HashMap<>();
fieldCache = new HashMap<>();
try { try {
sunMiscUnsafeInstance = (Unsafe) field("theUnsafe") sunMiscUnsafeInstance = (Unsafe) field("theUnsafe")
.inClass(Unsafe.class) .inClass(Unsafe.class)
.getStaticValue(); .getStaticValue();
} catch (Exception e) { } 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<>();