Skip to content

Commit

Permalink
- Use Thread Context ClassLoader. Replace only when URLClassLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas authored and elguardian committed Aug 5, 2020
1 parent 2f1f5a6 commit 3f01403
Showing 1 changed file with 14 additions and 6 deletions.
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.Member;
import java.net.URLClassLoader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -72,7 +73,6 @@
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
Expand Down Expand Up @@ -281,7 +281,6 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, Java

deserializeObjectMapper.registerModule(modDeser);
deserializeObjectMapper.setConfig(deserializeObjectMapper.getDeserializationConfig().with(introspectorPair));
deserializeObjectMapper.setTypeFactory(TypeFactory.defaultInstance().withClassLoader(classLoader));
}

if (formatDate) {
Expand Down Expand Up @@ -868,7 +867,7 @@ protected CustomAsWrapperTypeDeserializer(AsWrapperTypeDeserializer src, BeanPro
public Object deserializeTypedFromArray(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ClassLoader current = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(_baseType.getRawClass().getClassLoader());
Thread.currentThread().setContextClassLoader(getClassLoaderForDeserialization());
JsonDeserializer<Object> deser = _findDeserializer(ctxt, baseTypeName());
Object value = deser.deserialize(jp, ctxt);
return value;
Expand All @@ -881,7 +880,7 @@ public Object deserializeTypedFromArray(JsonParser jp, DeserializationContext ct
public Object deserializeTypedFromObject(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ClassLoader current = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(_baseType.getRawClass().getClassLoader());
Thread.currentThread().setContextClassLoader(getClassLoaderForDeserialization());
if (classesSet.contains(_baseType.getRawClass()) && !jsonContext.get().isStripped()) {

try {
Expand All @@ -905,7 +904,7 @@ public Object deserializeTypedFromObject(JsonParser jp, DeserializationContext c
public Object deserializeTypedFromScalar(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ClassLoader current = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(_baseType.getRawClass().getClassLoader());
Thread.currentThread().setContextClassLoader(getClassLoaderForDeserialization());
if (classesSet.contains(_baseType.getRawClass())) {
try {
return super.deserializeTypedFromScalar(jp, ctxt);
Expand All @@ -927,7 +926,7 @@ public Object deserializeTypedFromScalar(JsonParser jp, DeserializationContext c
public Object deserializeTypedFromAny(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ClassLoader current = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(_baseType.getRawClass().getClassLoader());
Thread.currentThread().setContextClassLoader(getClassLoaderForDeserialization());
if (classesSet.contains(_baseType.getRawClass())) {
try {
return super.deserializeTypedFromAny(jp, ctxt);
Expand Down Expand Up @@ -972,6 +971,15 @@ public Object deserializeTypedFromAny(JsonParser jp, DeserializationContext ctxt
}
}

private ClassLoader getClassLoaderForDeserialization() {
ClassLoader baseTypeClassLoader = _baseType.getRawClass().getClassLoader();
if (baseTypeClassLoader instanceof URLClassLoader) {
return classLoader;
} else {
return baseTypeClassLoader;
}
}

@Override
public TypeDeserializer forProperty(BeanProperty prop) {
if (prop != null) {
Expand Down

0 comments on commit 3f01403

Please sign in to comment.