I'm now using unsafe. When I run the following code:
unsafe.allocateInstance(Class.class)
There happen's
Exception in thread "main" java.lang.IllegalAccessException: java.lang.Class
Since Class
is a non-abstract class, why it so special? And is there any way to construct an 'empty' Class
like allocateInstance
?
Because there is an explicit check inside HotSpot JVM to ensure that
java.lang.Class
cannot be instantiated through JNI, Unsafe etc. See instanceKlass.cpp:Such instance would not be valid anyway, so it does not make sense.