Search in sources :

Example 11 with Label

use of com.alibaba.fastjson.asm.Label in project fastjson by alibaba.

the class ASMSerializerFactory method _notWriteDefault.

private void _notWriteDefault(MethodVisitor mw, FieldInfo property, Context context, Label _end) {
    if (context.writeDirect) {
        return;
    }
    Label elseLabel = new Label();
    mw.visitVarInsn(ILOAD, context.var("notWriteDefaultValue"));
    mw.visitJumpInsn(IFEQ, elseLabel);
    Class<?> propertyClass = property.fieldClass;
    if (propertyClass == boolean.class) {
        mw.visitVarInsn(ILOAD, context.var("boolean"));
        mw.visitJumpInsn(IFEQ, _end);
    } else if (propertyClass == byte.class) {
        mw.visitVarInsn(ILOAD, context.var("byte"));
        mw.visitJumpInsn(IFEQ, _end);
    } else if (propertyClass == short.class) {
        mw.visitVarInsn(ILOAD, context.var("short"));
        mw.visitJumpInsn(IFEQ, _end);
    } else if (propertyClass == int.class) {
        mw.visitVarInsn(ILOAD, context.var("int"));
        mw.visitJumpInsn(IFEQ, _end);
    } else if (propertyClass == long.class) {
        mw.visitVarInsn(LLOAD, context.var("long"));
        mw.visitInsn(LCONST_0);
        mw.visitInsn(LCMP);
        mw.visitJumpInsn(IFEQ, _end);
    } else if (propertyClass == float.class) {
        mw.visitVarInsn(FLOAD, context.var("float"));
        mw.visitInsn(FCONST_0);
        mw.visitInsn(FCMPL);
        mw.visitJumpInsn(IFEQ, _end);
    } else if (propertyClass == double.class) {
        mw.visitVarInsn(DLOAD, context.var("double"));
        mw.visitInsn(DCONST_0);
        mw.visitInsn(DCMPL);
        mw.visitJumpInsn(IFEQ, _end);
    }
    mw.visitLabel(elseLabel);
}
Also used : Label(com.alibaba.fastjson.asm.Label)

Example 12 with Label

use of com.alibaba.fastjson.asm.Label in project fastjson by alibaba.

the class ASMSerializerFactory method _long.

private void _long(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) {
    Label end_ = new Label();
    _nameApply(mw, property, context, end_);
    _get(mw, context, property);
    mw.visitVarInsn(LSTORE, context.var("long", 2));
    _filters(mw, property, context, end_);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitVarInsn(ILOAD, context.var("seperator"));
    mw.visitVarInsn(ALOAD, Context.fieldName);
    mw.visitVarInsn(LLOAD, context.var("long", 2));
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeFieldValue", "(CLjava/lang/String;J)V");
    _seperator(mw, context);
    mw.visitLabel(end_);
}
Also used : Label(com.alibaba.fastjson.asm.Label)

Example 13 with Label

use of com.alibaba.fastjson.asm.Label in project fastjson by alibaba.

the class ASMSerializerFactory method _if_write_null.

private void _if_write_null(MethodVisitor mw, FieldInfo fieldInfo, Context context) {
    Class<?> propertyClass = fieldInfo.fieldClass;
    Label _if = new Label();
    Label _else = new Label();
    Label _write_null = new Label();
    Label _end_if = new Label();
    mw.visitLabel(_if);
    JSONField annotation = fieldInfo.getAnnotation();
    int features = 0;
    if (annotation != null) {
        features = SerializerFeature.of(annotation.serialzeFeatures());
        ;
    }
    if ((features & SerializerFeature.WRITE_MAP_NULL_FEATURES) == 0) {
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitLdcInsn(SerializerFeature.WRITE_MAP_NULL_FEATURES);
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z");
        mw.visitJumpInsn(IFEQ, _else);
    }
    mw.visitLabel(_write_null);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitVarInsn(ILOAD, context.var("seperator"));
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
    _writeFieldName(mw, context);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitLdcInsn(features);
    if (propertyClass == String.class || propertyClass == Character.class) {
        mw.visitLdcInsn(SerializerFeature.WriteNullStringAsEmpty.mask);
    } else if (Number.class.isAssignableFrom(propertyClass)) {
        mw.visitLdcInsn(SerializerFeature.WriteNullNumberAsZero.mask);
    } else if (propertyClass == Boolean.class) {
        mw.visitLdcInsn(SerializerFeature.WriteNullBooleanAsFalse.mask);
    } else if (Collection.class.isAssignableFrom(propertyClass) || propertyClass.isArray()) {
        mw.visitLdcInsn(SerializerFeature.WriteNullListAsEmpty.mask);
    } else {
        mw.visitLdcInsn(0);
    }
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeNull", "(II)V");
    // seperator = ',';
    _seperator(mw, context);
    mw.visitJumpInsn(GOTO, _end_if);
    mw.visitLabel(_else);
    mw.visitLabel(_end_if);
}
Also used : Label(com.alibaba.fastjson.asm.Label) JSONField(com.alibaba.fastjson.annotation.JSONField)

Example 14 with Label

use of com.alibaba.fastjson.asm.Label in project fastjson by alibaba.

the class ASMSerializerFactory method _list.

private void _list(Class<?> clazz, MethodVisitor mw, FieldInfo fieldInfo, Context context) {
    Type propertyType = fieldInfo.fieldType;
    Type elementType = TypeUtils.getCollectionItemType(propertyType);
    Class<?> elementClass = null;
    if (elementType instanceof Class<?>) {
        elementClass = (Class<?>) elementType;
    }
    if (//
    elementClass == Object.class || elementClass == Serializable.class) {
        elementClass = null;
    }
    Label end_ = new Label(), else_ = new Label(), endIf_ = new Label();
    _nameApply(mw, fieldInfo, context, end_);
    _get(mw, context, fieldInfo);
    // cast
    mw.visitTypeInsn(CHECKCAST, "java/util/List");
    mw.visitVarInsn(ASTORE, context.var("list"));
    _filters(mw, fieldInfo, context, end_);
    mw.visitVarInsn(ALOAD, context.var("list"));
    mw.visitJumpInsn(IFNONNULL, else_);
    _if_write_null(mw, fieldInfo, context);
    mw.visitJumpInsn(GOTO, endIf_);
    // else {
    mw.visitLabel(else_);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitVarInsn(ILOAD, context.var("seperator"));
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
    _writeFieldName(mw, context);
    //
    mw.visitVarInsn(ALOAD, context.var("list"));
    mw.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I");
    mw.visitVarInsn(ISTORE, context.var("size"));
    Label _else_3 = new Label();
    Label _end_if_3 = new Label();
    mw.visitVarInsn(ILOAD, context.var("size"));
    mw.visitInsn(ICONST_0);
    mw.visitJumpInsn(IF_ICMPNE, _else_3);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitLdcInsn("[]");
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(Ljava/lang/String;)V");
    mw.visitJumpInsn(GOTO, _end_if_3);
    mw.visitLabel(_else_3);
    if (!context.nonContext) {
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, context.var("list"));
        mw.visitVarInsn(ALOAD, Context.fieldName);
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "setContext", "(Ljava/lang/Object;Ljava/lang/Object;)V");
    }
    if (//
    elementType == String.class && context.writeDirect) {
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitVarInsn(ALOAD, context.var("list"));
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(Ljava/util/List;)V");
    } else {
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitVarInsn(BIPUSH, '[');
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
        Label for_ = new Label(), forFirst_ = new Label(), forEnd_ = new Label();
        mw.visitInsn(ICONST_0);
        mw.visitVarInsn(ISTORE, context.var("i"));
        // for (; i < list.size() -1; ++i) {
        mw.visitLabel(for_);
        mw.visitVarInsn(ILOAD, context.var("i"));
        mw.visitVarInsn(ILOAD, context.var("size"));
        // i < list.size - 1
        mw.visitJumpInsn(IF_ICMPGE, forEnd_);
        mw.visitVarInsn(ILOAD, context.var("i"));
        // i < list.size - 1
        mw.visitJumpInsn(IFEQ, forFirst_);
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitVarInsn(BIPUSH, ',');
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
        mw.visitLabel(forFirst_);
        mw.visitVarInsn(ALOAD, context.var("list"));
        mw.visitVarInsn(ILOAD, context.var("i"));
        mw.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;");
        mw.visitVarInsn(ASTORE, context.var("list_item"));
        Label forItemNullEnd_ = new Label(), forItemNullElse_ = new Label();
        mw.visitVarInsn(ALOAD, context.var("list_item"));
        mw.visitJumpInsn(IFNONNULL, forItemNullElse_);
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeNull", "()V");
        mw.visitJumpInsn(GOTO, forItemNullEnd_);
        mw.visitLabel(forItemNullElse_);
        Label forItemClassIfEnd_ = new Label(), forItemClassIfElse_ = new Label();
        if (elementClass != null && Modifier.isPublic(elementClass.getModifiers())) {
            mw.visitVarInsn(ALOAD, context.var("list_item"));
            mw.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
            mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(elementClass)));
            mw.visitJumpInsn(IF_ACMPNE, forItemClassIfElse_);
            _getListFieldItemSer(context, mw, fieldInfo, elementClass);
            // mw.visitInsn(DUP);
            mw.visitVarInsn(ASTORE, context.var("list_item_desc"));
            Label instanceOfElse_ = new Label(), instanceOfEnd_ = new Label();
            if (context.writeDirect) {
                String writeMethodName = //
                context.nonContext && context.writeDirect ? //
                "writeDirectNonContext" : "write";
                mw.visitVarInsn(ALOAD, context.var("list_item_desc"));
                mw.visitTypeInsn(INSTANCEOF, JavaBeanSerializer);
                mw.visitJumpInsn(IFEQ, instanceOfElse_);
                mw.visitVarInsn(ALOAD, context.var("list_item_desc"));
                // cast
                mw.visitTypeInsn(CHECKCAST, JavaBeanSerializer);
                mw.visitVarInsn(ALOAD, Context.serializer);
                // object
                mw.visitVarInsn(ALOAD, context.var("list_item"));
                if (context.nonContext) {
                    // fieldName
                    mw.visitInsn(ACONST_NULL);
                } else {
                    mw.visitVarInsn(ILOAD, context.var("i"));
                    mw.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
                }
                // fieldType
                mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(elementClass)));
                // features
                mw.visitLdcInsn(fieldInfo.serialzeFeatures);
                //
                mw.visitMethodInsn(//
                INVOKEVIRTUAL, //
                JavaBeanSerializer, //
                writeMethodName, "(L" + JSONSerializer + ";Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
                mw.visitJumpInsn(GOTO, instanceOfEnd_);
                mw.visitLabel(instanceOfElse_);
            }
            mw.visitVarInsn(ALOAD, context.var("list_item_desc"));
            mw.visitVarInsn(ALOAD, Context.serializer);
            // object
            mw.visitVarInsn(ALOAD, context.var("list_item"));
            if (context.nonContext) {
                // fieldName
                mw.visitInsn(ACONST_NULL);
            } else {
                mw.visitVarInsn(ILOAD, context.var("i"));
                mw.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
            }
            // fieldType
            mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(elementClass)));
            // features
            mw.visitLdcInsn(fieldInfo.serialzeFeatures);
            //
            mw.visitMethodInsn(//
            INVOKEINTERFACE, //
            ObjectSerializer, //
            "write", "(L" + JSONSerializer + ";Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
            mw.visitLabel(instanceOfEnd_);
            mw.visitJumpInsn(GOTO, forItemClassIfEnd_);
        }
        mw.visitLabel(forItemClassIfElse_);
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, context.var("list_item"));
        if (context.nonContext) {
            mw.visitInsn(ACONST_NULL);
        } else {
            mw.visitVarInsn(ILOAD, context.var("i"));
            mw.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
        }
        if (elementClass != null && Modifier.isPublic(elementClass.getModifiers())) {
            mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc((Class<?>) elementType)));
            mw.visitLdcInsn(fieldInfo.serialzeFeatures);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeWithFieldName", "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
        } else {
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeWithFieldName", "(Ljava/lang/Object;Ljava/lang/Object;)V");
        }
        mw.visitLabel(forItemClassIfEnd_);
        mw.visitLabel(forItemNullEnd_);
        mw.visitIincInsn(context.var("i"), 1);
        mw.visitJumpInsn(GOTO, for_);
        mw.visitLabel(forEnd_);
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitVarInsn(BIPUSH, ']');
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
    }
    {
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "popContext", "()V");
    }
    mw.visitLabel(_end_if_3);
    _seperator(mw, context);
    mw.visitLabel(endIf_);
    mw.visitLabel(end_);
}
Also used : JSONType(com.alibaba.fastjson.annotation.JSONType) Serializable(java.io.Serializable) Label(com.alibaba.fastjson.asm.Label)

Example 15 with Label

use of com.alibaba.fastjson.asm.Label in project fastjson by alibaba.

the class ASMSerializerFactory method _double.

private void _double(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) {
    Label end_ = new Label();
    _nameApply(mw, property, context, end_);
    _get(mw, context, property);
    mw.visitVarInsn(DSTORE, context.var("double", 2));
    _filters(mw, property, context, end_);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitVarInsn(ILOAD, context.var("seperator"));
    mw.visitVarInsn(ALOAD, Context.fieldName);
    mw.visitVarInsn(DLOAD, context.var("double", 2));
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeFieldValue", "(CLjava/lang/String;D)V");
    _seperator(mw, context);
    mw.visitLabel(end_);
}
Also used : Label(com.alibaba.fastjson.asm.Label)

Aggregations

Label (com.alibaba.fastjson.asm.Label)30 FieldInfo (com.alibaba.fastjson.util.FieldInfo)6 ParseContext (com.alibaba.fastjson.parser.ParseContext)4 JSONType (com.alibaba.fastjson.annotation.JSONType)3 MethodVisitor (com.alibaba.fastjson.asm.MethodVisitor)3 MethodWriter (com.alibaba.fastjson.asm.MethodWriter)3 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 List (java.util.List)2 JSONException (com.alibaba.fastjson.JSONException)1 JSONField (com.alibaba.fastjson.annotation.JSONField)1 ClassWriter (com.alibaba.fastjson.asm.ClassWriter)1 FieldWriter (com.alibaba.fastjson.asm.FieldWriter)1 ResolveTask (com.alibaba.fastjson.parser.DefaultJSONParser.ResolveTask)1 SymbolTable (com.alibaba.fastjson.parser.SymbolTable)1 ASMUtils (com.alibaba.fastjson.util.ASMUtils)1 JavaBeanInfo (com.alibaba.fastjson.util.JavaBeanInfo)1 TypeUtils (com.alibaba.fastjson.util.TypeUtils)1 Serializable (java.io.Serializable)1