Switching to semi-dynamic discovery of sun.misc.Unsafe entity

This commit is contained in:
Viktor Klang 2012-01-19 16:35:36 +01:00
parent 19347dadbc
commit 69196a998f

View file

@ -11,9 +11,16 @@ public final class Unsafe {
public final static sun.misc.Unsafe instance;
static {
try {
Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
instance = (sun.misc.Unsafe) field.get(null);
sun.misc.Unsafe found = null;
for(Field field : sun.misc.Unsafe.class.getDeclaredFields()) {
if (field.getType() == sun.misc.Unsafe.class) {
field.setAccessible(true);
found = (sun.misc.Unsafe) field.get(null);
break;
}
}
if (found == null) throw new IllegalStateException("Can't find instance of sun.misc.Unsafe");
else instance = found;
} catch(Throwable t) {
throw new ExceptionInInitializerError(t);
}