Java tutorial
//package com.java2s; import sun.misc.Unsafe; import java.lang.reflect.Field; import java.security.AccessController; import java.security.PrivilegedExceptionAction; public class Main { /** * Gets the {@code sun.misc.Unsafe} instance, or {@code null} if not available on this platform. */ private static sun.misc.Unsafe getUnsafe() { sun.misc.Unsafe unsafe = null; try { unsafe = AccessController.doPrivileged(new PrivilegedExceptionAction<Unsafe>() { @Override public sun.misc.Unsafe run() throws Exception { Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; for (Field f : k.getDeclaredFields()) { f.setAccessible(true); Object x = f.get(null); if (k.isInstance(x)) { return k.cast(x); } } // The sun.misc.Unsafe field does not exist. return null; } }); } catch (Throwable e) { // Catching Throwable here due to the fact that Google AppEngine raises NoClassDefFoundError // for Unsafe. } return unsafe; } }