import sun.misc.Unsafe; import java.lang.reflect.Field; public class Test { public Test(){ System.out.println("exec"); } public void s(){ System.out.println("this s method"); } public static void main(String[] args) throws InstantiationException { Unsafe unsafe = reflectGetUnsafe(); Test test = (Test)unsafe.allocateInstance(Test.class); test.s(); } public static Unsafe reflectGetUnsafe() { try { Field field = Unsafe.class.getDeclaredField("theUnsafe"); field.setAccessible(true); return (Unsafe) field.get(null); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } return null; } }
执行结果
结论:unsafe可以突破很多限制。。。。。。。构造函数不一定必须执行
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)