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

Fix #529: Support serializing the Enum class object #530

Merged
merged 1 commit into from
Aug 12, 2017
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
2 changes: 1 addition & 1 deletion src/com/esotericsoftware/kryo/Kryo.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public Registration getRegistration (Class type) {
if (Proxy.isProxyClass(type)) {
// If a Proxy class, treat it like an InvocationHandler because the concrete class for a proxy is generated.
registration = getRegistration(InvocationHandler.class);
} else if (!type.isEnum() && Enum.class.isAssignableFrom(type)) {
} else if (!type.isEnum() && Enum.class.isAssignableFrom(type) && !Enum.class.equals(type)) {
// This handles an enum value that is an inner class. Eg: enum A {b{}};
registration = getRegistration(type.getEnclosingClass());
} else if (EnumSet.class.isAssignableFrom(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,12 @@ static public class EnumSerializer extends Serializer<Enum> {

public EnumSerializer (Class<? extends Enum> type) {
enumConstants = type.getEnumConstants();
if (enumConstants == null) throw new IllegalArgumentException("The type must be an enum: " + type);
// We allow the serialization of the (abstract!) Enum.class (instead of an actual "user" enum),
// which also creates an EnumSerializer instance during Kryo.writeClass with the following trace:
// ClassSerializer.write -> Kryo.writeClass -> DefaultClassResolver.writeClass
// -> Kryo.getDefaultSerializer -> ReflectionSerializerFactory.makeSerializer(kryo, EnumSerializer, Enum.class)
// This EnumSerializer instance is expected to be never called for write/read.
if (enumConstants == null && !Enum.class.equals(type)) throw new IllegalArgumentException("The type must be an enum: " + type);
}

public void write (Kryo kryo, Output output, Enum object) {
Expand Down
2 changes: 2 additions & 0 deletions test/com/esotericsoftware/kryo/DefaultSerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public void testClassSerializer () {
kryo.writeObject(out, void.class);
kryo.writeObject(out, ArrayList.class);
kryo.writeObject(out, TestEnum.class);
kryo.writeObject(out, Enum.class);

final Input in = new Input(out.getBuffer());

Expand All @@ -336,6 +337,7 @@ public void testClassSerializer () {
assertEquals(void.class, kryo.readObject(in, Class.class));
assertEquals(ArrayList.class, kryo.readObject(in, Class.class));
assertEquals(TestEnum.class, kryo.readObject(in, Class.class));
assertEquals(Enum.class, kryo.readObject(in, Class.class));
}

public void testLocaleSerializer () {
Expand Down