creating object instance without invoking initializer

笑着哭i 提交于 2020-01-14 08:13:51

问题


I'm trying to generate bytecode wich will create object instance without code initialization logic. Actually I want to reproduce generateSerializationConstructor behavior.

    {
        mv = cw.visitMethod(ACC_PUBLIC, "newObjectInstance", "()Ljava/lang/Object;", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, classNameInternal);
        mv.visitInsn(DUP);
        classNameInternal = "java/lang/Object";
        mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>", "()V");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

unfortunally I got such error:

java.lang.VerifyError: (class: com/esotericsoftware/reflectasm/benchmark/ConstructorAccessBenchmark$SomeClass_ClassAccess_, method: newObjectInstance signature: ()Ljava/lang/Object;) Call to wrong initialization method


回答1:


JVM specification prohibits creation of objects without calling proper constructor. However, there are two tricky ways of doing this. Both of them are specific to OpenJDK / Oracle JDK and are not guaranteed to work on all Java implementations.

  1. Call sun.misc.Unsafe.allocateInstance() internal API to instantiate a class without calling a constructor.
  2. When generating a class in run-time, inherit your class from sun.reflect.MagicAccessorImpl. JVM will skip bytecode verification for such class thus allowing to have NEW bytecode without proper INVOKESPECIAL.

Both techniques are used in our custom serialization engine which can be found at https://github.com/odnoklassniki/one-nio/blob/master/src/one/nio/serial/



来源:https://stackoverflow.com/questions/22570413/creating-object-instance-without-invoking-initializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!