java.util.ImmutableCollections$ListN can contain null values
Describe the bug
In older java versions, List::of was the only way to create a java.util.ImmutableCollections$ListN object, but now Stream::toList is also capable of creating them, and it does not have the limitation of not containing nulls
To Reproduce
There are two slightly different issues. It should be possible to fix both at the same time.
In this case, immutableWithNulls only has null values in it, so kryo successfully serializes it, but then can not deserialize it because it ends up calling List.of(null, null)
public void testImmutableDeserialization() throws Exception {
final Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);
final Output output = new Output(64);
final List<Integer> immutableWithNulls = Stream.of(null,1, null).filter(x -> x == null || x > 10).toList();
System.out.println(immutableWithNulls.getClass());
kryo.writeClassAndObject(output, immutableWithNulls);
try(Input input = new Input(output.getBuffer())) {
final Object copy = kryo.readClassAndObject(input);
assert immutableWithNulls.equals(copy);
}
In this case, immutableWithNulls has both null and non-null values, so kryo catches that there are null values in a supposedly nonnull container during serialization
}
@Test
public void testImmutableSerialization() throws Exception {
final Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);
final Output output = new Output(64);
final List<Integer> immutableWithNulls = Stream.of(null,1, null).toList();
kryo.writeClassAndObject(output, immutableWithNulls);
try(Input input = new Input(output.getBuffer())) {
final Object copy = kryo.readClassAndObject(input);
assert immutableWithNulls.equals(copy);
}
Environment:
- OS: Ubuntu running in WSL
- JDK 21 (but Stream::toList was added in 16. I have no clue if that is when it started returning a ListN)
- Kryo 5.5
Additional context Add any other context about the problem here.