private void _batchSet(Context context, MethodVisitor mw, boolean flag) { for (int i = 0, size = context.fieldInfoList.length; i < size; ++i) { Label notSet_ = new Label(); if (flag) { _isFlag(mw, context, i, notSet_); } FieldInfo fieldInfo = context.fieldInfoList[i]; _loadAndSet(context, mw, fieldInfo); if (flag) { mw.visitLabel(notSet_); } } }
private void _getCollectionFieldItemDeser(Context context, MethodVisitor mw, FieldInfo fieldInfo, Class<?> itemType) { Label notNull_ = new Label(); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_list_item_deser__", desc(ObjectDeserializer.class)); mw.visitJumpInsn(IFNONNULL, notNull_); mw.visitVarInsn(ALOAD, 0); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getConfig", "()" + desc(ParserConfig.class)); mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(itemType))); mw.visitMethodInsn(INVOKEVIRTUAL, type(ParserConfig.class), "getDeserializer", "(Ljava/lang/reflect/Type;)" + desc(ObjectDeserializer.class)); mw.visitFieldInsn(PUTFIELD, context.className, fieldInfo.name + "_asm_list_item_deser__", desc(ObjectDeserializer.class)); mw.visitLabel(notNull_); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_list_item_deser__", desc(ObjectDeserializer.class)); }
private void _getFieldDeser(Context context, MethodVisitor mw, FieldInfo fieldInfo) { Label notNull_ = new Label(); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_deser__", desc(ObjectDeserializer.class)); mw.visitJumpInsn(IFNONNULL, notNull_); mw.visitVarInsn(ALOAD, 0); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getConfig", "()" + desc(ParserConfig.class)); mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(fieldInfo.fieldClass))); mw.visitMethodInsn(INVOKEVIRTUAL, type(ParserConfig.class), "getDeserializer", "(Ljava/lang/reflect/Type;)" + desc(ObjectDeserializer.class)); mw.visitFieldInsn(PUTFIELD, context.className, fieldInfo.name + "_asm_deser__", desc(ObjectDeserializer.class)); mw.visitLabel(notNull_); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_deser__", desc(ObjectDeserializer.class)); }
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, "com/alibaba/fastjson/serializer/SerializeWriter", "writeFieldValue", "(CLjava/lang/String;D)V"); _seperator(mw, context); mw.visitLabel(_end); }
private void _int(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) { Label _end = new Label(); _nameApply(mw, property, context, _end); _get(mw, context, property); mw.visitVarInsn(ISTORE, context.var("int")); _filters(mw, property, context, _end); mw.visitVarInsn(ALOAD, context.var("out")); mw.visitVarInsn(ILOAD, context.var("seperator")); mw.visitVarInsn(ALOAD, context.fieldName()); mw.visitVarInsn(ILOAD, context.var("int")); mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/SerializeWriter", "writeFieldValue", "(CLjava/lang/String;I)V"); _seperator(mw, context); mw.visitLabel(_end); }
public FieldSerializer(FieldInfo fieldInfo){ super(); this.fieldInfo = fieldInfo; fieldInfo.setAccessible(true); this.double_quoted_fieldPrefix = '"' + fieldInfo.getName() + "\":"; this.single_quoted_fieldPrefix = '\'' + fieldInfo.getName() + "\':"; this.un_quoted_fieldPrefix = fieldInfo.getName() + ":"; JSONField annotation = fieldInfo.getAnnotation(JSONField.class); if (annotation != null) { for (SerializerFeature feature : annotation.serialzeFeatures()) { if (feature == SerializerFeature.WriteMapNullValue) { writeNull = true; } } } }
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_); }
private void _float(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) { Label end_ = new Label(); _nameApply(mw, property, context, end_); _get(mw, context, property); mw.visitVarInsn(FSTORE, context.var("float")); _filters(mw, property, context, end_); mw.visitVarInsn(ALOAD, context.var("out")); mw.visitVarInsn(ILOAD, context.var("seperator")); mw.visitVarInsn(ALOAD, Context.fieldName); mw.visitVarInsn(FLOAD, context.var("float")); mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeFieldValue", "(CLjava/lang/String;F)V"); _seperator(mw, context); mw.visitLabel(end_); }
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_); }
private void _byte(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) { Label _end = new Label(); _nameApply(mw, property, context, _end); _get(mw, context, property); mw.visitVarInsn(ISTORE, context.var("byte")); _filters(mw, property, context, _end); mw.visitVarInsn(ALOAD, context.var("out")); mw.visitVarInsn(ILOAD, context.var("seperator")); mw.visitVarInsn(ALOAD, context.fieldName()); mw.visitVarInsn(ILOAD, context.var("byte")); mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/SerializeWriter", "writeFieldValue", "(CLjava/lang/String;I)V"); _seperator(mw, context); mw.visitLabel(_end); }
private void _filters(MethodVisitor mw, FieldInfo property, Context context, Label _end) { if (property.fieldTransient) { mw.visitVarInsn(ALOAD, context.var("out")); mw.visitLdcInsn(SerializerFeature.SkipTransientField.mask); mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z"); mw.visitJumpInsn(IFNE, _end); } _notWriteDefault(mw, property, context, _end); if (context.writeDirect) { return; } _apply(mw, property, context); mw.visitJumpInsn(IFEQ, _end); _processKey(mw, property, context); _processValue(mw, property, context, _end); }
private void _nameApply(MethodVisitor mw, FieldInfo property, Context context, Label _end) { if (!context.writeDirect) { mw.visitVarInsn(ALOAD, 0); mw.visitVarInsn(ALOAD, Context.serializer); mw.visitVarInsn(ALOAD, Context.obj); mw.visitVarInsn(ALOAD, Context.fieldName); mw.visitMethodInsn(INVOKEVIRTUAL, JavaBeanSerializer, "applyName", "(L" + JSONSerializer + ";Ljava/lang/Object;Ljava/lang/String;)Z"); mw.visitJumpInsn(IFEQ, _end); _labelApply(mw, property, context, _end); } if (property.field == null) { mw.visitVarInsn(ALOAD, context.var("out")); mw.visitLdcInsn(SerializerFeature.IgnoreNonFieldGetter.mask); mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "isEnabled", "(I)Z"); // if true mw.visitJumpInsn(IFNE, _end); } }
private void _getCollectionFieldItemDeser(Context context, MethodVisitor mw, FieldInfo fieldInfo, Class<?> itemType) { Label notNull_ = new Label(); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.getClassName(), fieldInfo.getName() + "_asm_list_item_deser__", "Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;"); mw.visitJumpInsn(IFNONNULL, notNull_); mw.visitVarInsn(ALOAD, 0); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/parser/DefaultJSONParser", "getConfig", "()" + "Lcom/alibaba/fastjson/parser/ParserConfig;"); mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(getDesc(itemType))); mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/parser/ParserConfig", "getDeserializer", "(Ljava/lang/reflect/Type;)Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;"); mw.visitFieldInsn(PUTFIELD, context.getClassName(), fieldInfo.getName() + "_asm_list_item_deser__", "Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;"); mw.visitLabel(notNull_); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.getClassName(), fieldInfo.getName() + "_asm_list_item_deser__", "Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;"); }
private void _getListFieldItemSer(Context context, MethodVisitor mw, FieldInfo fieldInfo, Class<?> itemType) { Label notNull_ = new Label(); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_list_item_ser_", ObjectSerializer_desc); mw.visitJumpInsn(IFNONNULL, notNull_); mw.visitVarInsn(ALOAD, 0); // this mw.visitVarInsn(ALOAD, Context.serializer); mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(itemType))); mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "getObjectWriter", "(Ljava/lang/Class;)" + ObjectSerializer_desc); mw.visitFieldInsn(PUTFIELD, context.className, fieldInfo.name + "_asm_list_item_ser_", ObjectSerializer_desc); mw.visitLabel(notNull_); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_list_item_ser_", ObjectSerializer_desc); }
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_); mw.visitVarInsn(ALOAD, 0); // this 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); }
public SerializeBeanInfo(Class<?> beanType, // JSONType jsonType, // String typeName, // String typeKey, int features, FieldInfo[] fields, // FieldInfo[] sortedFields ){ this.beanType = beanType; this.jsonType = jsonType; this.typeName = typeName; this.typeKey = typeKey; this.features = features; this.fields = fields; this.sortedFields = sortedFields; }
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, "com/alibaba/fastjson/serializer/SerializeWriter", "writeFieldValue", "(CLjava/lang/String;J)V"); _seperator(mw, context); mw.visitLabel(_end); }
private void _char(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) { Label _end = new Label(); _nameApply(mw, property, context, _end); _get(mw, context, property); mw.visitVarInsn(ISTORE, context.var("char")); _filters(mw, property, context, _end); mw.visitVarInsn(ALOAD, context.var("out")); mw.visitVarInsn(ILOAD, context.var("seperator")); mw.visitVarInsn(ALOAD, context.fieldName()); mw.visitVarInsn(ILOAD, context.var("char")); mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/SerializeWriter", "writeFieldValue", "(CLjava/lang/String;C)V"); _seperator(mw, context); mw.visitLabel(_end); }
public FieldDeserializer createFieldDeserializerWithoutASM(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo) { Class<?> fieldClass = fieldInfo.getFieldClass(); if (fieldClass == boolean.class || fieldClass == Boolean.class) { return new BooleanFieldDeserializer(mapping, clazz, fieldInfo); } if (fieldClass == int.class || fieldClass == Integer.class) { return new IntegerFieldDeserializer(mapping, clazz, fieldInfo); } if (fieldClass == long.class || fieldClass == Long.class) { return new LongFieldDeserializer(mapping, clazz, fieldInfo); } if (fieldClass == String.class) { return new StringFieldDeserializer(mapping, clazz, fieldInfo); } if (fieldClass == List.class || fieldClass == ArrayList.class) { return new ArrayListTypeFieldDeserializer(mapping, clazz, fieldInfo); } return new DefaultFieldDeserializer(mapping, clazz, fieldInfo); }
public FieldDeserializer createFieldDeserializerWithoutASM(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo) { Class<?> fieldClass = fieldInfo.getFieldClass(); if (fieldClass == Boolean.TYPE || fieldClass == Boolean.class) { return new BooleanFieldDeserializer(mapping, clazz, fieldInfo); } if (fieldClass == Integer.TYPE || fieldClass == Integer.class) { return new IntegerFieldDeserializer(mapping, clazz, fieldInfo); } if (fieldClass == Long.TYPE || fieldClass == Long.class) { return new LongFieldDeserializer(mapping, clazz, fieldInfo); } if (fieldClass == String.class) { return new StringFieldDeserializer(mapping, clazz, fieldInfo); } if (fieldClass == List.class || fieldClass == ArrayList.class) { return new ArrayListTypeFieldDeserializer(mapping, clazz, fieldInfo); } return new DefaultFieldDeserializer(mapping, clazz, fieldInfo); }
private void _short(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) { Label _end = new Label(); _nameApply(mw, property, context, _end); _get(mw, context, property); mw.visitVarInsn(ISTORE, context.var("short")); _filters(mw, property, context, _end); mw.visitVarInsn(ALOAD, context.var("out")); mw.visitVarInsn(ILOAD, context.var("seperator")); mw.visitVarInsn(ALOAD, context.fieldName()); mw.visitVarInsn(ILOAD, context.var("short")); mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/SerializeWriter", "writeFieldValue", "(CLjava/lang/String;I)V"); _seperator(mw, context); mw.visitLabel(_end); }
private void _decimal(Class<?> cls, MethodVisitor mw, FieldInfo property, Context context) { Label _end = new Label(); _nameApply(mw, property, context, _end); _get(mw, context, property); mw.visitVarInsn(58, context.var("decimal")); _filters(mw, property, context, _end); Label _if = new Label(); Label _else = new Label(); Label _end_if = new Label(); mw.visitLabel(_if); mw.visitVarInsn(25, context.var("decimal")); mw.visitJumpInsn(199, _else); _if_write_null(mw, property, context); mw.visitJumpInsn(167, _end_if); mw.visitLabel(_else); mw.visitVarInsn(25, context.var("out")); mw.visitVarInsn(21, context.var("seperator")); mw.visitVarInsn(25, context.fieldName()); mw.visitVarInsn(25, context.var("decimal")); mw.visitMethodInsn(182, "com/alibaba/fastjson/serializer/SerializeWriter", "writeFieldValue", "(CLjava/lang/String;Ljava/math/BigDecimal;)V"); _seperator(mw, context); mw.visitJumpInsn(167, _end_if); mw.visitLabel(_end_if); mw.visitLabel(_end); }
private void _string(Class<?> cls, MethodVisitor mw, FieldInfo property, Context context) { Label _end = new Label(); _nameApply(mw, property, context, _end); _get(mw, context, property); mw.visitVarInsn(58, context.var("string")); _filters(mw, property, context, _end); Label _else = new Label(); Label _end_if = new Label(); mw.visitVarInsn(25, context.var("string")); mw.visitJumpInsn(199, _else); _if_write_null(mw, property, context); mw.visitJumpInsn(167, _end_if); mw.visitLabel(_else); mw.visitVarInsn(25, context.var("out")); mw.visitVarInsn(21, context.var("seperator")); mw.visitVarInsn(25, context.fieldName()); mw.visitVarInsn(25, context.var("string")); mw.visitMethodInsn(182, "com/alibaba/fastjson/serializer/SerializeWriter", "writeFieldValue", "(CLjava/lang/String;Ljava/lang/String;)V"); _seperator(mw, context); mw.visitLabel(_end_if); mw.visitLabel(_end); }
private void _filters(MethodVisitor mw, FieldInfo property, Context context, Label _end) { if (property.getField() != null && Modifier.isTransient(property.getField().getModifiers())) { mw.visitVarInsn(25, context.var("out")); mw.visitFieldInsn(178, "com/alibaba/fastjson/serializer/SerializerFeature", "SkipTransientField", "Lcom/alibaba/fastjson/serializer/SerializerFeature;"); mw.visitMethodInsn(182, "com/alibaba/fastjson/serializer/SerializeWriter", "isEnabled", "(Lcom/alibaba/fastjson/serializer/SerializerFeature;)Z"); mw.visitJumpInsn(154, _end); } _notWriteDefault(mw, property, context, _end); _apply(mw, property, context); mw.visitJumpInsn(153, _end); _processKey(mw, property, context); Label _else_processKey = new Label(); _processValue(mw, property, context); mw.visitVarInsn(25, context.original()); mw.visitVarInsn(25, context.processValue()); mw.visitJumpInsn(165, _else_processKey); _writeObject(mw, property, context, _end); mw.visitJumpInsn(167, _end); mw.visitLabel(_else_processKey); }
private void _batchSet(Context context, MethodVisitor mw, boolean flag) { for (int i = 0, size = context.getFieldInfoList().size(); i < size; ++i) { Label notSet_ = new Label(); if (flag) { _isFlag(mw, context, i, notSet_); } FieldInfo fieldInfo = context.getFieldInfoList().get(i); _loadAndSet(context, mw, fieldInfo); if (flag) { mw.visitLabel(notSet_); } } }
public FieldSerializer(FieldInfo fieldInfo) { int i = 0; this.fieldInfo = fieldInfo; fieldInfo.setAccessible(true); this.double_quoted_fieldPrefix = '\"' + fieldInfo.getName() + "\":"; this.single_quoted_fieldPrefix = '\'' + fieldInfo.getName() + "':"; this.un_quoted_fieldPrefix = fieldInfo.getName() + ":"; JSONField annotation = (JSONField) fieldInfo.getAnnotation(JSONField.class); if (annotation != null) { SerializerFeature[] serialzeFeatures = annotation.serialzeFeatures(); int length = serialzeFeatures.length; while (i < length) { if (serialzeFeatures[i] == SerializerFeature.WriteMapNullValue) { this.writeNull = true; } i++; } } }
public ArrayListTypeFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo){ super(clazz, fieldInfo); Type fieldType = fieldInfo.fieldType; if (fieldType instanceof ParameterizedType) { Type argType = ((ParameterizedType) fieldInfo.fieldType).getActualTypeArguments()[0]; if (argType instanceof WildcardType) { WildcardType wildcardType = (WildcardType) argType; Type[] upperBounds = wildcardType.getUpperBounds(); if (upperBounds.length == 1) { argType = upperBounds[0]; } } this.itemType = argType; } else { this.itemType = Object.class; } }
public Context(FieldInfo[] getters, // SerializeBeanInfo beanInfo, // String className, // boolean writeDirect, // boolean nonContext){ this.getters = getters; this.className = className; this.beanInfo = beanInfo; this.writeDirect = writeDirect; this.nonContext = nonContext || beanInfo.beanType.isEnum(); }
private void genBatchSet(List<FieldInfo> fieldList, boolean flag) throws IOException { for (int i = 0, size = fieldList.size(); i < size; ++i) { FieldInfo fieldInfo = fieldList.get(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.getMethod() != null) { print("\tinstance."); print(fieldInfo.getMethod().getName()); print("("); printFieldVarName(fieldInfo); println(");"); } else { print("\tinstance."); print(fieldInfo.getField().getName()); print(" = "); printFieldVarName(fieldInfo); println(";"); } if (flag) { decrementIndent(); println(); println("}"); } } }
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("}"); } } }
public void test_null() throws Exception { FieldInfo fieldInfo = new FieldInfo("getValue", Entity.class.getMethod("getValue"), null, null, null, 0, 0, 0, null, null, null); Assert.assertEquals(null, fieldInfo.getAnnotation()); Field field = GenericFieldEntity.class.getField("value"); Type type = new ParameterizedTypeImpl(new Type[] { ValueObject.class }, null, GenericFieldEntity.class); FieldInfo fieldInfoOfField = new FieldInfo("value", null, field, GenericFieldEntity.class, type, 0, 0, 0, null, null, null); Assert.assertEquals(fieldInfoOfField.fieldType, ValueObject.class); Assert.assertEquals(fieldInfoOfField.fieldClass, ValueObject.class); field = GenericListFieldEntity.class.getField("value"); type = new ParameterizedTypeImpl(new Type[] { ValueObject.class }, null, GenericListFieldEntity.class); FieldInfo fieldInfoOfListField = new FieldInfo("value", null, field, GenericListFieldEntity.class, type, 0, 0, 0, null, null, null); ParameterizedTypeImpl actualFieldType = (ParameterizedTypeImpl) fieldInfoOfListField.fieldType; Assert.assertEquals(actualFieldType.getActualTypeArguments()[0], ValueObject.class); Assert.assertEquals(actualFieldType.getRawType(), List.class); Assert.assertEquals(fieldInfoOfListField.fieldClass, List.class); Assert.assertEquals(null, ((ParameterizedTypeImpl) type).getOwnerType()); Method method = GenericSetterEntity.class.getMethod("setValue", Object.class); type = new ParameterizedTypeImpl(new Type[] { ValueObject.class }, null, GenericSetterEntity.class); FieldInfo fieldInfoOfSetter = new FieldInfo("value", method, null, GenericSetterEntity.class, type, 0, 0, 0, null, null, null); Assert.assertEquals(fieldInfoOfSetter.fieldType, ValueObject.class); Assert.assertEquals(fieldInfoOfSetter.fieldClass, ValueObject.class); fieldInfoOfSetter.toString(); method = GenericListSetterEntity.class.getMethod("setValue", List.class); type = new ParameterizedTypeImpl(new Type[] { ValueObject.class }, null, GenericListSetterEntity.class); FieldInfo fieldInfoOfListSetter = new FieldInfo("value", method, null, GenericListSetterEntity.class, type, 0, 0, 0, null, null, null); Assert.assertEquals(actualFieldType.getActualTypeArguments()[0], ValueObject.class); Assert.assertEquals(actualFieldType.getRawType(), List.class); Assert.assertEquals(fieldInfoOfListSetter.fieldClass, List.class); }
private void _set(Context context, MethodVisitor mw, FieldInfo fieldInfo) { if (fieldInfo.getMethod() != null) { mw.visitMethodInsn(INVOKEVIRTUAL, getType(fieldInfo.getDeclaringClass()), fieldInfo.getMethod().getName(), getDesc(fieldInfo.getMethod())); if (!fieldInfo.getMethod().getReturnType().equals(Void.TYPE)) { mw.visitInsn(POP); } } else { mw.visitFieldInsn(PUTFIELD, getType(fieldInfo.getDeclaringClass()), fieldInfo.getField().getName(), getDesc(fieldInfo.getFieldClass())); } }
public ArrayListTypeFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo) { super(clazz, fieldInfo); if (getFieldType() instanceof ParameterizedType) { this.itemType = ((ParameterizedType) getFieldType()).getActualTypeArguments()[0]; } else { this.itemType = Object.class; } }
public JavaBeanDeserializer(ParserConfig config, Class<?> clazz, Type type) { this.feildDeserializerMap = new IdentityHashMap(); this.fieldDeserializers = new ArrayList(); this.sortedFieldDeserializers = new ArrayList(); this.clazz = clazz; this.beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); for (FieldInfo fieldInfo : this.beanInfo.getFieldList()) { addFieldDeserializer(config, clazz, fieldInfo); } for (FieldInfo fieldInfo2 : this.beanInfo.getSortedFieldList()) { this.sortedFieldDeserializers.add((FieldDeserializer) this.feildDeserializerMap.get(fieldInfo2.getName().intern())); } }
private void _batchSet(Context context, MethodVisitor mw, boolean flag) { int size = context.getFieldInfoList().size(); for (int i = 0; i < size; i++) { Label notSet_ = new Label(); if (flag) { _isFlag(mw, context, i, notSet_); } _loadAndSet(context, mw, (FieldInfo) context.getFieldInfoList().get(i)); if (flag) { mw.visitLabel(notSet_); } } }
public FieldDeserializer createFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo) throws Exception { Class<?> fieldClass = fieldInfo.getFieldClass(); if (fieldClass == int.class || fieldClass == long.class || fieldClass == String.class) { return createStringFieldDeserializer(mapping, clazz, fieldInfo); } FieldDeserializer fieldDeserializer = mapping.createFieldDeserializerWithoutASM(mapping, clazz, fieldInfo); return fieldDeserializer; }
private void _deserialze_obj(Context context, MethodVisitor mw, Label reset_, FieldInfo fieldInfo, Class<?> fieldClass, int i) { Label matched_ = new Label(); Label _end_if = new Label(); mw.visitVarInsn(25, context.var("lexer")); mw.visitVarInsn(25, 0); mw.visitFieldInsn(180, context.getClassName(), fieldInfo.getName() + "_asm_prefix__", "[C"); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/JSONLexerBase", "matchField", "([C)Z"); mw.visitJumpInsn(154, matched_); mw.visitInsn(1); mw.visitVarInsn(58, context.var(fieldInfo.getName() + "_asm")); mw.visitJumpInsn(167, _end_if); mw.visitLabel(matched_); _setFlag(mw, context, i); mw.visitVarInsn(21, context.var("matchedCount")); mw.visitInsn(4); mw.visitInsn(96); mw.visitVarInsn(54, context.var("matchedCount")); _deserObject(context, mw, fieldInfo, fieldClass); mw.visitVarInsn(25, 1); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/DefaultJSONParser", "getResolveStatus", "()I"); mw.visitFieldInsn(178, "com/alibaba/fastjson/parser/DefaultJSONParser", "NeedToResolve", "I"); mw.visitJumpInsn(160, _end_if); mw.visitVarInsn(25, 1); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/DefaultJSONParser", "getLastResolveTask", "()Lcom/alibaba/fastjson/parser/DefaultJSONParser$ResolveTask;"); mw.visitVarInsn(58, context.var("resolveTask")); mw.visitVarInsn(25, context.var("resolveTask")); mw.visitVarInsn(25, 1); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/DefaultJSONParser", "getContext", "()Lcom/alibaba/fastjson/parser/ParseContext;"); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/DefaultJSONParser$ResolveTask", "setOwnerContext", "(Lcom/alibaba/fastjson/parser/ParseContext;)V"); mw.visitVarInsn(25, context.var("resolveTask")); mw.visitVarInsn(25, 0); mw.visitLdcInsn(fieldInfo.getName()); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/deserializer/ASMJavaBeanDeserializer", "getFieldDeserializer", "(Ljava/lang/String;)Lcom/alibaba/fastjson/parser/deserializer/FieldDeserializer;"); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/DefaultJSONParser$ResolveTask", "setFieldDeserializer", "(Lcom/alibaba/fastjson/parser/deserializer/FieldDeserializer;)V"); mw.visitVarInsn(25, 1); mw.visitFieldInsn(178, "com/alibaba/fastjson/parser/DefaultJSONParser", "NONE", "I"); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/DefaultJSONParser", "setResolveStatus", "(I)V"); mw.visitLabel(_end_if); }
private void _getFieldDeser(Context context, MethodVisitor mw, FieldInfo fieldInfo) { Label notNull_ = new Label(); mw.visitVarInsn(25, 0); mw.visitFieldInsn(180, context.getClassName(), fieldInfo.getName() + "_asm_deser__", "Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;"); mw.visitJumpInsn(199, notNull_); mw.visitVarInsn(25, 0); mw.visitVarInsn(25, 1); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/DefaultJSONParser", "getConfig", "()Lcom/alibaba/fastjson/parser/ParserConfig;"); mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(ASMUtils.getDesc(fieldInfo.getFieldClass()))); mw.visitMethodInsn(182, "com/alibaba/fastjson/parser/ParserConfig", "getDeserializer", "(Ljava/lang/reflect/Type;)Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;"); mw.visitFieldInsn(181, context.getClassName(), fieldInfo.getName() + "_asm_deser__", "Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;"); mw.visitLabel(notNull_); mw.visitVarInsn(25, 0); mw.visitFieldInsn(180, context.getClassName(), fieldInfo.getName() + "_asm_deser__", "Lcom/alibaba/fastjson/parser/deserializer/ObjectDeserializer;"); }
public FieldDeserializer createFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo) { boolean asmEnable = this.asmEnable; if (asmEnable) { Class<?> superClass = clazz; while (Modifier.isPublic(superClass.getModifiers())) { superClass = superClass.getSuperclass(); if (superClass != Object.class) { if (superClass == null) { break; } } break; } asmEnable = false; } if (fieldInfo.getFieldClass() == Class.class) { asmEnable = false; } if (asmEnable && this.asmFactory != null && this.asmFactory.isExternalClass(clazz)) { asmEnable = false; } if (!asmEnable) { return createFieldDeserializerWithoutASM(mapping, clazz, fieldInfo); } try { return this.asmFactory.createFieldDeserializer(mapping, clazz, fieldInfo); } catch (Throwable th) { return createFieldDeserializerWithoutASM(mapping, clazz, fieldInfo); } }
private void _get(MethodVisitor mw, Context context, FieldInfo property) { Method method = property.getMethod(); if (method != null) { mw.visitVarInsn(ALOAD, context.var("entity")); mw.visitMethodInsn(INVOKEVIRTUAL, getType(method.getDeclaringClass()), method.getName(), getDesc(method)); } else { mw.visitVarInsn(ALOAD, context.var("entity")); mw.visitFieldInsn(GETFIELD, getType(property.getDeclaringClass()), property.getField().getName(), getDesc(property.getFieldClass())); } }