return (ObjectSerializer) instance;
}
private void generateWriteMethod(Class<?> clazz, MethodVisitor mw, List<FieldInfo> getters, Context context)
throws Exception {
Label end = new Label();
int size = getters.size();
{
// 格式化输出不走asm 优化
Label endFormat_ = new Label();
Label notNull_ = new Label();
mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitFieldInsn(GETSTATIC, getType(SerializerFeature.class), "PrettyFormat",
"L" + getType(SerializerFeature.class) + ";");
mw.visitMethodInsn(INVOKEVIRTUAL, getType(SerializeWriter.class), "isEnabled",
"(" + "L" + getType(SerializerFeature.class) + ";" + ")Z");
mw.visitJumpInsn(IFEQ, endFormat_);
mw.visitVarInsn(ALOAD, 0);
mw.visitFieldInsn(GETFIELD, context.getClassName(), "nature", getDesc(JavaBeanSerializer.class));
mw.visitJumpInsn(IFNONNULL, notNull_);
initNature(clazz, mw, context);
// /////
mw.visitLabel(notNull_);
mw.visitVarInsn(ALOAD, 0);
mw.visitFieldInsn(GETFIELD, context.getClassName(), "nature", getDesc(JavaBeanSerializer.class));
mw.visitVarInsn(ALOAD, 1);
mw.visitVarInsn(ALOAD, 2);
mw.visitVarInsn(ALOAD, 3);
mw.visitVarInsn(ALOAD, 4);
mw.visitMethodInsn(INVOKEVIRTUAL, getType(JavaBeanSerializer.class), "write",
"(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;)V");
mw.visitInsn(RETURN);
mw.visitLabel(endFormat_);
}
{
// if (serializer.containsReference(object)) {
Label endRef_ = new Label();
Label notNull_ = new Label();
mw.visitVarInsn(ALOAD, context.serializer());
mw.visitVarInsn(ALOAD, context.obj());
mw.visitMethodInsn(INVOKEVIRTUAL, getType(JSONSerializer.class), "containsReference",
"(Ljava/lang/Object;)Z");
mw.visitJumpInsn(IFEQ, endRef_);
mw.visitVarInsn(ALOAD, 0);
mw.visitFieldInsn(GETFIELD, context.getClassName(), "nature", getDesc(JavaBeanSerializer.class));
mw.visitJumpInsn(IFNONNULL, notNull_);
initNature(clazz, mw, context);
// /////
mw.visitLabel(notNull_);
mw.visitVarInsn(ALOAD, 0);
mw.visitFieldInsn(GETFIELD, context.getClassName(), "nature", getDesc(JavaBeanSerializer.class));
mw.visitVarInsn(ALOAD, 1);
mw.visitVarInsn(ALOAD, 2);
mw.visitMethodInsn(INVOKEVIRTUAL, getType(JavaBeanSerializer.class), "writeReference",
"(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;)V");
mw.visitInsn(RETURN);
mw.visitLabel(endRef_);
}
{
mw.visitVarInsn(ALOAD, context.serializer());
mw.visitMethodInsn(INVOKEVIRTUAL, getType(JSONSerializer.class), "getContext",
"()Lcom/alibaba/fastjson/serializer/SerialContext;");
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.visitMethodInsn(INVOKEVIRTUAL, getType(JSONSerializer.class), "setContext",
"(Lcom/alibaba/fastjson/serializer/SerialContext;Ljava/lang/Object;Ljava/lang/Object;)V");
}
// SEPERATO
{
Label end_ = new Label();
Label else_ = new Label();
Label writeClass_ = new Label();
// mw.visitVarInsn(ALOAD, context.var("out"));
// mw.visitFieldInsn(GETSTATIC, getType(SerializerFeature.class), "WriteClassName",
// "L" + getType(SerializerFeature.class) + ";");
// mw.visitMethodInsn(INVOKEVIRTUAL, getType(SerializeWriter.class), "isEnabled",
// "(" + "L" + getType(SerializerFeature.class) + ";" + ")Z");
// mw.visitJumpInsn(IFEQ, else_);
mw.visitVarInsn(ALOAD, context.serializer());
mw.visitVarInsn(ALOAD, context.paramFieldType());
mw.visitVarInsn(ALOAD, context.obj());
mw.visitMethodInsn(INVOKEVIRTUAL, getType(JSONSerializer.class), "isWriteClassName",
"(Ljava/lang/reflect/Type;Ljava/lang/Object;)Z");
mw.visitJumpInsn(IFEQ, else_);
// mw.visitVarInsn(ALOAD, context.paramFieldType());
// mw.visitJumpInsn(IFNULL, writeClass_);
// IFNULL
mw.visitVarInsn(ALOAD, context.paramFieldType());
mw.visitVarInsn(ALOAD, context.obj());
mw.visitMethodInsn(INVOKEVIRTUAL, getType(Object.class), "getClass", "()Ljava/lang/Class;");
mw.visitJumpInsn(IF_ACMPEQ, else_);
mw.visitLabel(writeClass_);
mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitLdcInsn("{\"@type\":\"" + clazz.getName() + "\"");
mw.visitMethodInsn(INVOKEVIRTUAL, getType(SerializeWriter.class), "write", "(Ljava/lang/String;)V");
mw.visitVarInsn(BIPUSH, ',');
mw.visitJumpInsn(GOTO, end_);
mw.visitLabel(else_);
mw.visitVarInsn(BIPUSH, '{');
mw.visitLabel(end_);
}
mw.visitVarInsn(ISTORE, context.var("seperator"));
for (int i = 0; i < size; ++i) {
FieldInfo property = getters.get(i);
Class<?> propertyClass = property.getFieldClass();
mw.visitLdcInsn(property.getName());
mw.visitVarInsn(ASTORE, context.fieldName());
if (propertyClass == byte.class) {
_byte(clazz, mw, property, context);
} else if (propertyClass == short.class) {
_short(clazz, mw, property, context);
} else if (propertyClass == int.class) {
_int(clazz, mw, property, context);
} 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) {
_boolean(clazz, mw, property, context);
} else if (propertyClass == char.class) {
_char(clazz, mw, property, context);
} 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);
// _object(clazz, mw, property, context);
} else if (propertyClass.isEnum()) {
_enum(clazz, mw, property, context);
} else {
_object(clazz, mw, property, context);
}
}
Label _if = new Label();
Label _else = new Label();
Label _end_if = new Label();
mw.visitLabel(_if);
// if (seperator == '{')
mw.visitVarInsn(ILOAD, context.var("seperator"));