MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC | ACC_STATIC, FACTORY_METHOD_NAME, Type.getMethodDescriptor(Type.getObjectType(classToInstantiateInternalName)), null, null); methodVisitor.visitCode(); methodVisitor.visitTypeInsn(NEW, classToInstantiateInternalName); if (invokeObjectConstructor) { methodVisitor.visitInsn(DUP); methodVisitor.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE), false); } methodVisitor.visitInsn(ARETURN); methodVisitor.visitMaxs(1, 0); methodVisitor.visitEnd();
Constructor<Object> objCons = Object.class.getConstructor(); Constructor<?> c = sun.reflect.ReflectionFactory.getReflectionFactory().newConstructorForSerialization(noConstructorClass, objCons); Object instance = c.newInstance(); logger.info("Instance: {}", instance);
public class sun.reflect.GeneratedSerializationConstructorAccessor1 extends sun.reflect.SerializationConstructorAccessorImpl { public sun.reflect.GeneratedSerializationConstructorAccessor1 (); Code: 0: aload_0 1: invokespecial # 36 // Method sun / reflect / SerializationConstructorAccessorImpl. "<Init>" :() V 4: return public java.lang.Object newInstance (java.lang.Object []) throws java.lang.reflect.InvocationTargetException; Code: 0: new # 6 // class com / example / test / classes / NoConstructor 3: dup 4: aload_1 5: ifnull 24 8: aload_1 9: arraylength 10: sipush 0 13: if_icmpeq 24 16: new # 22 // class java / lang / IllegalArgumentException 19: dup 20: invokespecial # 29 // Method java / lang / IllegalArgumentException. "<Init>" :() V 23: athrow 24: invokespecial # 12 // Method java / lang / Object. "<Init>" :() V 27: areturn 28: invokespecial # 42 // Method java / lang / Object.toString :() Ljava / lang / String; 31: new # 22 // class java / lang / IllegalArgumentException 34: dup_x1 35: swap 36: invokespecial # 32 // Method java / lang / IllegalArgumentException. "<Init>" :( Ljava / lang / String;) V 39: athrow 40: new # 24 // class java / lang / reflect / InvocationTargetException 43: dup_x1 44: swap 45: invokespecial # 35 // Method java / lang / reflect / InvocationTargetException. "<Init>" :( Ljava / lang / Throwable;) V 48: athrow Exception table: from to target type 0 24 28 Class java / lang / ClassCastException 0 24 28 Class java / lang / NullPointerException 24 27 40 Class java / lang / Throwable }
Java serialization (in java.io) is a concept of the first non-Serializable superclass. This is not a valid operation according to the VM specification; one can not (for A and B, where B is a subclass of A) write “new B; invokespecial A () »without getting verification error.
In all other respects, it can be reused for this purpose. It was not necessary for the bug fix for 4486457. Future debugging easier.
Source: https://habr.com/ru/post/252621/
All Articles