Java通过反射实例化sun.misc.Unsafe
示例
public static Unsafe getUnsafe() {
try {
Field unsafe = Unsafe.class.getDeclaredField("theUnsafe");
unsafe.setAccessible(true);
return (Unsafe) unsafe.get(null);
} catch (IllegalAccessException e) {
//处理
} catch (IllegalArgumentException e) {
//处理
} catch (NoSuchFieldException e) {
//处理
} catch (SecurityException e) {
//处理
}
}sun.misc.Unsafe具有Private构造函数,并且通过getUnsafe()检查类加载器来保护static方法,以确保使用主类加载器加载代码。因此,一种加载实例的方法是使用反射来获取静态字段。