Skip to content

Add serializers for unmodifiable and synchronized collections#1154

Merged
theigl merged 20 commits into
masterfrom
jdk-collections
Jun 23, 2026
Merged

Add serializers for unmodifiable and synchronized collections#1154
theigl merged 20 commits into
masterfrom
jdk-collections

Conversation

@theigl

@theigl theigl commented Dec 8, 2024

Copy link
Copy Markdown
Collaborator

This PR adds serializers for JDK unmodifiable and synchronized collections.

This has been one of the most requested features for Kryo, but I have so far rejected all requests and redirected users to the kryo-serializers project because it is not possible to implement these serializers without reflection. Since this comes up again and again an we already have the same issue with the ClosureSerializer that has been part of Kryo for many years, I decided to give up my opposition.

The serializers need to be registered manually for now to maintain backwards compatibility, but will be added as default serializers in Kryo 6.

Note: The unsafe logic will probably have to be replaced with Varhandles for JDK24+.

#689
#885
#909
#913
#997
#1022

@theigl theigl self-assigned this Dec 8, 2024
@nastra

nastra commented Apr 29, 2025

Copy link
Copy Markdown

@theigl thanks for working on this. Do you already know which version might be shipped with this feature?

@theigl

theigl commented Apr 29, 2025

Copy link
Copy Markdown
Collaborator Author

Hi @nastra. I'm still unhappy about merging this because it uses terminally deprecated code in Unsafe, but I think I will include it in Kryo 5.7.0. The release will probably happen in June or July.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces built-in (manually-registered) Kryo serializers for JDK Collections.*unmodifiable* and Collections.*synchronized* wrappers, enabling round-trip serialization/copy without relying on default reflective construction of these wrapper types.

Changes:

  • Added UnmodifiableCollectionSerializers and SynchronizedCollectionSerializers that unwrap the delegate collection/map via Unsafe field offsets and re-wrap on read/copy.
  • Added JUnit tests covering round-trip and copy behavior for common unmodifiable/synchronized wrapper variants.
  • Exposed small UnsafeUtil helpers (objectFieldOffset, getObject) used by the new serializers.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
src/com/esotericsoftware/kryo/unsafe/UnsafeUtil.java Adds Unsafe helper wrappers used to read internal wrapper delegate fields.
src/com/esotericsoftware/kryo/serializers/UnmodifiableCollectionSerializers.java New serializers for Collections.unmodifiable* wrappers with manual registration/default-serializer helpers.
src/com/esotericsoftware/kryo/serializers/SynchronizedCollectionSerializers.java New serializers for Collections.synchronized* wrappers, including synchronized write behavior.
test/com/esotericsoftware/kryo/serializers/UnmodifiableCollectionsSerializersTest.java New round-trip/copy test coverage for unmodifiable wrappers.
test/com/esotericsoftware/kryo/serializers/SynchronizedCollectionSerializersTest.java New round-trip/copy test coverage for synchronized wrappers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/com/esotericsoftware/kryo/serializers/SynchronizedCollectionSerializers.java Outdated
Comment thread src/com/esotericsoftware/kryo/serializers/SynchronizedCollectionSerializers.java Outdated
Comment thread src/com/esotericsoftware/kryo/serializers/UnmodifiableCollectionSerializers.java Outdated
Comment thread src/com/esotericsoftware/kryo/serializers/UnmodifiableCollectionSerializers.java Outdated
theigl and others added 5 commits June 23, 2026 10:25
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@theigl theigl merged commit 4a41970 into master Jun 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants