Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
bug fixed external class.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Nov 4, 2017
1 parent 1082ddf commit ea4bfce
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
19 changes: 15 additions & 4 deletions src/main/java/com/alibaba/fastjson/parser/ParserConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -858,12 +858,16 @@ public Class<?> checkAutoType(String typeName, Class<?> expectClass) {
}

final String className = typeName.replace('$', '.');
Class<?> clazz = null;

if (autoTypeSupport || expectClass != null) {
for (int i = 0; i < acceptList.length; ++i) {
String accept = acceptList[i];
if (className.startsWith(accept)) {
return TypeUtils.loadClass(typeName, defaultClassLoader);
clazz = TypeUtils.loadClass(typeName, defaultClassLoader);
if (clazz != null) {
return clazz;
}
}
}

Expand All @@ -875,7 +879,10 @@ public Class<?> checkAutoType(String typeName, Class<?> expectClass) {
}
}

Class<?> clazz = TypeUtils.getClassFromMapping(typeName);
if (clazz == null) {
clazz = TypeUtils.getClassFromMapping(typeName);
}

if (clazz == null) {
clazz = deserializers.findClass(typeName);
}
Expand All @@ -900,7 +907,9 @@ public Class<?> checkAutoType(String typeName, Class<?> expectClass) {
for (int i = 0; i < acceptList.length; ++i) {
String accept = acceptList[i];
if (className.startsWith(accept)) {
clazz = TypeUtils.loadClass(typeName, defaultClassLoader);
if (clazz == null) {
clazz = TypeUtils.loadClass(typeName, defaultClassLoader);
}

if (expectClass != null && expectClass.isAssignableFrom(clazz)) {
throw new JSONException("type not match. " + typeName + " -> " + expectClass.getName());
Expand All @@ -910,7 +919,9 @@ public Class<?> checkAutoType(String typeName, Class<?> expectClass) {
}
}

clazz = TypeUtils.loadClass(typeName, defaultClassLoader);
if (clazz == null) {
clazz = TypeUtils.loadClass(typeName, defaultClassLoader);
}

if (clazz != null) {
if (TypeUtils.getAnnotation(clazz,JSONType.class) != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/alibaba/json/bvt/TestExternal3.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


public class TestExternal3 extends TestCase {
ParserConfig confg = new ParserConfig();
ParserConfig confg = ParserConfig.global;
protected void setUp() throws Exception {
confg.addAccept("external.VO");
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/alibaba/json/bvt/TestExternal4.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.alibaba.fastjson.serializer.SerializerFeature;

public class TestExternal4 extends TestCase {
ParserConfig confg = new ParserConfig();
ParserConfig confg = ParserConfig.global;
protected void setUp() throws Exception {
confg.addAccept("external.VO2");
}
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/com/alibaba/json/bvt/TestExternal5.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.alibaba.fastjson.serializer.SerializerFeature;

public class TestExternal5 extends TestCase {
ParserConfig confg = new ParserConfig();
ParserConfig confg = ParserConfig.global;
protected void setUp() throws Exception {
confg.addAccept("com.alibaba.dubbo.demo");
}
Expand All @@ -37,9 +37,11 @@ public void test_0() throws Exception {

String text = JSON.toJSONString(obj, SerializerFeature.WriteClassName, SerializerFeature.WriteMapNullValue);
System.out.println(text);
JSON.parseObject(text, clazz, confg);
String clazzName = JSON.parse(text, confg).getClass().getName();
Assert.assertEquals(clazz.getName(), clazzName);
Object object = JSON.parseObject(text, clazz, confg);
assertEquals("a1", clazz.getName(), object.getClass().getName());

Object object2 = JSON.parse(text, confg);
assertEquals("a2 " + text, clazz.getName(), object2.getClass().getName());
}

public static class ExtClassLoader extends ClassLoader {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/alibaba/json/bvt/TestExternal6.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.alibaba.fastjson.serializer.SerializerFeature;

public class TestExternal6 extends TestCase {
ParserConfig confg = new ParserConfig();
ParserConfig confg = ParserConfig.global;

protected void setUp() throws Exception {
confg.addAccept("org.mule.esb.model");
Expand Down

0 comments on commit ea4bfce

Please sign in to comment.