Search in sources :

Example 6 with Label

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

the class ASMSerializerFactory method _writeObject.

private void _writeObject(MethodVisitor mw, FieldInfo fieldInfo, Context context, Label _end) {
    String format = fieldInfo.getFormat();
    Class<?> fieldClass = fieldInfo.fieldClass;
    Label notNull_ = new Label();
    // if (obj == null)
    if (context.writeDirect) {
        mw.visitVarInsn(ALOAD, context.var("object"));
    } else {
        mw.visitVarInsn(ALOAD, Context.processValue);
    }
    mw.visitInsn(DUP);
    mw.visitVarInsn(ASTORE, context.var("object"));
    mw.visitJumpInsn(IFNONNULL, notNull_);
    _if_write_null(mw, fieldInfo, context);
    mw.visitJumpInsn(GOTO, _end);
    mw.visitLabel(notNull_);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitVarInsn(ILOAD, context.var("seperator"));
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
    _writeFieldName(mw, context);
    Label classIfEnd_ = new Label(), classIfElse_ = new Label();
    if (//
    Modifier.isPublic(fieldClass.getModifiers()) && //
    !ParserConfig.isPrimitive2(fieldClass)) {
        mw.visitVarInsn(ALOAD, context.var("object"));
        mw.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
        mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(fieldClass)));
        mw.visitJumpInsn(IF_ACMPNE, classIfElse_);
        _getFieldSer(context, mw, fieldInfo);
        mw.visitVarInsn(ASTORE, context.var("fied_ser"));
        Label instanceOfElse_ = new Label(), instanceOfEnd_ = new Label();
        mw.visitVarInsn(ALOAD, context.var("fied_ser"));
        mw.visitTypeInsn(INSTANCEOF, JavaBeanSerializer);
        mw.visitJumpInsn(IFEQ, instanceOfElse_);
        boolean fieldBeanToArray = (fieldInfo.serialzeFeatures & SerializerFeature.BeanToArray.mask) != 0;
        String writeMethodName;
        if (context.nonContext && context.writeDirect) {
            writeMethodName = fieldBeanToArray ? "writeAsArrayNonContext" : "writeDirectNonContext";
        } else {
            writeMethodName = fieldBeanToArray ? "writeAsArray" : "write";
        }
        mw.visitVarInsn(ALOAD, context.var("fied_ser"));
        // cast
        mw.visitTypeInsn(CHECKCAST, JavaBeanSerializer);
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, context.var("object"));
        mw.visitVarInsn(ALOAD, Context.fieldName);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_fieldType", "Ljava/lang/reflect/Type;");
        // 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("fied_ser"));
        mw.visitVarInsn(ALOAD, Context.serializer);
        mw.visitVarInsn(ALOAD, context.var("object"));
        mw.visitVarInsn(ALOAD, Context.fieldName);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_fieldType", "Ljava/lang/reflect/Type;");
        // 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, classIfEnd_);
    }
    mw.visitLabel(classIfElse_);
    mw.visitVarInsn(ALOAD, Context.serializer);
    if (context.writeDirect) {
        mw.visitVarInsn(ALOAD, context.var("object"));
    } else {
        mw.visitVarInsn(ALOAD, Context.processValue);
    }
    if (format != null) {
        mw.visitLdcInsn(format);
        mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeWithFormat", "(Ljava/lang/Object;Ljava/lang/String;)V");
    } else {
        mw.visitVarInsn(ALOAD, Context.fieldName);
        if (//
        fieldInfo.fieldType instanceof Class<?> && ((Class<?>) fieldInfo.fieldType).isPrimitive()) {
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeWithFieldName", "(Ljava/lang/Object;Ljava/lang/Object;)V");
        } else {
            if (fieldInfo.fieldClass == String.class) {
                mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(String.class)));
            } else {
                mw.visitVarInsn(ALOAD, 0);
                mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_fieldType", "Ljava/lang/reflect/Type;");
            }
            mw.visitLdcInsn(fieldInfo.serialzeFeatures);
            mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeWithFieldName", "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
        }
    }
    mw.visitLabel(classIfEnd_);
    _seperator(mw, context);
}
Also used : Label(com.alibaba.fastjson.asm.Label)

Example 7 with Label

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

the class ASMSerializerFactory method _getFieldSer.

private void _getFieldSer(Context context, MethodVisitor mw, FieldInfo fieldInfo) {
    Label notNull_ = new Label();
    mw.visitVarInsn(ALOAD, 0);
    mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_ser_", ObjectSerializer_desc);
    mw.visitJumpInsn(IFNONNULL, notNull_);
    // this
    mw.visitVarInsn(ALOAD, 0);
    mw.visitVarInsn(ALOAD, Context.serializer);
    mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(fieldInfo.fieldClass)));
    mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "getObjectWriter", "(Ljava/lang/Class;)" + ObjectSerializer_desc);
    mw.visitFieldInsn(PUTFIELD, context.className, fieldInfo.name + "_asm_ser_", ObjectSerializer_desc);
    mw.visitLabel(notNull_);
    mw.visitVarInsn(ALOAD, 0);
    mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_ser_", ObjectSerializer_desc);
}
Also used : Label(com.alibaba.fastjson.asm.Label)

Example 8 with Label

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

the class ASMSerializerFactory method _processKey.

private void _processKey(MethodVisitor mw, FieldInfo property, Context context) {
    Label _else_processKey = new Label();
    mw.visitVarInsn(ILOAD, context.var("hasNameFilters"));
    mw.visitJumpInsn(IFEQ, _else_processKey);
    Class<?> propertyClass = property.fieldClass;
    mw.visitVarInsn(ALOAD, 0);
    mw.visitVarInsn(ALOAD, Context.serializer);
    mw.visitVarInsn(ALOAD, Context.obj);
    mw.visitVarInsn(ALOAD, Context.fieldName);
    if (propertyClass == byte.class) {
        mw.visitVarInsn(ILOAD, context.var("byte"));
        mw.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
    } else if (propertyClass == short.class) {
        mw.visitVarInsn(ILOAD, context.var("short"));
        mw.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
    } else if (propertyClass == int.class) {
        mw.visitVarInsn(ILOAD, context.var("int"));
        mw.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
    } else if (propertyClass == char.class) {
        mw.visitVarInsn(ILOAD, context.var("char"));
        mw.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
    } else if (propertyClass == long.class) {
        mw.visitVarInsn(LLOAD, context.var("long", 2));
        mw.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
    } else if (propertyClass == float.class) {
        mw.visitVarInsn(FLOAD, context.var("float"));
        mw.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
    } else if (propertyClass == double.class) {
        mw.visitVarInsn(DLOAD, context.var("double", 2));
        mw.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
    } else if (propertyClass == boolean.class) {
        mw.visitVarInsn(ILOAD, context.var("boolean"));
        mw.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
    } else if (propertyClass == BigDecimal.class) {
        mw.visitVarInsn(ALOAD, context.var("decimal"));
    } else if (propertyClass == String.class) {
        mw.visitVarInsn(ALOAD, context.var("string"));
    } else if (propertyClass.isEnum()) {
        mw.visitVarInsn(ALOAD, context.var("enum"));
    } else if (List.class.isAssignableFrom(propertyClass)) {
        mw.visitVarInsn(ALOAD, context.var("list"));
    } else {
        mw.visitVarInsn(ALOAD, context.var("object"));
    }
    mw.visitMethodInsn(INVOKEVIRTUAL, JavaBeanSerializer, "processKey", "(L" + JSONSerializer + ";Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;");
    mw.visitVarInsn(ASTORE, Context.fieldName);
    mw.visitLabel(_else_processKey);
}
Also used : Label(com.alibaba.fastjson.asm.Label) List(java.util.List)

Example 9 with Label

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

the class ASMSerializerFactory method _decimal.

private void _decimal(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) {
    Label end_ = new Label();
    _nameApply(mw, property, context, end_);
    _get(mw, context, property);
    mw.visitVarInsn(ASTORE, context.var("decimal"));
    _filters(mw, property, context, end_);
    Label if_ = new Label();
    Label else_ = new Label();
    Label endIf_ = new Label();
    mw.visitLabel(if_);
    // if (decimalValue == null) {
    mw.visitVarInsn(ALOAD, context.var("decimal"));
    mw.visitJumpInsn(IFNONNULL, else_);
    _if_write_null(mw, property, context);
    mw.visitJumpInsn(GOTO, endIf_);
    // else { out.writeFieldValue(seperator, fieldName, fieldValue)
    mw.visitLabel(else_);
    mw.visitVarInsn(ALOAD, context.var("out"));
    mw.visitVarInsn(ILOAD, context.var("seperator"));
    mw.visitVarInsn(ALOAD, Context.fieldName);
    mw.visitVarInsn(ALOAD, context.var("decimal"));
    mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeFieldValue", "(CLjava/lang/String;Ljava/math/BigDecimal;)V");
    _seperator(mw, context);
    mw.visitJumpInsn(GOTO, endIf_);
    mw.visitLabel(endIf_);
    mw.visitLabel(end_);
}
Also used : Label(com.alibaba.fastjson.asm.Label)

Example 10 with Label

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

the class ASMSerializerFactory method _string.

private void _string(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) {
    Label end_ = new Label();
    _nameApply(mw, property, context, end_);
    _get(mw, context, property);
    mw.visitVarInsn(ASTORE, context.var("string"));
    _filters(mw, property, context, end_);
    Label else_ = new Label();
    Label endIf_ = new Label();
    // if (value == null) {
    mw.visitVarInsn(ALOAD, context.var("string"));
    mw.visitJumpInsn(IFNONNULL, else_);
    _if_write_null(mw, property, context);
    mw.visitJumpInsn(GOTO, endIf_);
    // else { out.writeFieldValue(seperator, fieldName, fieldValue)
    mw.visitLabel(else_);
    if (context.writeDirect) {
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitVarInsn(ILOAD, context.var("seperator"));
        mw.visitVarInsn(ALOAD, Context.fieldName);
        mw.visitVarInsn(ALOAD, context.var("string"));
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeFieldValueStringWithDoubleQuoteCheck", "(CLjava/lang/String;Ljava/lang/String;)V");
    } else {
        mw.visitVarInsn(ALOAD, context.var("out"));
        mw.visitVarInsn(ILOAD, context.var("seperator"));
        mw.visitVarInsn(ALOAD, Context.fieldName);
        mw.visitVarInsn(ALOAD, context.var("string"));
        mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeFieldValue", "(CLjava/lang/String;Ljava/lang/String;)V");
    }
    _seperator(mw, context);
    mw.visitLabel(endIf_);
    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