[util] use reflection to access internal Unsafe class in DebugReflectionUtil
authorNikolay Chashnikov <Nikolay.Chashnikov@jetbrains.com>
Wed, 12 Aug 2020 16:56:42 +0000 (19:56 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Wed, 12 Aug 2020 18:36:00 +0000 (18:36 +0000)
This is needed to be able to compile intellij.platform.util module using JDK 11 (IDEA-248086).

GitOrigin-RevId: ca5268de89db3e2c782cc93a35b02f35ed5875a8

platform/util/src/com/intellij/util/ref/DebugReflectionUtil.java

index 8bfc7a71e997e26d3f82ac52b999da73c961883a..d53826383b76f3f630d7708c36b0553b2a8685ea 100644 (file)
@@ -15,7 +15,6 @@ import it.unimi.dsi.fastutil.ints.IntSet;
 import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
-import sun.misc.Unsafe;
 
 import java.lang.ref.Reference;
 import java.lang.reflect.Field;
@@ -39,7 +38,18 @@ public final class DebugReflectionUtil {
   });
 
   private static final Field[] EMPTY_FIELD_ARRAY = new Field[0];
-  private static final Method Unsafe_shouldBeInitialized = ReflectionUtil.getDeclaredMethod(Unsafe.class, "shouldBeInitialized", Class.class);
+  private static final Method Unsafe_shouldBeInitialized;
+
+  static {
+    Method shouldBeInitialized;
+    try {
+      shouldBeInitialized = ReflectionUtil.getDeclaredMethod(Class.forName("sun.misc.Unsafe"), "shouldBeInitialized", Class.class);
+    }
+    catch (ClassNotFoundException ignored) {
+      shouldBeInitialized = null;
+    }
+    Unsafe_shouldBeInitialized = shouldBeInitialized;
+  }
 
   private static Field @NotNull [] getAllFields(@NotNull Class<?> aClass) {
     Field[] cached = allFields.get(aClass);