}
}
Collections.sort(context.getFieldInfoList());
MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "deserialze", "(" + getDesc(PBDeserializer.class)
+ getDesc(Type.class)
+ "Ljava/lang/Object;)Ljava/lang/Object;", null,
null);
//Label super_ = new Label();
Label return_ = new Label();
Label end_ = new Label();
Constructor<?> defaultConstructor = context.getBeanInfo().getDefaultConstructor();
// create instance
if (context.getClazz().isInterface()) {
mw.visitVarInsn(ALOAD, 0);
mw.visitVarInsn(ALOAD, 1);
mw.visitMethodInsn(INVOKESPECIAL, getType(ASMJavaBeanDeserializer.class), "createInstance",
"(" + getDesc(PBDeserializer.class) + ")Ljava/lang/Object;");
mw.visitTypeInsn(CHECKCAST, getType(context.getClazz())); // cast
mw.visitVarInsn(ASTORE, context.var("instance"));
} else {
if (defaultConstructor != null) {
if (Modifier.isPublic(defaultConstructor.getModifiers())) {
mw.visitTypeInsn(NEW, getType(context.getClazz()));
mw.visitInsn(DUP);
mw.visitMethodInsn(INVOKESPECIAL, getType(context.getClazz()), "<init>", "()V");
mw.visitVarInsn(ASTORE, context.var("instance"));
} else {
mw.visitVarInsn(ALOAD, 0);
mw.visitVarInsn(ALOAD, 1);
mw.visitMethodInsn(INVOKESPECIAL, getType(ASMJavaBeanDeserializer.class), "createInstance",
"(" + getDesc(PBDeserializer.class) + ")Ljava/lang/Object;");
mw.visitTypeInsn(CHECKCAST, getType(context.getClazz())); // cast
mw.visitVarInsn(ASTORE, context.var("instance"));
}
} else {
mw.visitInsn(ACONST_NULL);
mw.visitTypeInsn(CHECKCAST, getType(context.getClazz())); // cast
mw.visitVarInsn(ASTORE, context.var("instance"));
}
}
{
mw.visitVarInsn(ALOAD, 1); // parser
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "getContext",
"()Lcom/jd/glowworm/deserializer/ParseContext;");
mw.visitVarInsn(ASTORE, context.var("context"));
mw.visitVarInsn(ALOAD, 1); // parser
mw.visitVarInsn(ALOAD, context.var("context"));
mw.visitVarInsn(ALOAD, context.var("instance"));
mw.visitVarInsn(ALOAD, 3); // fieldName
mw.visitMethodInsn(INVOKEVIRTUAL,
getType(PBDeserializer.class),
"setContext",
"(Lcom/jd/glowworm/deserializer/ParseContext;Ljava/lang/Object;Ljava/lang/Object;)Lcom/jd/glowworm/deserializer/ParseContext;");
mw.visitVarInsn(ASTORE, context.var("childContext"));
}
for (int i = 0, size = context.getFieldInfoList().size(); i < size; ++i) {
FieldInfo fieldInfo = context.getFieldInfoList().get(i);
Class<?> fieldClass = fieldInfo.getFieldClass();
Type fieldType = fieldInfo.getFieldType();
mw.visitVarInsn(ALOAD, 1);
if (fieldClass == boolean.class) {
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldBoolean", "()Z");
mw.visitVarInsn(ISTORE, context.var(fieldInfo.getName() + "_asm"));
} else if (fieldClass == byte.class) {
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldInt", "()I");
mw.visitVarInsn(ISTORE, context.var(fieldInfo.getName() + "_asm"));
} else if (fieldClass == short.class) {
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldInt", "()I");
mw.visitVarInsn(ISTORE, context.var(fieldInfo.getName() + "_asm"));
} else if (fieldClass == int.class) {
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldInt", "()I");
mw.visitVarInsn(ISTORE, context.var(fieldInfo.getName() + "_asm"));
} else if (fieldClass == long.class) {
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldlong", "()J");
mw.visitVarInsn(LSTORE, context.var(fieldInfo.getName() + "_asm", 2));
} else if (fieldClass == float.class) {
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldFloat", "()F");
mw.visitVarInsn(FSTORE, context.var(fieldInfo.getName() + "_asm"));
} else if (fieldClass == double.class) {
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldDouble", "()D");
mw.visitVarInsn(DSTORE, context.var(fieldInfo.getName() + "_asm", 2));
} else if (fieldClass == String.class) {
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldString",
"()Ljava/lang/String;");
mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm"));
} else if (fieldClass == byte[].class) {
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldByteArray", "()[B");
mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm"));
} else if (fieldClass.isEnum()) {
mw.visitInsn(ACONST_NULL);
mw.visitTypeInsn(CHECKCAST, getType(fieldClass)); // cast
mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm"));
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldString",
"()Ljava/lang/String;");
mw.visitMethodInsn(INVOKESTATIC, getType(fieldClass), "valueOf", "(Ljava/lang/String;)"
+ getDesc(fieldClass));
mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm"));
} else if (Collection.class.isAssignableFrom(fieldClass)) {
Type actualTypeArgument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
if (actualTypeArgument instanceof Class) {
Class<?> itemClass = (Class<?>) actualTypeArgument;
if (!Modifier.isPublic(itemClass.getModifiers())) {
throw new ASMException("can not create ASMParser");
}
/*if (itemClass == String.class) {
mw.visitLdcInsn(com.jd.glowworm.asm.Type.getType(getDesc(fieldClass))); // cast
mw.visitMethodInsn(INVOKEVIRTUAL, getType(PBDeserializer.class), "scanFieldStringList",
"([Ljava/lang/Class;)" + getDesc(Collection.class));
mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm"));
} else {*/
_deserialze_list_obj(context, mw, fieldInfo, fieldClass, itemClass);
/*if (i == size - 1) {
_deserialize_endCheck(context, mw, reset_);
}*/
continue;
//}
} else {
throw new ASMException("can not create ASMParser");
}
} else {
_deserialze_obj(context, mw, fieldInfo, fieldClass);
/*if (i == size - 1) {
_deserialize_endCheck(context, mw, reset_);
}*/
continue;
}
}
mw.visitLabel(end_);
if (!context.getClazz().isInterface() && !Modifier.isAbstract(context.getClazz().getModifiers())) {
if (defaultConstructor != null) {
_batchSet(context, mw);
} else {
Constructor<?> creatorConstructor = context.getBeanInfo().getCreatorConstructor();
if (creatorConstructor != null) {
mw.visitTypeInsn(NEW, getType(context.getClazz()));
mw.visitInsn(DUP);
_loadCreatorParameters(context, mw);
mw.visitMethodInsn(INVOKESPECIAL, getType(context.getClazz()), "<init>",
getDesc(creatorConstructor));
mw.visitVarInsn(ASTORE, context.var("instance"));
} else {
Method factoryMethod = context.getBeanInfo().getFactoryMethod();
if (factoryMethod != null) {
_loadCreatorParameters(context, mw);
mw.visitMethodInsn(INVOKESTATIC, getType(factoryMethod.getDeclaringClass()),
factoryMethod.getName(), getDesc(factoryMethod));
mw.visitVarInsn(ASTORE, context.var("instance"));
} else {
throw new PBException("TODO");
}
}
}
}
mw.visitLabel(return_);
_setContext(context, mw, true);
mw.visitVarInsn(ALOAD, context.var("instance"));
mw.visitInsn(ARETURN);
/*mw.visitLabel(super_);
mw.visitVarInsn(ALOAD, 0);
mw.visitVarInsn(ALOAD, 1);
mw.visitVarInsn(ALOAD, 2);
mw.visitVarInsn(ALOAD, 3);
mw.visitMethodInsn(INVOKESPECIAL, getType(ASMJavaBeanDeserializer.class), "deserialze",
"(" + getDesc(PBDeserializer.class) + getDesc(Type.class)
+ "Ljava/lang/Object;)Ljava/lang/Object;");
mw.visitInsn(ARETURN);*/
int maxStack = context.getVariantCount();
Constructor<?> creatorConstructor = context.getBeanInfo().getCreatorConstructor();
if (creatorConstructor != null) {
int constructorTypeStack = 2;
for (Class<?> type : creatorConstructor.getParameterTypes()) {
if (type == long.class || type == double.class) {
constructorTypeStack += 2;
} else {
constructorTypeStack++;
}
}
if (maxStack < constructorTypeStack) {
maxStack = constructorTypeStack;
}
} else {
Method factoryMethod = context.getBeanInfo().getFactoryMethod();
if (factoryMethod != null) {
int paramStacks = 2;
for (Class<?> type : factoryMethod.getParameterTypes()) {
if (type == long.class || type == double.class) {
paramStacks += 2;
} else {
paramStacks++;
}
}
if (maxStack < paramStacks) {
maxStack = paramStacks;
}
}
}
//System.out.println(maxStack+"#"+context.getVariantCount());
mw.visitMaxs(maxStack, context.getVariantCount());
mw.visitEnd();
}