Skip to content

Commit 9947cdb

Browse files
authored
#922 Fall back to default class resolution if class cannot be loaded with provided class loader (#926)
1 parent cb1cd85 commit 9947cdb

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/com/esotericsoftware/kryo/serializers/JavaSerializer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ private static class ObjectInputStreamWithKryoClassLoader extends ObjectInputStr
8484
protected Class resolveClass (ObjectStreamClass type) {
8585
try {
8686
return Class.forName(type.getName(), false, kryo.getClassLoader());
87+
} catch (ClassNotFoundException ignored) {}
88+
try {
89+
return super.resolveClass(type);
8790
} catch (ClassNotFoundException ex) {
8891
throw new KryoException("Class not found: " + type.getName(), ex);
92+
} catch (IOException ex) {
93+
throw new KryoException("Could not load class: " + type.getName(), ex);
8994
}
9095
}
9196
}

test/com/esotericsoftware/kryo/serializers/JavaSerializerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.esotericsoftware.kryo.KryoTestCase;
2323

2424
import java.io.Serializable;
25+
import java.net.URL;
26+
import java.net.URLClassLoader;
2527

2628
import org.junit.jupiter.api.Test;
2729

@@ -42,6 +44,17 @@ void testJavaSerializer () {
4244
roundTrip(146, test);
4345
}
4446

47+
@Test
48+
void testJavaSerializerFallbackToDefaultClassLoader () {
49+
kryo.setClassLoader(new URLClassLoader(new URL[]{}, null));
50+
51+
kryo.register(TestClass.class, new JavaSerializer());
52+
53+
TestClass test = new TestClass();
54+
test.intField = 54321;
55+
roundTrip(139, test);
56+
}
57+
4558
public static class TestClass implements Serializable {
4659
String stringField;
4760
int intField;

0 commit comments

Comments
 (0)