Skip to content

Commit

Permalink
Support closures when validating types in CompatibleFieldSerializer (#…
Browse files Browse the repository at this point in the history
…822)

* #821 Add assignable check for `Closure` marker class
  • Loading branch information
theigl authored Apr 17, 2021
1 parent cca03d9 commit ea31c5e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
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());
}
}
}

0 comments on commit ea31c5e

Please sign in to comment.