Search in sources :

Example 1 with FieldVisitor

use of com.alibaba.fastjson.asm.FieldVisitor in project uavstack by uavorg.

the class ASMDeserializerFactory method _init.

private void _init(ClassWriter cw, Context context) {
    for (int i = 0, size = context.getFieldInfoList().size(); i < size; ++i) {
        FieldInfo fieldInfo = context.getFieldInfoList().get(i);
        // public FieldVisitor visitField(final int access, final String name, final String desc, final String
        // signature, final Object value) {
        FieldVisitor fw = cw.visitField(ACC_PUBLIC, fieldInfo.getName() + "_asm_prefix__", "[C");
        fw.visitEnd();
    }
    for (int i = 0, size = context.getFieldInfoList().size(); i < size; ++i) {
        FieldInfo fieldInfo = context.getFieldInfoList().get(i);
        Class<?> fieldClass = fieldInfo.getFieldClass();
        if (fieldClass.isPrimitive()) {
            continue;
        }
        if (fieldClass.isEnum()) {
            continue;
        }
        if (Collection.class.isAssignableFrom(fieldClass)) {
            FieldVisitor fw = cw.visitField(ACC_PUBLIC, fieldInfo.getName() + "_asm_list_item_deser__", "Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;");
            fw.visitEnd();
        } else {
            FieldVisitor fw = cw.visitField(ACC_PUBLIC, fieldInfo.getName() + "_asm_deser__", "Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;");
            fw.visitEnd();
        }
    }
    MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "(Lcom/alibaba/fastjson/parser/ParserConfig;Ljava/lang/Class;)V", null, null);
    mw.visitVarInsn(ALOAD, 0);
    mw.visitVarInsn(ALOAD, 1);
    mw.visitVarInsn(ALOAD, 2);
    mw.visitMethodInsn(INVOKESPECIAL, "com/alibaba/fastjson/parser/deserializer/ASMJavaBeanDeserializer", "<init>", "(Lcom/alibaba/fastjson/parser/ParserConfig;Ljava/lang/Class;)V");
    mw.visitVarInsn(ALOAD, 0);
    mw.visitFieldInsn(GETFIELD, "com/alibaba/fastjson/parser/deserializer/ASMJavaBeanDeserializer", "serializer", "Lcom/alibaba/fastjson/parser/deserializer/ASMJavaBeanDeserializer$InnerJavaBeanDeserializer;");
    mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer", "getFieldDeserializerMap", "()Ljava/util/Map;");
    mw.visitInsn(POP);
    // init fieldNamePrefix
    for (int i = 0, size = context.getFieldInfoList().size(); i < size; ++i) {
        FieldInfo fieldInfo = context.getFieldInfoList().get(i);
        mw.visitVarInsn(ALOAD, 0);
        // public char[] toCharArray()
        mw.visitLdcInsn("\"" + fieldInfo.getName() + "\":");
        mw.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "toCharArray", "()[C");
        mw.visitFieldInsn(PUTFIELD, context.getClassName(), fieldInfo.getName() + "_asm_prefix__", "[C");
    }
    mw.visitInsn(RETURN);
    mw.visitMaxs(4, 4);
    mw.visitEnd();
}
Also used : FieldVisitor(com.alibaba.fastjson.asm.FieldVisitor) FieldInfo(com.alibaba.fastjson.util.FieldInfo) MethodVisitor(com.alibaba.fastjson.asm.MethodVisitor)

Example 2 with FieldVisitor

use of com.alibaba.fastjson.asm.FieldVisitor in project uavstack by uavorg.

the class ASMSerializerFactory method createJavaBeanSerializer.

public ObjectSerializer createJavaBeanSerializer(Class<?> clazz, Map<String, String> aliasMap) throws Exception {
    if (clazz.isPrimitive()) {
        throw new JSONException("unsupportd class " + clazz.getName());
    }
    List<FieldInfo> getters = TypeUtils.computeGetters(clazz, aliasMap, false);
    for (FieldInfo getter : getters) {
        if (!ASMUtils.checkName(getter.getMember().getName())) {
            return null;
        }
    }
    String className = getGenClassName(clazz);
    int beanSerializeFeatures = TypeUtils.getSerializeFeatures(clazz);
    ClassWriter cw = new ClassWriter();
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, "com/alibaba/fastjson/serializer/ASMJavaBeanSerializer", new String[] { "com/alibaba/fastjson/serializer/ObjectSerializer" });
    for (FieldInfo fieldInfo : getters) {
        {
            FieldVisitor fw = cw.visitField(ACC_PUBLIC, fieldInfo.getName() + "_asm_fieldPrefix", "Ljava/lang/reflect/Type;");
            fw.visitEnd();
        }
        FieldVisitor fw = cw.visitField(ACC_PUBLIC, fieldInfo.getName() + "_asm_fieldType", "Ljava/lang/reflect/Type;");
        fw.visitEnd();
    }
    MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mw.visitVarInsn(ALOAD, 0);
    mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(getDesc(clazz)));
    mw.visitMethodInsn(INVOKESPECIAL, "com/alibaba/fastjson/serializer/ASMJavaBeanSerializer", "<init>", "(Ljava/lang/Class;)V");
    for (FieldInfo fieldInfo : getters) {
        mw.visitVarInsn(ALOAD, 0);
        mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(getDesc(fieldInfo.getDeclaringClass())));
        if (fieldInfo.getMethod() != null) {
            mw.visitLdcInsn(fieldInfo.getMethod().getName());
            mw.visitMethodInsn(INVOKESTATIC, "com/alibaba/fastjson/util/ASMUtils", "getMethodType", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Type;");
        } else {
            mw.visitLdcInsn(fieldInfo.getField().getName());
            mw.visitMethodInsn(INVOKESTATIC, "com/alibaba/fastjson/util/ASMUtils", "getFieldType", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Type;");
        }
        mw.visitFieldInsn(PUTFIELD, className, fieldInfo.getName() + "_asm_fieldType", "Ljava/lang/reflect/Type;");
    }
    mw.visitInsn(RETURN);
    mw.visitMaxs(4, 4);
    mw.visitEnd();
    {
        Context context = new Context(className, beanSerializeFeatures);
        mw = cw.visitMethod(ACC_PUBLIC, "write", "(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V", null, new String[] { "java/io/IOException" });
        // serializer
        mw.visitVarInsn(ALOAD, context.serializer());
        mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/JSONSerializer", "getWriter", "()Lcom/alibaba/fastjson/serializer/SerializeWriter;");
        mw.visitVarInsn(ASTORE, context.var("out"));
        JSONType jsonType = clazz.getAnnotation(JSONType.class);
        if (jsonType == null || jsonType.alphabetic()) {
            Label _else = new Label();
            mw.visitVarInsn(ALOAD, context.var("out"));
            mw.visitFieldInsn(GETSTATIC, "com/alibaba/fastjson/serializer/SerializerFeature", "SortField", "Lcom/alibaba/fastjson/serializer/SerializerFeature;");
            mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/SerializeWriter", "isEnabled", "(Lcom/alibaba/fastjson/serializer/SerializerFeature;)Z");
            mw.visitJumpInsn(IFEQ, _else);
            mw.visitVarInsn(ALOAD, 0);
            mw.visitVarInsn(ALOAD, 1);
            mw.visitVarInsn(ALOAD, 2);
            mw.visitVarInsn(ALOAD, 3);
            mw.visitVarInsn(ALOAD, 4);
            mw.visitVarInsn(ILOAD, 5);
            mw.visitMethodInsn(INVOKEVIRTUAL, className, "write1", "(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
            mw.visitInsn(RETURN);
            mw.visitLabel(_else);
        }
        // obj
        mw.visitVarInsn(ALOAD, context.obj());
        // serializer
        mw.visitTypeInsn(CHECKCAST, getType(clazz));
        // obj
        mw.visitVarInsn(ASTORE, context.var("entity"));
        generateWriteMethod(clazz, mw, getters, context);
        mw.visitInsn(RETURN);
        mw.visitMaxs(6, context.getVariantCount() + 1);
        mw.visitEnd();
    }
    List<FieldInfo> sortedGetters = TypeUtils.computeGetters(clazz, aliasMap, true);
    {
        // sortField support
        Context context = new Context(className, beanSerializeFeatures);
        mw = cw.visitMethod(ACC_PUBLIC, "write1", "(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V", null, new String[] { "java/io/IOException" });
        // serializer
        mw.visitVarInsn(ALOAD, context.serializer());
        mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/JSONSerializer", "getWriter", "()Lcom/alibaba/fastjson/serializer/SerializeWriter;");
        mw.visitVarInsn(ASTORE, context.var("out"));
        // obj
        mw.visitVarInsn(ALOAD, context.obj());
        // serializer
        mw.visitTypeInsn(CHECKCAST, getType(clazz));
        // obj
        mw.visitVarInsn(ASTORE, context.var("entity"));
        generateWriteMethod(clazz, mw, sortedGetters, context);
        mw.visitInsn(RETURN);
        mw.visitMaxs(6, context.getVariantCount() + 1);
        mw.visitEnd();
    }
    // writeAsArray
    {
        Context context = new Context(className, beanSerializeFeatures);
        mw = cw.visitMethod(ACC_PUBLIC, "writeAsArray", "(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;)V", null, new String[] { "java/io/IOException" });
        // serializer
        mw.visitVarInsn(ALOAD, context.serializer());
        mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/JSONSerializer", "getWriter", "()Lcom/alibaba/fastjson/serializer/SerializeWriter;");
        mw.visitVarInsn(ASTORE, context.var("out"));
        // obj
        mw.visitVarInsn(ALOAD, context.obj());
        // serializer
        mw.visitTypeInsn(CHECKCAST, getType(clazz));
        // obj
        mw.visitVarInsn(ASTORE, context.var("entity"));
        generateWriteAsArray(clazz, mw, sortedGetters, context);
        mw.visitInsn(RETURN);
        mw.visitMaxs(6, context.getVariantCount() + 1);
        mw.visitEnd();
    }
    byte[] code = cw.toByteArray();
    if (JSON.DUMP_CLASS != null) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(JSON.DUMP_CLASS + File.separator + className + ".class");
            fos.write(code);
        } catch (Exception ex) {
        // System.err.println("FASTJSON dump class:"+className+"失败:"+ex.getMessage());
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
    Class<?> exampleClass = classLoader.defineClassPublic(className, code, 0, code.length);
    Object instance = exampleClass.newInstance();
    return (ObjectSerializer) instance;
}
Also used : Label(com.alibaba.fastjson.asm.Label) JSONException(com.alibaba.fastjson.JSONException) FieldVisitor(com.alibaba.fastjson.asm.FieldVisitor) ClassWriter(com.alibaba.fastjson.asm.ClassWriter) JSONException(com.alibaba.fastjson.JSONException) MethodVisitor(com.alibaba.fastjson.asm.MethodVisitor) FileOutputStream(java.io.FileOutputStream) JSONType(com.alibaba.fastjson.annotation.JSONType) FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Aggregations

FieldVisitor (com.alibaba.fastjson.asm.FieldVisitor)2 MethodVisitor (com.alibaba.fastjson.asm.MethodVisitor)2 FieldInfo (com.alibaba.fastjson.util.FieldInfo)2 JSONException (com.alibaba.fastjson.JSONException)1 JSONType (com.alibaba.fastjson.annotation.JSONType)1 ClassWriter (com.alibaba.fastjson.asm.ClassWriter)1 Label (com.alibaba.fastjson.asm.Label)1 FileOutputStream (java.io.FileOutputStream)1