Skip to content

Commit 8c67092

Browse files
YakoYako
authored andcommitted
Merge branch 'master' of https://github.com/alibaba/fastjson
2 parents d884d85 + ccbc2f3 commit 8c67092

2 files changed

Lines changed: 25 additions & 50 deletions

File tree

src/main/java/com/alibaba/fastjson/parser/deserializer/ASMDeserializerFactory.java

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.concurrent.atomic.AtomicLong;
2323

2424
import com.alibaba.fastjson.JSON;
25-
import com.alibaba.fastjson.asm.ASMException;
2625
import com.alibaba.fastjson.asm.ClassWriter;
2726
import com.alibaba.fastjson.asm.FieldVisitor;
2827
import com.alibaba.fastjson.asm.Label;
@@ -34,6 +33,7 @@
3433
import com.alibaba.fastjson.util.ASMUtils;
3534
import com.alibaba.fastjson.util.DeserializeBeanInfo;
3635
import com.alibaba.fastjson.util.FieldInfo;
36+
import com.alibaba.fastjson.util.TypeUtils;
3737

3838
public class ASMDeserializerFactory implements Opcodes {
3939

@@ -198,7 +198,7 @@ void _deserialzeArrayMapping(ClassWriter cw, Context context) {
198198
mw.visitTypeInsn(CHECKCAST, getType(fieldClass)); // cast
199199
mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm"));
200200
} else if (Collection.class.isAssignableFrom(fieldClass)) {
201-
Class<?> itemClass = getCollectionItemClass(fieldType);
201+
Class<?> itemClass = TypeUtils.getCollectionItemClass(fieldType);
202202
if (itemClass == String.class) {
203203
mw.visitVarInsn(ALOAD, context.var("lexer"));
204204
mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(getDesc(fieldClass)));
@@ -482,20 +482,6 @@ void _deserialze(ClassWriter cw, Context context) {
482482
mw.visitVarInsn(DSTORE, context.var(fieldInfo.getName() + "_asm", 2));
483483

484484
} else if (fieldClass == String.class) {
485-
Label notEnd_ = new Label();
486-
487-
mw.visitIntInsn(ILOAD, context.var("matchStat"));
488-
mw.visitInsn(ICONST_4); // END
489-
mw.visitJumpInsn(IF_ICMPNE, notEnd_);
490-
491-
mw.visitVarInsn(ALOAD, context.var("lexer"));
492-
mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/parser/JSONLexerBase", "stringDefaultValue",
493-
"()Ljava/lang/String;");
494-
mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm"));
495-
mw.visitJumpInsn(GOTO, notMatch_);
496-
497-
mw.visitLabel(notEnd_);
498-
499485
mw.visitVarInsn(ALOAD, context.var("lexer"));
500486
mw.visitVarInsn(ALOAD, 0);
501487
mw.visitFieldInsn(GETFIELD, context.getClassName(), fieldInfo.getName() + "_asm_prefix__", "[C");
@@ -533,7 +519,7 @@ void _deserialze(ClassWriter cw, Context context) {
533519
mw.visitVarInsn(ALOAD, 0);
534520
mw.visitFieldInsn(GETFIELD, context.getClassName(), fieldInfo.getName() + "_asm_prefix__", "[C");
535521

536-
Class<?> itemClass = getCollectionItemClass(fieldType);
522+
Class<?> itemClass = TypeUtils.getCollectionItemClass(fieldType);
537523

538524
if (itemClass == String.class) {
539525
mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(getDesc(fieldClass))); // cast
@@ -646,25 +632,6 @@ void _deserialze(ClassWriter cw, Context context) {
646632
mw.visitEnd();
647633
}
648634

649-
private Class<?> getCollectionItemClass(Type fieldType) {
650-
if (fieldType instanceof ParameterizedType) {
651-
Class<?> itemClass;
652-
Type actualTypeArgument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
653-
654-
if (actualTypeArgument instanceof Class) {
655-
itemClass = (Class<?>) actualTypeArgument;
656-
if (!Modifier.isPublic(itemClass.getModifiers())) {
657-
throw new ASMException("can not create ASMParser");
658-
}
659-
} else {
660-
throw new ASMException("can not create ASMParser");
661-
}
662-
return itemClass;
663-
}
664-
665-
return Object.class;
666-
}
667-
668635
private void _isEnable(Context context, MethodVisitor mw, Feature feature) {
669636
mw.visitVarInsn(ALOAD, context.var("lexer"));
670637
mw.visitFieldInsn(GETSTATIC, "com/alibaba/fastjson/parser/Feature", feature.name(), "Lcom/alibaba/fastjson/parser/Feature;");
@@ -763,7 +730,7 @@ private void _loadAndSet(Context context, MethodVisitor mw, FieldInfo fieldInfo)
763730
_set(context, mw, fieldInfo);
764731
} else if (Collection.class.isAssignableFrom(fieldClass)) {
765732
mw.visitVarInsn(ALOAD, context.var("instance"));
766-
Type itemType = getCollectionItemClass(fieldType);
733+
Type itemType = TypeUtils.getCollectionItemClass(fieldType);
767734
if (itemType == String.class) {
768735
mw.visitVarInsn(ALOAD, context.var(fieldInfo.getName() + "_asm"));
769736
mw.visitTypeInsn(CHECKCAST, getType(fieldClass)); // cast
@@ -818,7 +785,6 @@ private void _setContext(Context context, MethodVisitor mw) {
818785
}
819786

820787
private void _deserialize_endCheck(Context context, MethodVisitor mw, Label reset_) {
821-
Label _end_if = new Label();
822788
// Label nextToken_ = new Label();
823789

824790
// mw.visitFieldInsn(GETSTATIC, getType(System.class), "out", "Ljava/io/PrintStream;");
@@ -842,23 +808,15 @@ private void _deserialize_endCheck(Context context, MethodVisitor mw, Label rese
842808
mw.visitVarInsn(ALOAD, context.var("lexer"));
843809
mw.visitFieldInsn(GETSTATIC, "com/alibaba/fastjson/parser/JSONToken", "COMMA", "I");
844810
mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/parser/JSONLexerBase", "nextToken", "(I)V");
845-
846-
mw.visitLabel(_end_if);
847811
}
848812

849813
private void _deserialze_list_obj(Context context, MethodVisitor mw, Label reset_, FieldInfo fieldInfo,
850814
Class<?> fieldClass, Class<?> itemType, int i) {
851-
Label matched_ = new Label();
852815
Label _end_if = new Label();
853816

854817
mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/parser/JSONLexerBase", "matchField", "([C)Z");
855-
mw.visitJumpInsn(IFNE, matched_);
856-
mw.visitInsn(ACONST_NULL);
857-
mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm"));
818+
mw.visitJumpInsn(IFEQ, _end_if);
858819

859-
mw.visitJumpInsn(GOTO, _end_if);
860-
861-
mw.visitLabel(matched_);
862820
_setFlag(mw, context, i);
863821

864822
Label valueNotNull_ = new Label();
@@ -871,9 +829,6 @@ private void _deserialze_list_obj(Context context, MethodVisitor mw, Label reset
871829
mw.visitFieldInsn(GETSTATIC, "com/alibaba/fastjson/parser/JSONToken", "COMMA", "I");
872830
mw.visitMethodInsn(INVOKEVIRTUAL, "com/alibaba/fastjson/parser/JSONLexerBase", "nextToken", "(I)V");
873831

874-
mw.visitInsn(ACONST_NULL);
875-
mw.visitTypeInsn(CHECKCAST, getType(fieldClass)); // cast
876-
mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm"));
877832
// loop_end_
878833

879834
mw.visitLabel(valueNotNull_);

src/main/java/com/alibaba/fastjson/util/TypeUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.alibaba.fastjson.JSONObject;
5454
import com.alibaba.fastjson.annotation.JSONField;
5555
import com.alibaba.fastjson.annotation.JSONType;
56+
import com.alibaba.fastjson.asm.ASMException;
5657
import com.alibaba.fastjson.parser.Feature;
5758
import com.alibaba.fastjson.parser.JSONScanner;
5859
import com.alibaba.fastjson.parser.ParserConfig;
@@ -1400,4 +1401,23 @@ static void setAccessible(AccessibleObject obj) {
14001401
setAccessibleEnable = false;
14011402
}
14021403
}
1404+
1405+
public static Class<?> getCollectionItemClass(Type fieldType) {
1406+
if (fieldType instanceof ParameterizedType) {
1407+
Class<?> itemClass;
1408+
Type actualTypeArgument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
1409+
1410+
if (actualTypeArgument instanceof Class) {
1411+
itemClass = (Class<?>) actualTypeArgument;
1412+
if (!Modifier.isPublic(itemClass.getModifiers())) {
1413+
throw new ASMException("can not create ASMParser");
1414+
}
1415+
} else {
1416+
throw new ASMException("can not create ASMParser");
1417+
}
1418+
return itemClass;
1419+
}
1420+
1421+
return Object.class;
1422+
}
14031423
}

0 commit comments

Comments
 (0)