Skip to content

Commit

Permalink
#922 Fall back to default class resolution if class cannot be loaded …
Browse files Browse the repository at this point in the history
…with provided class loader
  • Loading branch information
theigl committed Nov 17, 2022
1 parent cb1cd85 commit a19035b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/com/esotericsoftware/kryo/serializers/JavaSerializer.java
Expand Up @@ -84,8 +84,13 @@ private static class ObjectInputStreamWithKryoClassLoader extends ObjectInputStr
protected Class resolveClass (ObjectStreamClass type) {
try {
return Class.forName(type.getName(), false, kryo.getClassLoader());
} catch (ClassNotFoundException ignored) {}
try {
return super.resolveClass(type);
} catch (ClassNotFoundException ex) {
throw new KryoException("Class not found: " + type.getName(), ex);
} catch (IOException ex) {
throw new KryoException("Could not load class: " + type.getName(), ex);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/com/esotericsoftware/kryo/serializers/JavaSerializerTest.java
Expand Up @@ -22,6 +22,8 @@
import com.esotericsoftware.kryo.KryoTestCase;

import java.io.Serializable;
import java.net.URL;
import java.net.URLClassLoader;

import org.junit.jupiter.api.Test;

Expand All @@ -42,6 +44,17 @@ void testJavaSerializer () {
roundTrip(146, test);
}

@Test
void testJavaSerializerFallbackToDefaultClassLoader () {
kryo.setClassLoader(new URLClassLoader(new URL[]{}, null));

kryo.register(TestClass.class, new JavaSerializer());

TestClass test = new TestClass();
test.intField = 54321;
roundTrip(139, test);
}

public static class TestClass implements Serializable {
String stringField;
int intField;
Expand Down

0 comments on commit a19035b

Please sign in to comment.