use of com.alibaba.fastjson.asm.Label in project uavstack by uavorg.
the class ASMSerializerFactory method _int.
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);
}
use of com.alibaba.fastjson.asm.Label in project uavstack by uavorg.
the class ASMSerializerFactory method createJavaBeanSerializer.
public ObjectSerializer createJavaBeanSerializer(Class<?> clazz, Map<String, String> aliasMap) throws Exception {
if (clazz.isPrimitive()) {
throw new JSONException("unsupportd class " + clazz.getName());
}
List<FieldInfo> getters = TypeUtils.computeGetters(clazz, aliasMap, false);
for (FieldInfo getter : getters) {
if (!ASMUtils.checkName(getter.getMember().getName())) {
return null;
}
}
String className = getGenClassName(clazz);
int beanSerializeFeatures = TypeUtils.getSerializeFeatures(clazz);
ClassWriter cw = new ClassWriter();
cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, "com/alibaba/fastjson/serializer/ASMJavaBeanSerializer", new String[] { "com/alibaba/fastjson/serializer/ObjectSerializer" });
for (FieldInfo fieldInfo : getters) {
{
FieldVisitor fw = cw.visitField(ACC_PUBLIC, fieldInfo.getName() + "_asm_fieldPrefix", "Ljava/lang/reflect/Type;");
fw.visitEnd();
}
FieldVisitor fw = cw.visitField(ACC_PUBLIC, fieldInfo.getName() + "_asm_fieldType", "Ljava/lang/reflect/Type;");
fw.visitEnd();
}
MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mw.visitVarInsn(ALOAD, 0);
mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(getDesc(clazz)));
mw.visitMethodInsn(INVOKESPECIAL, "com/alibaba/fastjson/serializer/ASMJavaBeanSerializer", "<init>", "(Ljava/lang/Class;)V");
for (FieldInfo fieldInfo : getters) {
mw.visitVarInsn(ALOAD, 0);
mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(getDesc(fieldInfo.getDeclaringClass())));
if (fieldInfo.getMethod() != null) {
mw.visitLdcInsn(fieldInfo.getMethod().getName());
mw.visitMethodInsn(INVOKESTATIC, "com/alibaba/fastjson/util/ASMUtils", "getMethodType", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Type;");
} else {
mw.visitLdcInsn(fieldInfo.getField().getName());
mw.visitMethodInsn(INVOKESTATIC, "com/alibaba/fastjson/util/ASMUtils", "getFieldType", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Type;");
}
mw.visitFieldInsn(PUTFIELD, className, fieldInfo.getName() + "_asm_fieldType", "Ljava/lang/reflect/Type;");
}
mw.visitInsn(RETURN);
mw.visitMaxs(4, 4);
mw.visitEnd();
{
Context context = new Context(className, beanSerializeFeatures);
mw = cw.visitMethod(ACC_PUBLIC, "write", "(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V", null, new String[] { "java/io/IOException" });
// serializer
mw.visitVarInsn(ALOAD, context.serializer());
mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/JSONSerializer", "getWriter", "()Lcom/alibaba/fastjson/serializer/SerializeWriter;");
mw.visitVarInsn(ASTORE, context.var("out"));
JSONType jsonType = clazz.getAnnotation(JSONType.class);
if (jsonType == null || jsonType.alphabetic()) {
Label _else = new Label();
mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitFieldInsn(GETSTATIC, "com/alibaba/fastjson/serializer/SerializerFeature", "SortField", "Lcom/alibaba/fastjson/serializer/SerializerFeature;");
mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/SerializeWriter", "isEnabled", "(Lcom/alibaba/fastjson/serializer/SerializerFeature;)Z");
mw.visitJumpInsn(IFEQ, _else);
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(INVOKEVIRTUAL, className, "write1", "(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
mw.visitInsn(RETURN);
mw.visitLabel(_else);
}
// obj
mw.visitVarInsn(ALOAD, context.obj());
// serializer
mw.visitTypeInsn(CHECKCAST, getType(clazz));
// obj
mw.visitVarInsn(ASTORE, context.var("entity"));
generateWriteMethod(clazz, mw, getters, context);
mw.visitInsn(RETURN);
mw.visitMaxs(6, context.getVariantCount() + 1);
mw.visitEnd();
}
List<FieldInfo> sortedGetters = TypeUtils.computeGetters(clazz, aliasMap, true);
{
// sortField support
Context context = new Context(className, beanSerializeFeatures);
mw = cw.visitMethod(ACC_PUBLIC, "write1", "(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V", null, new String[] { "java/io/IOException" });
// serializer
mw.visitVarInsn(ALOAD, context.serializer());
mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/JSONSerializer", "getWriter", "()Lcom/alibaba/fastjson/serializer/SerializeWriter;");
mw.visitVarInsn(ASTORE, context.var("out"));
// obj
mw.visitVarInsn(ALOAD, context.obj());
// serializer
mw.visitTypeInsn(CHECKCAST, getType(clazz));
// obj
mw.visitVarInsn(ASTORE, context.var("entity"));
generateWriteMethod(clazz, mw, sortedGetters, context);
mw.visitInsn(RETURN);
mw.visitMaxs(6, context.getVariantCount() + 1);
mw.visitEnd();
}
// writeAsArray
{
Context context = new Context(className, beanSerializeFeatures);
mw = cw.visitMethod(ACC_PUBLIC, "writeAsArray", "(Lcom/alibaba/fastjson/serializer/JSONSerializer;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;)V", null, new String[] { "java/io/IOException" });
// serializer
mw.visitVarInsn(ALOAD, context.serializer());
mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/serializer/JSONSerializer", "getWriter", "()Lcom/alibaba/fastjson/serializer/SerializeWriter;");
mw.visitVarInsn(ASTORE, context.var("out"));
// obj
mw.visitVarInsn(ALOAD, context.obj());
// serializer
mw.visitTypeInsn(CHECKCAST, getType(clazz));
// obj
mw.visitVarInsn(ASTORE, context.var("entity"));
generateWriteAsArray(clazz, mw, sortedGetters, context);
mw.visitInsn(RETURN);
mw.visitMaxs(6, context.getVariantCount() + 1);
mw.visitEnd();
}
byte[] code = cw.toByteArray();
if (JSON.DUMP_CLASS != null) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(JSON.DUMP_CLASS + File.separator + className + ".class");
fos.write(code);
} catch (Exception ex) {
// System.err.println("FASTJSON dump class:"+className+"失败:"+ex.getMessage());
} finally {
if (fos != null) {
fos.close();
}
}
}
Class<?> exampleClass = classLoader.defineClassPublic(className, code, 0, code.length);
Object instance = exampleClass.newInstance();
return (ObjectSerializer) instance;
}
use of com.alibaba.fastjson.asm.Label in project uavstack by uavorg.
the class ASMSerializerFactory method _char.
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);
}
use of com.alibaba.fastjson.asm.Label in project uavstack by uavorg.
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 _end_if = 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, _end_if);
// 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, "com/alibaba/fastjson/serializer/SerializeWriter", "writeFieldValue", "(CLjava/lang/String;Ljava/math/BigDecimal;)V");
_seperator(mw, context);
mw.visitJumpInsn(GOTO, _end_if);
mw.visitLabel(_end_if);
mw.visitLabel(_end);
}
use of com.alibaba.fastjson.asm.Label in project fastjson by alibaba.
the class ASMDeserializerFactory method _setContext.
private void _setContext(Context context, MethodVisitor mw) {
// parser
mw.visitVarInsn(ALOAD, 1);
mw.visitVarInsn(ALOAD, context.var("context"));
mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "setContext", "(" + desc(ParseContext.class) + ")V");
Label endIf_ = new Label();
mw.visitVarInsn(ALOAD, context.var("childContext"));
mw.visitJumpInsn(IFNULL, endIf_);
mw.visitVarInsn(ALOAD, context.var("childContext"));
mw.visitVarInsn(ALOAD, context.var("instance"));
mw.visitFieldInsn(PUTFIELD, type(ParseContext.class), "object", "Ljava/lang/Object;");
mw.visitLabel(endIf_);
}
Aggregations