import sun.misc.Unsafe;
import java.lang.reflect.Field;
/**
* @author chunyang.leng
* @date 2021-07-15 5:19 下午
*/
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可以突破很多限制。。。。。。。构造函数不一定必须执行