Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#922 JavaSerializer class resolution fallback #926

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/com/esotericsoftware/kryo/serializers/JavaSerializer.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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