private void _createInstance(ClassWriter cw, Context context) { Constructor<?> defaultConstructor = context.beanInfo.defaultConstructor; if (!Modifier.isPublic(defaultConstructor.getModifiers())) { return; } MethodVisitor mw = new MethodWriter(cw, ACC_PUBLIC, "createInstance", "(L" + DefaultJSONParser + ";Ljava/lang/reflect/Type;)Ljava/lang/Object;", null, null); mw.visitTypeInsn(NEW, type(context.getInstClass())); mw.visitInsn(DUP); mw.visitMethodInsn(INVOKESPECIAL, type(context.getInstClass()), "<init>", "()V"); mw.visitInsn(ARETURN); mw.visitMaxs(3, 3); mw.visitEnd(); }
public ObjectDeserializer createJavaBeanDeserializer(ParserConfig config, JavaBeanInfo beanInfo) throws Exception { Class<?> clazz = beanInfo.clazz; if (clazz.isPrimitive()) { throw new IllegalArgumentException("not support type :" + clazz.getName()); } String className = "FastjsonASMDeserializer_" + seed.incrementAndGet() + "_" + clazz.getSimpleName(); String packageName = ASMDeserializerFactory.class.getPackage().getName(); String classNameType = packageName.replace('.', '/') + "/" + className; String classNameFull = packageName + "." + className; ClassWriter cw = new ClassWriter(); cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, classNameType, type(JavaBeanDeserializer.class), null); _init(cw, new Context(classNameType, config, beanInfo, 3)); _createInstance(cw, new Context(classNameType, config, beanInfo, 3)); _deserialze(cw, new Context(classNameType, config, beanInfo, 5)); _deserialzeArrayMapping(cw, new Context(classNameType, config, beanInfo, 4)); byte[] code = cw.toByteArray(); Class<?> deserClass = classLoader.defineClassPublic(classNameFull, code, 0, code.length); Constructor<?> constructor = deserClass.getConstructor(ParserConfig.class, JavaBeanInfo.class); Object instance = constructor.newInstance(config, beanInfo); return (ObjectDeserializer) instance; }
private void _createInstance(ClassWriter cw, Context context) { MethodVisitor mw = cw.visitMethod(1, "createInstance", "(Lcom/alibaba/fastjson/parser/DefaultJSONParser;Ljava/lang/reflect/Type;)Ljava/lang/Object;", null, null); mw.visitTypeInsn(187, ASMUtils.getType(context.getClazz())); mw.visitInsn(89); mw.visitMethodInsn(183, ASMUtils.getType(context.getClazz()), "<init>", "()V"); mw.visitInsn(176); mw.visitMaxs(3, 3); mw.visitEnd(); }
private void _createInstance(ClassWriter cw, Context context) { MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "createInstance" , "(Lcom/alibaba/fastjson/parser/DefaultJSONParser;Ljava/lang/reflect/Type;)Ljava/lang/Object;", null, null); mw.visitTypeInsn(NEW, getType(context.getClazz())); mw.visitInsn(DUP); mw.visitMethodInsn(INVOKESPECIAL, getType(context.getClazz()), "<init>", "()V"); mw.visitInsn(ARETURN); mw.visitMaxs(3, 3); mw.visitEnd(); }
public ObjectDeserializer createJavaBeanDeserializer(ParserConfig config, Class<?> clazz, Type type) throws Exception { if (clazz.isPrimitive()) { throw new IllegalArgumentException("not support type :" + clazz.getName()); } String className = getGenClassName(clazz); ClassWriter cw = new ClassWriter(); cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, getType(ASMJavaBeanDeserializer.class), null); DeserializeBeanInfo beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); _init(cw, new Context(className, config, beanInfo, 3)); _createInstance(cw, new Context(className, config, beanInfo, 3)); _deserialze(cw, new Context(className, config, beanInfo, 4)); _deserialzeArrayMapping(cw, new Context(className, config, beanInfo, 4)); byte[] code = cw.toByteArray(); // org.apache.commons.io.IOUtils.write(code, new java.io.FileOutputStream( // "/usr/alibaba/workspace-3.7/fastjson-asm/target/classes/" // + className + ".class")); Class<?> exampleClass = classLoader.defineClassPublic(className, code, 0, code.length); Constructor<?> constructor = exampleClass.getConstructor(ParserConfig.class, Class.class); Object instance = constructor.newInstance(config, clazz); return (ObjectDeserializer) instance; }
private void _createInstance(ClassWriter cw, Context context) { MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "createInstance", "(" + getDesc(DefaultJSONParser.class) + getDesc(Type.class) + ")Ljava/lang/Object;", null, null); mw.visitTypeInsn(NEW, getType(context.getClazz())); mw.visitInsn(DUP); mw.visitMethodInsn(INVOKESPECIAL, getType(context.getClazz()), "<init>", "()V"); mw.visitInsn(ARETURN); mw.visitMaxs(3, 3); mw.visitEnd(); }
public ObjectDeserializer createJavaBeanDeserializer(ParserConfig config, Class<?> clazz, Type type) throws Exception { Exception ex; Throwable th; if (clazz.isPrimitive()) { throw new IllegalArgumentException("not support type :" + clazz.getName()); } String className = getGenClassName(clazz); ClassWriter cw = new ClassWriter(); cw.visit(49, 33, className, "com/alibaba/fastjson/parser/deserializer/ASMJavaBeanDeserializer", null); DeserializeBeanInfo beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); _init(cw, new Context(className, config, beanInfo, 3)); _createInstance(cw, new Context(className, config, beanInfo, 3)); _deserialze(cw, new Context(className, config, beanInfo, 4)); _deserialzeArrayMapping(cw, new Context(className, config, beanInfo, 4)); byte[] code = cw.toByteArray(); if (JSON.DUMP_CLASS != null) { FileOutputStream fos = null; try { FileOutputStream fos2 = new FileOutputStream(JSON.DUMP_CLASS + File.separator + className + ".class"); try { fos2.write(code); if (fos2 != null) { fos2.close(); } } catch (Exception e) { ex = e; fos = fos2; try { System.err.println("FASTJSON dump class:" + className + "失败:" + ex.getMessage()); if (fos != null) { fos.close(); } return (ObjectDeserializer) this.classLoader.defineClassPublic(className, code, 0, code.length).getConstructor(new Class[]{ParserConfig.class, Class.class}).newInstance(new Object[]{config, clazz}); } catch (Throwable th2) { th = th2; if (fos != null) { fos.close(); } throw th; } } catch (Throwable th3) { th = th3; fos = fos2; if (fos != null) { fos.close(); } throw th; } } catch (Exception e2) { ex = e2; System.err.println("FASTJSON dump class:" + className + "失败:" + ex.getMessage()); if (fos != null) { fos.close(); } return (ObjectDeserializer) this.classLoader.defineClassPublic(className, code, 0, code.length).getConstructor(new Class[]{ParserConfig.class, Class.class}).newInstance(new Object[]{config, clazz}); } } return (ObjectDeserializer) this.classLoader.defineClassPublic(className, code, 0, code.length).getConstructor(new Class[]{ParserConfig.class, Class.class}).newInstance(new Object[]{config, clazz}); }
public FieldDeserializer createStringFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo) throws Exception { Class<?> superClass; int INVAKE_TYPE; Class<?> fieldClass = fieldInfo.getFieldClass(); Method method = fieldInfo.getMethod(); String className = getGenFieldDeserializer(clazz, fieldInfo); ClassWriter cw = new ClassWriter(); if (fieldClass == Integer.TYPE) { superClass = IntegerFieldDeserializer.class; } else if (fieldClass == Long.TYPE) { superClass = LongFieldDeserializer.class; } else { superClass = StringFieldDeserializer.class; } if (clazz.isInterface()) { INVAKE_TYPE = 185; } else { INVAKE_TYPE = 182; } cw.visit(49, 33, className, ASMUtils.getType(superClass), null); MethodVisitor mw = cw.visitMethod(1, "<init>", "(Lcom/alibaba/fastjson/parser/ParserConfig;Ljava/lang/Class;Lcom/alibaba/fastjson/util/FieldInfo;)V", null, null); mw.visitVarInsn(25, 0); mw.visitVarInsn(25, 1); mw.visitVarInsn(25, 2); mw.visitVarInsn(25, 3); mw.visitMethodInsn(183, ASMUtils.getType(superClass), "<init>", "(Lcom/alibaba/fastjson/parser/ParserConfig;Ljava/lang/Class;Lcom/alibaba/fastjson/util/FieldInfo;)V"); mw.visitInsn(177); mw.visitMaxs(4, 6); mw.visitEnd(); if (method != null) { if (fieldClass == Integer.TYPE) { mw = cw.visitMethod(1, "setValue", "(Ljava/lang/Object;I)V", null, null); mw.visitVarInsn(25, 1); mw.visitTypeInsn(192, ASMUtils.getType(method.getDeclaringClass())); mw.visitVarInsn(21, 2); mw.visitMethodInsn(INVAKE_TYPE, ASMUtils.getType(method.getDeclaringClass()), method.getName(), ASMUtils.getDesc(method)); mw.visitInsn(177); mw.visitMaxs(3, 3); mw.visitEnd(); } else if (fieldClass == Long.TYPE) { mw = cw.visitMethod(1, "setValue", "(Ljava/lang/Object;J)V", null, null); mw.visitVarInsn(25, 1); mw.visitTypeInsn(192, ASMUtils.getType(method.getDeclaringClass())); mw.visitVarInsn(22, 2); mw.visitMethodInsn(INVAKE_TYPE, ASMUtils.getType(method.getDeclaringClass()), method.getName(), ASMUtils.getDesc(method)); mw.visitInsn(177); mw.visitMaxs(3, 4); mw.visitEnd(); } else { mw = cw.visitMethod(1, "setValue", "(Ljava/lang/Object;Ljava/lang/Object;)V", null, null); mw.visitVarInsn(25, 1); mw.visitTypeInsn(192, ASMUtils.getType(method.getDeclaringClass())); mw.visitVarInsn(25, 2); mw.visitTypeInsn(192, ASMUtils.getType(fieldClass)); mw.visitMethodInsn(INVAKE_TYPE, ASMUtils.getType(method.getDeclaringClass()), method.getName(), ASMUtils.getDesc(method)); mw.visitInsn(177); mw.visitMaxs(3, 3); mw.visitEnd(); } } byte[] code = cw.toByteArray(); return (FieldDeserializer) this.classLoader.defineClassPublic(className, code, 0, code.length).getConstructor(new Class[]{ParserConfig.class, Class.class, FieldInfo.class}).newInstance(new Object[]{mapping, clazz, fieldInfo}); }