diff --git a/src/com/esotericsoftware/kryo/serializers/ImmutableCollectionsSerializers.java b/src/com/esotericsoftware/kryo/serializers/ImmutableCollectionsSerializers.java index 7e874a2e6..585306f42 100644 --- a/src/com/esotericsoftware/kryo/serializers/ImmutableCollectionsSerializers.java +++ b/src/com/esotericsoftware/kryo/serializers/ImmutableCollectionsSerializers.java @@ -41,6 +41,15 @@ public static void addDefaultSerializers (Kryo kryo) { } } + /** Creates new serializers for all types of {@link java.util.ImmutableCollections}s and registers them. + * + * @param kryo the {@link Kryo} instance to register the serializers on. */ + public static void registerSerializers (Kryo kryo) { + JdkImmutableListSerializer.registerSerializers(kryo); + JdkImmutableMapSerializer.registerSerializers(kryo); + JdkImmutableSetSerializer.registerSerializers(kryo); + } + static class JdkImmutableListSerializer extends CollectionSerializer> { private JdkImmutableListSerializer () { @@ -79,6 +88,14 @@ static void addDefaultSerializers (Kryo kryo) { kryo.addDefaultSerializer(List.of(1, 2, 3, 4).getClass(), serializer); kryo.addDefaultSerializer(List.of(1, 2, 3, 4).subList(0, 2).getClass(), serializer); } + + static void registerSerializers (Kryo kryo) { + final JdkImmutableListSerializer serializer = new JdkImmutableListSerializer(); + kryo.register(List.of().getClass(), serializer); + kryo.register(List.of(1).getClass(), serializer); + kryo.register(List.of(1, 2, 3, 4).getClass(), serializer); + kryo.register(List.of(1, 2, 3, 4).subList(0, 2).getClass(), serializer); + } } static class JdkImmutableMapSerializer extends MapSerializer> { @@ -119,6 +136,13 @@ static void addDefaultSerializers (Kryo kryo) { kryo.addDefaultSerializer(Map.of(1, 2).getClass(), serializer); kryo.addDefaultSerializer(Map.of(1, 2, 3, 4).getClass(), serializer); } + + static void registerSerializers (Kryo kryo) { + final JdkImmutableMapSerializer serializer = new JdkImmutableMapSerializer(); + kryo.register(Map.of().getClass(), serializer); + kryo.register(Map.of(1, 2).getClass(), serializer); + kryo.register(Map.of(1, 2, 3, 4).getClass(), serializer); + } } static class JdkImmutableSetSerializer extends CollectionSerializer> { @@ -158,6 +182,13 @@ static void addDefaultSerializers (Kryo kryo) { kryo.addDefaultSerializer(Set.of(1).getClass(), serializer); kryo.addDefaultSerializer(Set.of(1, 2, 3, 4).getClass(), serializer); } + + static void registerSerializers (Kryo kryo) { + final JdkImmutableSetSerializer serializer = new JdkImmutableSetSerializer(); + kryo.register(Set.of().getClass(), serializer); + kryo.register(Set.of(1).getClass(), serializer); + kryo.register(Set.of(1, 2, 3, 4).getClass(), serializer); + } } } diff --git a/test-jdk11/com/esotericsoftware/kryo/serializers/ImmutableCollectionsSerializersTest.java b/test-jdk11/com/esotericsoftware/kryo/serializers/ImmutableCollectionsSerializersTest.java index c19f89479..a47aeb94f 100644 --- a/test-jdk11/com/esotericsoftware/kryo/serializers/ImmutableCollectionsSerializersTest.java +++ b/test-jdk11/com/esotericsoftware/kryo/serializers/ImmutableCollectionsSerializersTest.java @@ -39,13 +39,8 @@ class ImmutableCollectionsSerializersTest extends KryoTestCase { @BeforeEach public void setUp () throws Exception { super.setUp(); - kryo.register(Class.forName("java.util.ImmutableCollections$List12")); - kryo.register(Class.forName("java.util.ImmutableCollections$ListN")); - kryo.register(Class.forName("java.util.ImmutableCollections$SubList")); - kryo.register(Class.forName("java.util.ImmutableCollections$Map1")); - kryo.register(Class.forName("java.util.ImmutableCollections$MapN")); - kryo.register(Class.forName("java.util.ImmutableCollections$Set12")); - kryo.register(Class.forName("java.util.ImmutableCollections$SetN")); + + ImmutableCollectionsSerializers.registerSerializers(kryo); kryo.register(HashMap.class); kryo.register(TestClass.class); }