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

Support closures when validating types in CompatibleFieldSerializer #822

Merged
merged 3 commits into from
Apr 17, 2021
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: 2 additions & 0 deletions src/com/esotericsoftware/kryo/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.SerializerFactory;
import com.esotericsoftware.kryo.serializers.ClosureSerializer;
import com.esotericsoftware.kryo.serializers.FieldSerializer;
import com.esotericsoftware.kryo.util.Generics.GenericType;

Expand Down Expand Up @@ -234,6 +235,7 @@ public static boolean isAssignableTo (Class<?> from, Class<?> to) {
if (to.isAssignableFrom(from)) return true;
if (from.isPrimitive()) return isPrimitiveWrapperOf(to, from);
if (to.isPrimitive()) return isPrimitiveWrapperOf(from, to);
if (from == ClosureSerializer.Closure.class) return to.isInterface();
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.esotericsoftware.kryo.io.Output;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -476,6 +477,19 @@ void testClassWithObjectField() {
assertEquals(o2, kryo.readClassAndObject(new Input(output2.getBuffer())));
}

@Test
void testClassWithLambdaField () {
CompatibleFieldSerializer.CompatibleFieldSerializerConfig config = new CompatibleFieldSerializer.CompatibleFieldSerializerConfig();
config.setChunkedEncoding(true);
config.setReadUnknownFieldData(true);
kryo.setDefaultSerializer(new CompatibleFieldSerializerFactory());
kryo.register(ClassWithLambdaField.class);
kryo.register(SerializedLambda.class);
kryo.register(ClosureSerializer.Closure.class, new ClosureSerializer());

roundTrip(236, new ClassWithLambdaField());
}

public static class TestClass {
public String text = "something";
public int moo = 120;
Expand Down Expand Up @@ -732,4 +746,25 @@ public boolean equals (Object o) {
return Objects.equals(value, that.value);
}
}

public static class ClassWithLambdaField {

@FunctionalInterface
public interface Callback extends Serializable {
int call();
}

private final Callback callback;

public ClassWithLambdaField () {
callback = () -> 1;
}

public boolean equals (Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClassWithLambdaField that = (ClassWithLambdaField)o;
return Objects.equals(callback.call(), that.callback.call());
}
}
}