Add serializers for unmodifiable and synchronized collections#1154
Conversation
|
@theigl thanks for working on this. Do you already know which version might be shipped with this feature? |
|
Hi @nastra. I'm still unhappy about merging this because it uses terminally deprecated code in |
1df82cb to
d3689ce
Compare
d3689ce to
e460f5d
Compare
# Conflicts: # .github/workflows/pr-workflow.yml
There was a problem hiding this comment.
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
UnmodifiableCollectionSerializersandSynchronizedCollectionSerializersthat unwrap the delegate collection/map viaUnsafefield offsets and re-wrap on read/copy. - Added JUnit tests covering round-trip and copy behavior for common unmodifiable/synchronized wrapper variants.
- Exposed small
UnsafeUtilhelpers (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.
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>
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
ClosureSerializerthat 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