java - Why Unsafe.allocateInstance(Class.class) failed? -


i'm using unsafe. when run following code:

unsafe.allocateinstance(class.class) 

there happen's

exception in thread "main" java.lang.illegalaccessexception: java.lang.class 

since class non-abstract class, why special? , there way construct 'empty' class allocateinstance?

because there explicit check inside hotspot jvm ensure java.lang.class cannot instantiated through jni, unsafe etc. see instanceklass.cpp:

void instanceklass::check_valid_for_instantiation(bool throwerror, traps) {   if (is_interface() || is_abstract()) {     resourcemark rm(thread);     throw_msg(throwerror ? vmsymbols::java_lang_instantiationerror()               : vmsymbols::java_lang_instantiationexception(), external_name());   }   if (this == systemdictionary::class_klass()) {     resourcemark rm(thread);     throw_msg(throwerror ? vmsymbols::java_lang_illegalaccesserror()               : vmsymbols::java_lang_illegalaccessexception(), external_name());   } } 

such instance not valid anyway, not make sense.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -