Skip to content

Commit

Permalink
bug fix for autoType classLoader, #252
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 14, 2022
1 parent 0499cc5 commit 1ae8a83
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
24 changes: 13 additions & 11 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -753,19 +753,21 @@ public ObjectReader checkAutoType(Class expectClass, long expectClassHash, long
Class objectClass = autoTypeObjectReader.getObjectClass();
if (objectClass != null) {
ClassLoader objectClassLoader = objectClass.getClassLoader();
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (objectClassLoader != contextClassLoader) {
String typeName = getString();
Class contextClass = TypeUtils.getMapping(typeName);
if (contextClass == null) {
try {
contextClass = contextClassLoader.loadClass(typeName);
} catch (ClassNotFoundException ignored) {
if (objectClassLoader != null) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (objectClassLoader != contextClassLoader) {
String typeName = getString();
Class contextClass = TypeUtils.getMapping(typeName);
if (contextClass == null) {
try {
contextClass = contextClassLoader.loadClass(typeName);
} catch (ClassNotFoundException ignored) {
}
}
}

if (contextClass != null && !objectClass.equals(contextClass)) {
autoTypeObjectReader = getObjectReader(contextClass);
if (contextClass != null && !objectClass.equals(contextClass)) {
autoTypeObjectReader = getObjectReader(contextClass);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ public Object readObject(JSONReader jsonReader, long features) {

if (autoTypeObjectReader != null) {
Class objectClass = autoTypeObjectReader.getObjectClass();
ClassLoader objectClassLoader = objectClass.getClassLoader();
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (objectClassLoader != contextClassLoader) {
Class contextClass = null;

String typeName = jsonReader.getString();
try {
contextClass = contextClassLoader.loadClass(typeName);
} catch (ClassNotFoundException ignored) {}

if (!objectClass.equals(contextClass)) {
autoTypeObjectReader = context.getObjectReader(contextClass);
if (objectClass != null) {
ClassLoader objectClassLoader = objectClass.getClassLoader();
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (objectClassLoader != contextClassLoader) {
Class contextClass = null;

String typeName = jsonReader.getString();
try {
contextClass = contextClassLoader.loadClass(typeName);
} catch (ClassNotFoundException ignored) {
}

if (!objectClass.equals(contextClass)) {
autoTypeObjectReader = context.getObjectReader(contextClass);
}
}
}
}
Expand Down

0 comments on commit 1ae8a83

Please # to comment.