Search in sources :

Example 16 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class ASMSerializerFactory method generateWriteMethod.

private void generateWriteMethod(Class<?> clazz, MethodVisitor mw, FieldInfo[] getters, Context context) throws Exception {
    // if (serializer.containsReference(object)) {
    Label end = new Label();
    int size = getters.length;
    if (!context.writeDirect) {
        // pretty format not byte code optimized
        Label endSupper_ = new Label();
        Label supper_ = new Label();
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitLdcInsn(SerializerFeature.PrettyFormat.mask);
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z");
        mw.visitJumpInsn(IFNE, supper_);
        boolean hasMethod = false;
        for (FieldInfo getter : getters) {
            if (getter.method != null) {
                hasMethod = true;
            }
        }
        if (hasMethod) {
            mw.visitVarInsn(ALOAD, context.var("out"));
            mw.visitLdcInsn(SerializerFeature.IgnoreErrorGetter.mask);
            mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z");
            mw.visitJumpInsn(IFEQ, endSupper_);
        } else {
            mw.visitJumpInsn(GOTO, endSupper_);
        }
        mw.visitLabel(supper_);
        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(INVOKESPECIAL, JavaBeanSerializer, "write", "(L" + JSONSerializer + ";Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
        mw.visitInsn(RETURN);
        mw.visitLabel(endSupper_);
    }
    if (!context.nonContext) {
        Label endRef_ = new Label();
        // /////
        // this
        mw.visitVarInsn(ALOAD, 0);
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, Context.obj);
        mw.visitVarInsn(ILOAD, Context.features);
        mw.visitMethodInsn(INVOKEVIRTUAL, JavaBeanSerializer, "writeReference", "(L" + JSONSerializer + ";Ljava/lang/Object;I)Z");
        mw.visitJumpInsn(IFEQ, endRef_);
        mw.visitInsn(RETURN);
        mw.visitLabel(endRef_);
    }
    final String writeAsArrayMethodName;
    if (context.writeDirect) {
        if (context.nonContext) {
            writeAsArrayMethodName = "writeAsArrayNonContext";
        } else {
            writeAsArrayMethodName = "writeAsArray";
        }
    } else {
        writeAsArrayMethodName = "writeAsArrayNormal";
    }
    if ((context.beanInfo.features & SerializerFeature.BeanToArray.mask) == 0) {
        Label endWriteAsArray_ = new Label();
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitLdcInsn(SerializerFeature.BeanToArray.mask);
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z");
        mw.visitJumpInsn(IFEQ, endWriteAsArray_);
        // /////
        // this
        mw.visitVarInsn(ALOAD, 0);
        mw.visitVarInsn(ALOAD, Context.serializer);
        // obj
        mw.visitVarInsn(ALOAD, 2);
        // fieldObj
        mw.visitVarInsn(ALOAD, 3);
        // fieldType
        mw.visitVarInsn(ALOAD, 4);
        // features
        mw.visitVarInsn(ILOAD, 5);
        //
        mw.visitMethodInsn(//
        INVOKEVIRTUAL, //
        context.className, //
        writeAsArrayMethodName, "(L" + JSONSerializer + ";Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
        mw.visitInsn(RETURN);
        mw.visitLabel(endWriteAsArray_);
    } else {
        // this
        mw.visitVarInsn(ALOAD, 0);
        mw.visitVarInsn(ALOAD, Context.serializer);
        // obj
        mw.visitVarInsn(ALOAD, 2);
        // fieldObj
        mw.visitVarInsn(ALOAD, 3);
        // fieldType
        mw.visitVarInsn(ALOAD, 4);
        // features
        mw.visitVarInsn(ILOAD, 5);
        //
        mw.visitMethodInsn(//
        INVOKEVIRTUAL, //
        context.className, //
        writeAsArrayMethodName, "(L" + JSONSerializer + ";Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
        mw.visitInsn(RETURN);
    }
    if (!context.nonContext) {
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "getContext", "()" + SerialContext_desc);
        mw.visitVarInsn(ASTORE, context.var("parent"));
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, context.var("parent"));
        mw.visitVarInsn(ALOAD, Context.obj);
        mw.visitVarInsn(ALOAD, Context.paramFieldName);
        mw.visitLdcInsn(context.beanInfo.features);
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "setContext", "(" + SerialContext_desc + "Ljava/lang/Object;Ljava/lang/Object;I)V");
    }
    // SEPERATO
    if (!context.writeDirect) {
        Label end_ = new Label();
        Label else_ = new Label();
        Label writeClass_ = new Label();
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, Context.paramFieldType);
        mw.visitVarInsn(ALOAD, Context.obj);
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "isWriteClassName", "(Ljava/lang/reflect/Type;Ljava/lang/Object;)Z");
        mw.visitJumpInsn(IFEQ, else_);
        // IFNULL
        mw.visitVarInsn(ALOAD, Context.paramFieldType);
        mw.visitVarInsn(ALOAD, Context.obj);
        mw.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
        mw.visitJumpInsn(IF_ACMPEQ, else_);
        mw.visitLabel(writeClass_);
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitVarInsn(BIPUSH, '{');
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
        mw.visitVarInsn(ALOAD, 0);
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, Context.obj);
        mw.visitMethodInsn(INVOKEVIRTUAL, JavaBeanSerializer, "writeClassName", "(L" + JSONSerializer + ";Ljava/lang/Object;)V");
        mw.visitVarInsn(BIPUSH, ',');
        mw.visitJumpInsn(GOTO, end_);
        mw.visitLabel(else_);
        mw.visitVarInsn(BIPUSH, '{');
        mw.visitLabel(end_);
    } else {
        mw.visitVarInsn(BIPUSH, '{');
    }
    mw.visitVarInsn(ISTORE, context.var("seperator"));
    if (!context.writeDirect) {
        _before(mw, context);
    }
    if (!context.writeDirect) {
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isNotWriteDefaultValue", "()Z");
        mw.visitVarInsn(ISTORE, context.var("notWriteDefaultValue"));
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "checkValue", "(" + SerializeFilterable_desc + ")Z");
        mw.visitVarInsn(ISTORE, context.var("checkValue"));
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "hasNameFilters", "(" + SerializeFilterable_desc + ")Z");
        mw.visitVarInsn(ISTORE, context.var("hasNameFilters"));
    }
    for (int i = 0; i < size; ++i) {
        FieldInfo property = getters[i];
        Class<?> propertyClass = property.fieldClass;
        mw.visitLdcInsn(property.name);
        mw.visitVarInsn(ASTORE, Context.fieldName);
        if (//
        propertyClass == byte.class || //
        propertyClass == short.class || propertyClass == int.class) {
            _int(clazz, mw, property, context, context.var(propertyClass.getName()), 'I');
        } else if (propertyClass == long.class) {
            _long(clazz, mw, property, context);
        } else if (propertyClass == float.class) {
            _float(clazz, mw, property, context);
        } else if (propertyClass == double.class) {
            _double(clazz, mw, property, context);
        } else if (propertyClass == boolean.class) {
            _int(clazz, mw, property, context, context.var("boolean"), 'Z');
        } else if (propertyClass == char.class) {
            _int(clazz, mw, property, context, context.var("char"), 'C');
        } else if (propertyClass == String.class) {
            _string(clazz, mw, property, context);
        } else if (propertyClass == BigDecimal.class) {
            _decimal(clazz, mw, property, context);
        } else if (List.class.isAssignableFrom(propertyClass)) {
            _list(clazz, mw, property, context);
        } else if (propertyClass.isEnum()) {
            _enum(clazz, mw, property, context);
        } else {
            _object(clazz, mw, property, context);
        }
    }
    if (!context.writeDirect) {
        _after(mw, context);
    }
    Label _else = new Label();
    Label _end_if = new Label();
    mw.visitVarInsn(ILOAD, context.var("seperator"));
    mw.visitIntInsn(BIPUSH, '{');
    mw.visitJumpInsn(IF_ICMPNE, _else);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitVarInsn(BIPUSH, '{');
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
    mw.visitLabel(_else);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitVarInsn(BIPUSH, '}');
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
    mw.visitLabel(_end_if);
    mw.visitLabel(end);
    if (!context.nonContext) {
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, context.var("parent"));
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "setContext", "(" + SerialContext_desc + ")V");
    }
}
Also used : Label(com.alibaba.fastjson.asm.Label) FieldInfo(com.alibaba.fastjson.util.FieldInfo) BigDecimal(java.math.BigDecimal)

Example 17 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class ASMDeserializerFactory method _deserialze.

private void _deserialze(ClassWriter cw, Context context) {
    if (context.fieldInfoList.length == 0) {
        return;
    }
    for (FieldInfo fieldInfo : context.fieldInfoList) {
        Class<?> fieldClass = fieldInfo.fieldClass;
        Type fieldType = fieldInfo.fieldType;
        if (fieldClass == char.class) {
            return;
        }
        if (Collection.class.isAssignableFrom(fieldClass)) {
            if (fieldType instanceof ParameterizedType) {
                Type itemType = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
                if (itemType instanceof Class) {
                    continue;
                } else {
                    return;
                }
            } else {
                return;
            }
        }
    }
    JavaBeanInfo beanInfo = context.beanInfo;
    context.fieldInfoList = beanInfo.sortedFields;
    MethodVisitor mw = new MethodWriter(cw, ACC_PUBLIC, "deserialze", "(L" + DefaultJSONParser + ";Ljava/lang/reflect/Type;Ljava/lang/Object;I)Ljava/lang/Object;", null, null);
    Label reset_ = new Label();
    Label super_ = new Label();
    Label return_ = new Label();
    Label end_ = new Label();
    defineVarLexer(context, mw);
    {
        Label next_ = new Label();
        // isSupportArrayToBean
        mw.visitVarInsn(ALOAD, context.var("lexer"));
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "token", "()I");
        mw.visitLdcInsn(JSONToken.LBRACKET);
        mw.visitJumpInsn(IF_ICMPNE, next_);
        if ((beanInfo.parserFeatures & Feature.SupportArrayToBean.mask) == 0) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ILOAD, 4);
            mw.visitLdcInsn(Feature.SupportArrayToBean.mask);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "isEnabled", "(II)Z");
            mw.visitJumpInsn(IFEQ, next_);
        }
        mw.visitVarInsn(ALOAD, 0);
        mw.visitVarInsn(ALOAD, Context.parser);
        mw.visitVarInsn(ALOAD, 2);
        mw.visitVarInsn(ALOAD, 3);
        //mw.visitVarInsn(ALOAD, 5);
        mw.visitInsn(ACONST_NULL);
        //
        mw.visitMethodInsn(//
        INVOKESPECIAL, //
        context.className, //
        "deserialzeArrayMapping", "(L" + DefaultJSONParser + ";Ljava/lang/reflect/Type;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
        mw.visitInsn(ARETURN);
        mw.visitLabel(next_);
    // deserialzeArrayMapping
    }
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(Feature.SortFeidFastMatch.mask);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "isEnabled", "(I)Z");
    mw.visitJumpInsn(IFEQ, super_);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(context.clazz.getName());
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanType", "(Ljava/lang/String;)I");
    mw.visitLdcInsn(com.alibaba.fastjson.parser.JSONLexerBase.NOT_MATCH);
    mw.visitJumpInsn(IF_ICMPEQ, super_);
    // parser
    mw.visitVarInsn(ALOAD, 1);
    mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getContext", "()" + desc(ParseContext.class));
    mw.visitVarInsn(ASTORE, context.var("mark_context"));
    // ParseContext context = parser.getContext();
    mw.visitInsn(ICONST_0);
    mw.visitVarInsn(ISTORE, context.var("matchedCount"));
    _createInstance(context, mw);
    {
        // parser
        mw.visitVarInsn(ALOAD, 1);
        mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getContext", "()" + desc(ParseContext.class));
        mw.visitVarInsn(ASTORE, context.var("context"));
        // parser
        mw.visitVarInsn(ALOAD, 1);
        mw.visitVarInsn(ALOAD, context.var("context"));
        mw.visitVarInsn(ALOAD, context.var("instance"));
        // fieldName
        mw.visitVarInsn(ALOAD, 3);
        //
        mw.visitMethodInsn(//
        INVOKEVIRTUAL, //
        DefaultJSONParser, //
        "setContext", "(" + desc(ParseContext.class) + "Ljava/lang/Object;Ljava/lang/Object;)" + desc(ParseContext.class));
        mw.visitVarInsn(ASTORE, context.var("childContext"));
    }
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitFieldInsn(GETFIELD, JSONLexerBase, "matchStat", "I");
    mw.visitLdcInsn(com.alibaba.fastjson.parser.JSONLexerBase.END);
    mw.visitJumpInsn(IF_ICMPEQ, return_);
    // UNKOWN
    mw.visitInsn(ICONST_0);
    mw.visitIntInsn(ISTORE, context.var("matchStat"));
    int fieldListSize = context.fieldInfoList.length;
    for (int i = 0; i < fieldListSize; i += 32) {
        mw.visitInsn(ICONST_0);
        mw.visitVarInsn(ISTORE, context.var("_asm_flag_" + (i / 32)));
    }
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(Feature.InitStringFieldAsEmpty.mask);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "isEnabled", "(I)Z");
    mw.visitIntInsn(ISTORE, context.var("initStringFieldAsEmpty"));
    // declare and init
    for (int i = 0; i < fieldListSize; ++i) {
        FieldInfo fieldInfo = context.fieldInfoList[i];
        Class<?> fieldClass = fieldInfo.fieldClass;
        if (//
        fieldClass == boolean.class || //
        fieldClass == byte.class || //
        fieldClass == short.class || fieldClass == int.class) {
            mw.visitInsn(ICONST_0);
            mw.visitVarInsn(ISTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == long.class) {
            mw.visitInsn(LCONST_0);
            mw.visitVarInsn(LSTORE, context.var(fieldInfo.name + "_asm", 2));
        } else if (fieldClass == float.class) {
            mw.visitInsn(FCONST_0);
            mw.visitVarInsn(FSTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == double.class) {
            mw.visitInsn(DCONST_0);
            mw.visitVarInsn(DSTORE, context.var(fieldInfo.name + "_asm", 2));
        } else {
            if (fieldClass == String.class) {
                Label flagEnd_ = new Label();
                Label flagElse_ = new Label();
                mw.visitVarInsn(ILOAD, context.var("initStringFieldAsEmpty"));
                mw.visitJumpInsn(IFEQ, flagElse_);
                _setFlag(mw, context, i);
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "stringDefaultValue", "()Ljava/lang/String;");
                mw.visitJumpInsn(GOTO, flagEnd_);
                mw.visitLabel(flagElse_);
                mw.visitInsn(ACONST_NULL);
                mw.visitLabel(flagEnd_);
            } else {
                mw.visitInsn(ACONST_NULL);
            }
            // cast
            mw.visitTypeInsn(CHECKCAST, type(fieldClass));
            mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        }
    }
    for (int i = 0; i < fieldListSize; ++i) {
        FieldInfo fieldInfo = context.fieldInfoList[i];
        Class<?> fieldClass = fieldInfo.fieldClass;
        Type fieldType = fieldInfo.fieldType;
        Label notMatch_ = new Label();
        if (fieldClass == boolean.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldBoolean", "([C)Z");
            mw.visitVarInsn(ISTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == byte.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldInt", "([C)I");
            mw.visitVarInsn(ISTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == short.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldInt", "([C)I");
            mw.visitVarInsn(ISTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == int.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldInt", "([C)I");
            mw.visitVarInsn(ISTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == long.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldLong", "([C)J");
            mw.visitVarInsn(LSTORE, context.var(fieldInfo.name + "_asm", 2));
        } else if (fieldClass == float.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldFloat", "([C)F");
            mw.visitVarInsn(FSTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == double.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldDouble", "([C)D");
            mw.visitVarInsn(DSTORE, context.var(fieldInfo.name + "_asm", 2));
        } else if (fieldClass == String.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldString", "([C)Ljava/lang/String;");
            mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == int[].class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldIntArray", "([C)[I");
            mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == float[].class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldFloatArray", "([C)[F");
            mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == float[][].class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldFloatArray2", "([C)[[F");
            mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass.isEnum()) {
            mw.visitVarInsn(ALOAD, 0);
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            _getFieldDeser(context, mw, fieldInfo);
            mw.visitMethodInsn(INVOKEVIRTUAL, type(JavaBeanDeserializer.class), "scanEnum", "(L" + JSONLexerBase + ";[C" + desc(ObjectDeserializer.class) + ")Ljava/lang/Enum;");
            // cast
            mw.visitTypeInsn(CHECKCAST, type(fieldClass));
            mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        //            } else if (fieldClass.isEnum()) {
        //                mw.visitVarInsn(ALOAD, context.var("lexer"));
        //                mw.visitVarInsn(ALOAD, 0);
        //                mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
        //                Label enumNull_ = new Label();
        //                mw.visitInsn(ACONST_NULL);
        //                mw.visitTypeInsn(CHECKCAST, type(fieldClass)); // cast
        //                mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        //
        //                mw.visitVarInsn(ALOAD, 1);
        //
        //                mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getSymbolTable", "()" + desc(SymbolTable.class));
        //
        //                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldSymbol",
        //                        "([C" + desc(SymbolTable.class) + ")Ljava/lang/String;");
        //                mw.visitInsn(DUP);
        //                mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm_enumName"));
        //
        //                mw.visitJumpInsn(IFNULL, enumNull_);
        //
        //                mw.visitVarInsn(ALOAD, context.var(fieldInfo.name + "_asm_enumName"));
        //                mw.visitMethodInsn(INVOKEVIRTUAL, type(String.class), "length", "()I");
        //                mw.visitJumpInsn(IFEQ, enumNull_);
        //
        //                mw.visitVarInsn(ALOAD, context.var(fieldInfo.name + "_asm_enumName"));
        //                mw.visitMethodInsn(INVOKESTATIC, type(fieldClass), "valueOf",
        //                        "(Ljava/lang/String;)" + desc(fieldClass));
        //                mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        //                mw.visitLabel(enumNull_);
        } else if (Collection.class.isAssignableFrom(fieldClass)) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(ALOAD, 0);
            mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C");
            Class<?> itemClass = TypeUtils.getCollectionItemClass(fieldType);
            if (itemClass == String.class) {
                // cast
                mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(fieldClass)));
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFieldStringArray", "([CLjava/lang/Class;)" + desc(Collection.class));
                mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
            } else {
                _deserialze_list_obj(context, mw, reset_, fieldInfo, fieldClass, itemClass, i);
                if (i == fieldListSize - 1) {
                    _deserialize_endCheck(context, mw, reset_);
                }
                continue;
            }
        } else {
            _deserialze_obj(context, mw, reset_, fieldInfo, fieldClass, i);
            if (i == fieldListSize - 1) {
                _deserialize_endCheck(context, mw, reset_);
            }
            continue;
        }
        mw.visitVarInsn(ALOAD, context.var("lexer"));
        mw.visitFieldInsn(GETFIELD, JSONLexerBase, "matchStat", "I");
        Label flag_ = new Label();
        // mw.visitInsn(DUP);
        mw.visitJumpInsn(IFLE, flag_);
        _setFlag(mw, context, i);
        mw.visitLabel(flag_);
        mw.visitVarInsn(ALOAD, context.var("lexer"));
        mw.visitFieldInsn(GETFIELD, JSONLexerBase, "matchStat", "I");
        mw.visitInsn(DUP);
        mw.visitVarInsn(ISTORE, context.var("matchStat"));
        mw.visitLdcInsn(com.alibaba.fastjson.parser.JSONLexerBase.NOT_MATCH);
        mw.visitJumpInsn(IF_ICMPEQ, reset_);
        mw.visitVarInsn(ALOAD, context.var("lexer"));
        mw.visitFieldInsn(GETFIELD, JSONLexerBase, "matchStat", "I");
        mw.visitJumpInsn(IFLE, notMatch_);
        // increment matchedCount
        mw.visitVarInsn(ILOAD, context.var("matchedCount"));
        mw.visitInsn(ICONST_1);
        mw.visitInsn(IADD);
        mw.visitVarInsn(ISTORE, context.var("matchedCount"));
        mw.visitVarInsn(ALOAD, context.var("lexer"));
        mw.visitFieldInsn(GETFIELD, JSONLexerBase, "matchStat", "I");
        mw.visitLdcInsn(com.alibaba.fastjson.parser.JSONLexerBase.END);
        mw.visitJumpInsn(IF_ICMPEQ, end_);
        mw.visitLabel(notMatch_);
        if (i == fieldListSize - 1) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitFieldInsn(GETFIELD, JSONLexerBase, "matchStat", "I");
            mw.visitLdcInsn(com.alibaba.fastjson.parser.JSONLexerBase.END);
            mw.visitJumpInsn(IF_ICMPNE, reset_);
        }
    }
    // endFor
    mw.visitLabel(end_);
    if (!context.clazz.isInterface() && !Modifier.isAbstract(context.clazz.getModifiers())) {
        _batchSet(context, mw);
    }
    mw.visitLabel(return_);
    _setContext(context, mw);
    mw.visitVarInsn(ALOAD, context.var("instance"));
    Method buildMethod = context.beanInfo.buildMethod;
    if (buildMethod != null) {
        mw.visitMethodInsn(INVOKEVIRTUAL, type(context.getInstClass()), buildMethod.getName(), "()" + desc(buildMethod.getReturnType()));
    }
    mw.visitInsn(ARETURN);
    mw.visitLabel(reset_);
    _batchSet(context, mw);
    mw.visitVarInsn(ALOAD, 0);
    mw.visitVarInsn(ALOAD, 1);
    mw.visitVarInsn(ALOAD, 2);
    mw.visitVarInsn(ALOAD, 3);
    mw.visitVarInsn(ALOAD, context.var("instance"));
    mw.visitVarInsn(ILOAD, 4);
    int flagSize = (fieldListSize / 32);
    if (fieldListSize != 0 && (fieldListSize % 32) != 0) {
        flagSize += 1;
    }
    if (flagSize == 1) {
        mw.visitInsn(ICONST_1);
    } else {
        mw.visitIntInsn(BIPUSH, flagSize);
    }
    mw.visitIntInsn(NEWARRAY, T_INT);
    for (int i = 0; i < flagSize; ++i) {
        mw.visitInsn(DUP);
        if (i == 0) {
            mw.visitInsn(ICONST_0);
        } else if (i == 1) {
            mw.visitInsn(ICONST_1);
        } else {
            mw.visitIntInsn(BIPUSH, i);
        }
        mw.visitVarInsn(ILOAD, context.var("_asm_flag_" + i));
        mw.visitInsn(IASTORE);
    }
    mw.visitMethodInsn(INVOKEVIRTUAL, type(JavaBeanDeserializer.class), "parseRest", "(L" + DefaultJSONParser + ";Ljava/lang/reflect/Type;Ljava/lang/Object;Ljava/lang/Object;I[I)Ljava/lang/Object;");
    // cast
    mw.visitTypeInsn(CHECKCAST, type(context.clazz));
    mw.visitInsn(ARETURN);
    mw.visitLabel(super_);
    mw.visitVarInsn(ALOAD, 0);
    mw.visitVarInsn(ALOAD, 1);
    mw.visitVarInsn(ALOAD, 2);
    mw.visitVarInsn(ALOAD, 3);
    mw.visitVarInsn(ILOAD, 4);
    //
    mw.visitMethodInsn(//
    INVOKESPECIAL, //
    type(JavaBeanDeserializer.class), //
    "deserialze", "(L" + DefaultJSONParser + ";Ljava/lang/reflect/Type;Ljava/lang/Object;I)Ljava/lang/Object;");
    mw.visitInsn(ARETURN);
    mw.visitMaxs(10, context.variantIndex);
    mw.visitEnd();
}
Also used : JavaBeanInfo(com.alibaba.fastjson.util.JavaBeanInfo) MethodWriter(com.alibaba.fastjson.asm.MethodWriter) Label(com.alibaba.fastjson.asm.Label) Method(java.lang.reflect.Method) MethodVisitor(com.alibaba.fastjson.asm.MethodVisitor) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ParseContext(com.alibaba.fastjson.parser.ParseContext) FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Example 18 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class ASMDeserializerFactory method _deserialzeArrayMapping.

private void _deserialzeArrayMapping(ClassWriter cw, Context context) {
    MethodVisitor mw = new MethodWriter(cw, ACC_PUBLIC, "deserialzeArrayMapping", "(L" + DefaultJSONParser + ";Ljava/lang/reflect/Type;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    defineVarLexer(context, mw);
    _createInstance(context, mw);
    FieldInfo[] sortedFieldInfoList = context.beanInfo.sortedFields;
    int fieldListSize = sortedFieldInfoList.length;
    for (int i = 0; i < fieldListSize; ++i) {
        final boolean last = (i == fieldListSize - 1);
        final char seperator = last ? ']' : ',';
        FieldInfo fieldInfo = sortedFieldInfoList[i];
        Class<?> fieldClass = fieldInfo.fieldClass;
        Type fieldType = fieldInfo.fieldType;
        if (//
        fieldClass == byte.class || //
        fieldClass == short.class || fieldClass == int.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanInt", "(C)I");
            mw.visitVarInsn(ISTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == long.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanLong", "(C)J");
            mw.visitVarInsn(LSTORE, context.var(fieldInfo.name + "_asm", 2));
        } else if (fieldClass == boolean.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanBoolean", "(C)Z");
            mw.visitVarInsn(ISTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == float.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanFloat", "(C)F");
            mw.visitVarInsn(FSTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == double.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanDouble", "(C)D");
            mw.visitVarInsn(DSTORE, context.var(fieldInfo.name + "_asm", 2));
        } else if (fieldClass == char.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanString", "(C)Ljava/lang/String;");
            mw.visitInsn(ICONST_0);
            mw.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C");
            mw.visitVarInsn(ISTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass == String.class) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanString", "(C)Ljava/lang/String;");
            mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        } else if (fieldClass.isEnum()) {
            Label enumNumIf_ = new Label();
            Label enumNumErr_ = new Label();
            Label enumStore_ = new Label();
            Label enumQuote_ = new Label();
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "getCurrent", "()C");
            mw.visitInsn(DUP);
            mw.visitVarInsn(ISTORE, context.var("ch"));
            mw.visitLdcInsn((int) 'n');
            mw.visitJumpInsn(IF_ICMPEQ, enumQuote_);
            mw.visitVarInsn(ILOAD, context.var("ch"));
            mw.visitLdcInsn((int) '\"');
            mw.visitJumpInsn(IF_ICMPNE, enumNumIf_);
            mw.visitLabel(enumQuote_);
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(fieldClass)));
            mw.visitVarInsn(ALOAD, 1);
            mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getSymbolTable", "()" + desc(SymbolTable.class));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanEnum", "(Ljava/lang/Class;" + desc(SymbolTable.class) + "C)Ljava/lang/Enum;");
            mw.visitJumpInsn(GOTO, enumStore_);
            // (ch >= '0' && ch <= '9') {
            mw.visitLabel(enumNumIf_);
            mw.visitVarInsn(ILOAD, context.var("ch"));
            mw.visitLdcInsn((int) '0');
            mw.visitJumpInsn(IF_ICMPLT, enumNumErr_);
            mw.visitVarInsn(ILOAD, context.var("ch"));
            mw.visitLdcInsn((int) '9');
            mw.visitJumpInsn(IF_ICMPGT, enumNumErr_);
            _getFieldDeser(context, mw, fieldInfo);
            // cast
            mw.visitTypeInsn(CHECKCAST, type(EnumDeserializer.class));
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanInt", "(C)I");
            mw.visitMethodInsn(INVOKEVIRTUAL, type(EnumDeserializer.class), "valueOf", "(I)Ljava/lang/Enum;");
            mw.visitJumpInsn(GOTO, enumStore_);
            mw.visitLabel(enumNumErr_);
            mw.visitVarInsn(ALOAD, 0);
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitVarInsn(BIPUSH, seperator);
            mw.visitMethodInsn(INVOKEVIRTUAL, type(JavaBeanDeserializer.class), "scanEnum", "(L" + JSONLexerBase + ";C)Ljava/lang/Enum;");
            mw.visitLabel(enumStore_);
            // cast
            mw.visitTypeInsn(CHECKCAST, type(fieldClass));
            mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        } else if (Collection.class.isAssignableFrom(fieldClass)) {
            Class<?> itemClass = TypeUtils.getCollectionItemClass(fieldType);
            if (itemClass == String.class) {
                if (fieldClass == List.class || fieldClass == Collections.class || fieldClass == ArrayList.class) {
                    mw.visitTypeInsn(NEW, type(ArrayList.class));
                    mw.visitInsn(DUP);
                    mw.visitMethodInsn(INVOKESPECIAL, type(ArrayList.class), "<init>", "()V");
                } else {
                    mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(fieldClass)));
                    mw.visitMethodInsn(INVOKESTATIC, type(TypeUtils.class), "createCollection", "(Ljava/lang/Class;)Ljava/util/Collection;");
                }
                mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitVarInsn(ALOAD, context.var(fieldInfo.name + "_asm"));
                mw.visitVarInsn(BIPUSH, seperator);
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanStringArray", "(Ljava/util/Collection;C)V");
                Label valueNullEnd_ = new Label();
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitFieldInsn(GETFIELD, JSONLexerBase, "matchStat", "I");
                mw.visitLdcInsn(com.alibaba.fastjson.parser.JSONLexerBase.VALUE_NULL);
                mw.visitJumpInsn(IF_ICMPNE, valueNullEnd_);
                mw.visitInsn(ACONST_NULL);
                mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
                mw.visitLabel(valueNullEnd_);
            } else {
                Label notError_ = new Label();
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "token", "()I");
                mw.visitVarInsn(ISTORE, context.var("token"));
                mw.visitVarInsn(ILOAD, context.var("token"));
                int token = i == 0 ? JSONToken.LBRACKET : JSONToken.COMMA;
                mw.visitLdcInsn(token);
                mw.visitJumpInsn(IF_ICMPEQ, notError_);
                // DefaultJSONParser
                mw.visitVarInsn(ALOAD, 1);
                mw.visitVarInsn(ILOAD, context.var("token"));
                mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "throwException", "(I)V");
                mw.visitLabel(notError_);
                Label quickElse_ = new Label(), quickEnd_ = new Label();
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "getCurrent", "()C");
                mw.visitVarInsn(BIPUSH, '[');
                mw.visitJumpInsn(IF_ICMPNE, quickElse_);
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
                mw.visitInsn(POP);
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitLdcInsn(JSONToken.LBRACKET);
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
                mw.visitJumpInsn(GOTO, quickEnd_);
                mw.visitLabel(quickElse_);
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitLdcInsn(JSONToken.LBRACKET);
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "nextToken", "(I)V");
                mw.visitLabel(quickEnd_);
                _newCollection(mw, fieldClass, i, false);
                mw.visitInsn(DUP);
                mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
                _getCollectionFieldItemDeser(context, mw, fieldInfo, itemClass);
                mw.visitVarInsn(ALOAD, 1);
                mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(itemClass)));
                mw.visitVarInsn(ALOAD, 3);
                mw.visitMethodInsn(INVOKESTATIC, type(JavaBeanDeserializer.class), "parseArray", //
                "(Ljava/util/Collection;" + //
                desc(ObjectDeserializer.class) + "L" + DefaultJSONParser + //
                ";" + "Ljava/lang/reflect/Type;Ljava/lang/Object;)V");
            }
        } else if (fieldClass.isArray()) {
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            mw.visitLdcInsn(com.alibaba.fastjson.parser.JSONToken.LBRACKET);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "nextToken", "(I)V");
            mw.visitVarInsn(ALOAD, Context.parser);
            mw.visitVarInsn(ALOAD, 0);
            mw.visitLdcInsn(i);
            mw.visitMethodInsn(INVOKEVIRTUAL, type(JavaBeanDeserializer.class), "getFieldType", "(I)Ljava/lang/reflect/Type;");
            mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "parseObject", "(Ljava/lang/reflect/Type;)Ljava/lang/Object;");
            // cast
            mw.visitTypeInsn(CHECKCAST, type(fieldClass));
            mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
        } else {
            Label objElseIf_ = new Label();
            Label objEndIf_ = new Label();
            if (fieldClass == java.util.Date.class) {
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "getCurrent", "()C");
                mw.visitLdcInsn((int) '1');
                mw.visitJumpInsn(IF_ICMPNE, objElseIf_);
                mw.visitTypeInsn(NEW, type(java.util.Date.class));
                mw.visitInsn(DUP);
                mw.visitVarInsn(ALOAD, context.var("lexer"));
                mw.visitVarInsn(BIPUSH, seperator);
                mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "scanLong", "(C)J");
                mw.visitMethodInsn(INVOKESPECIAL, type(java.util.Date.class), "<init>", "(J)V");
                mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm"));
                mw.visitJumpInsn(GOTO, objEndIf_);
            }
            mw.visitLabel(objElseIf_);
            _quickNextToken(context, mw, JSONToken.LBRACKET);
            _deserObject(context, mw, fieldInfo, fieldClass, i);
            mw.visitVarInsn(ALOAD, 0);
            mw.visitVarInsn(ALOAD, context.var("lexer"));
            if (!last) {
                mw.visitLdcInsn(JSONToken.COMMA);
            } else {
                mw.visitLdcInsn(JSONToken.RBRACKET);
            }
            //
            mw.visitMethodInsn(//
            INVOKESPECIAL, //
            type(JavaBeanDeserializer.class), "check", "(" + desc(JSONLexer.class) + "I)V");
            mw.visitLabel(objEndIf_);
            continue;
        }
    }
    _batchSet(context, mw, false);
    Label quickElse_ = new Label(), quickElseIf_ = new Label(), quickElseIfEOI_ = new Label(), quickEnd_ = new Label();
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "getCurrent", "()C");
    mw.visitInsn(DUP);
    mw.visitVarInsn(ISTORE, context.var("ch"));
    mw.visitVarInsn(BIPUSH, ',');
    mw.visitJumpInsn(IF_ICMPNE, quickElseIf_);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
    mw.visitInsn(POP);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(JSONToken.COMMA);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
    mw.visitJumpInsn(GOTO, quickEnd_);
    mw.visitLabel(quickElseIf_);
    mw.visitVarInsn(ILOAD, context.var("ch"));
    mw.visitVarInsn(BIPUSH, ']');
    mw.visitJumpInsn(IF_ICMPNE, quickElseIfEOI_);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
    mw.visitInsn(POP);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(JSONToken.RBRACKET);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
    mw.visitJumpInsn(GOTO, quickEnd_);
    mw.visitLabel(quickElseIfEOI_);
    mw.visitVarInsn(ILOAD, context.var("ch"));
    mw.visitVarInsn(BIPUSH, (char) JSONLexer.EOI);
    mw.visitJumpInsn(IF_ICMPNE, quickElse_);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "next", "()C");
    mw.visitInsn(POP);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(JSONToken.EOF);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "setToken", "(I)V");
    mw.visitJumpInsn(GOTO, quickEnd_);
    mw.visitLabel(quickElse_);
    mw.visitVarInsn(ALOAD, context.var("lexer"));
    mw.visitLdcInsn(JSONToken.COMMA);
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "nextToken", "(I)V");
    mw.visitLabel(quickEnd_);
    mw.visitVarInsn(ALOAD, context.var("instance"));
    mw.visitInsn(ARETURN);
    mw.visitMaxs(5, context.variantIndex);
    mw.visitEnd();
}
Also used : MethodWriter(com.alibaba.fastjson.asm.MethodWriter) Label(com.alibaba.fastjson.asm.Label) ArrayList(java.util.ArrayList) SymbolTable(com.alibaba.fastjson.parser.SymbolTable) TypeUtils(com.alibaba.fastjson.util.TypeUtils) MethodVisitor(com.alibaba.fastjson.asm.MethodVisitor) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Example 19 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class DeserializerGen method genBatchSet.

private void genBatchSet(FieldInfo[] fieldList, boolean flag) throws IOException {
    for (int i = 0, size = fieldList.length; i < size; ++i) {
        FieldInfo fieldInfo = fieldList[i];
        String varName = "_asm_flag_" + (i / 32);
        if (flag) {
            print("if ((");
            print(varName);
            print(" & ");
            print(Integer.toString(1 << i));
            print(") != 0) {");
            println();
            incrementIndent();
        }
        if (fieldInfo.method != null) {
            print("\tinstance.");
            print(fieldInfo.method.getName());
            print("(");
            printFieldVarName(fieldInfo);
            println(");");
        } else {
            print("\tinstance.");
            print(fieldInfo.field.getName());
            print(" = ");
            printFieldVarName(fieldInfo);
            println(";");
        }
        if (flag) {
            decrementIndent();
            println();
            println("}");
        }
    }
}
Also used : FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Example 20 with FieldInfo

use of com.alibaba.fastjson.util.FieldInfo in project fastjson by alibaba.

the class DeserializerGen method genConstructor.

protected void genConstructor() throws IOException {
    for (int i = 0, size = beanInfo.fields.length; i < size; ++i) {
        FieldInfo fieldInfo = beanInfo.fields[i];
        print("private char[] ");
        printFieldPrefix(fieldInfo);
        print(" = \"\\\"");
        print(fieldInfo.name);
        print("\\\":\".toCharArray();");
        println();
    }
    println();
    boolean fieldDeserFlag = false;
    for (int i = 0, size = beanInfo.fields.length; i < size; ++i) {
        FieldInfo fieldInfo = beanInfo.fields[i];
        Class<?> fieldClass = fieldInfo.fieldClass;
        if (fieldClass.isPrimitive()) {
            continue;
        }
        if (fieldClass.isEnum()) {
            continue;
        }
        print("private ObjectDeserializer ");
        if (Collection.class.isAssignableFrom(fieldClass)) {
            printListFieldItemDeser(fieldInfo);
        } else {
            printFieldDeser(fieldInfo);
        }
        println(";");
        fieldDeserFlag = true;
        if (Collection.class.isAssignableFrom(fieldClass)) {
            print("private Type ");
            printListFieldItemType(fieldInfo);
            print(" = ");
            Class<?> fieldItemClass = TypeUtils.getCollectionItemClass(fieldInfo.fieldType);
            printClassName(fieldItemClass);
            println(".class;");
        }
    }
    if (fieldDeserFlag) {
        println();
    }
    // constructor
    print("public ");
    print(genClassName);
    print(" (ParserConfig config, Class clazz) {");
    incrementIndent();
    println();
    println("super(config, clazz);");
    decrementIndent();
    println();
    print("}");
    println();
}
Also used : FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Aggregations

FieldInfo (com.alibaba.fastjson.util.FieldInfo)20 JSONException (com.alibaba.fastjson.JSONException)7 Label (com.alibaba.fastjson.asm.Label)6 JSONObject (com.alibaba.fastjson.JSONObject)4 JSONField (com.alibaba.fastjson.annotation.JSONField)4 MethodVisitor (com.alibaba.fastjson.asm.MethodVisitor)4 MethodWriter (com.alibaba.fastjson.asm.MethodWriter)4 Type (java.lang.reflect.Type)4 JSON (com.alibaba.fastjson.JSON)3 JSONType (com.alibaba.fastjson.annotation.JSONType)3 ParseContext (com.alibaba.fastjson.parser.ParseContext)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 Collection (java.util.Collection)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 FieldWriter (com.alibaba.fastjson.asm.FieldWriter)2 JavaBeanInfo (com.alibaba.fastjson.util.JavaBeanInfo)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2