fix: use custom Gson deserializers to avoid Java 26+ final field mutation warnings#2434
Open
maxandersen wants to merge 1 commit intojbangdev:mainfrom
Open
fix: use custom Gson deserializers to avoid Java 26+ final field mutation warnings#2434maxandersen wants to merge 1 commit intojbangdev:mainfrom
maxandersen wants to merge 1 commit intojbangdev:mainfrom
Conversation
…tion warnings Fixes jbangdev#2417 Java 26 introduced warnings when Gson uses reflection to mutate final fields during deserialization. Future Java versions will block this entirely. This commit replaces Gson's default reflection-based deserialization with custom JsonDeserializer implementations for all catalog data classes: - CatalogRefDeserializer - TemplateDeserializer - AliasDeserializer - JavaAgentDeserializer - CatalogDeserializer Benefits: - No reflection warnings on Java 26+ - Future-proof (won't break when Java blocks final field mutation) - Keeps final fields for immutability and thread-safety - GraalVM native-image friendly - Maintains compatibility with Java 8+ All existing tests pass. Tested successfully with Java 26. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This was referenced Mar 27, 2026
Collaborator
Author
|
@quintesse you prefer this or we do the flag? |
Contributor
|
I'm okay with the flag, because this a quite a bit of code. I'm guessing the flag will be okay to use for quite some time. If they ever deprecate it we can still use this solution. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2417
Problem
Java 26 introduced warnings when Gson uses reflection to mutate final fields during deserialization:
Future Java versions will block this entirely, breaking JBang's catalog deserialization.
Solution
This PR replaces Gson's default reflection-based deserialization with custom
JsonDeserializerimplementations for all catalog data classes:CatalogRefDeserializer- handlescatalog-refwith alternate namesTemplateDeserializer- handlesfile-refsand propertiesAliasDeserializer- handles 29 fields including runtime options, dependencies, etc.JavaAgentDeserializer- handles nestedJavaAgentstructureCatalogDeserializer- handles top-level catalog with nested mapsThese deserializers use constructor-based initialization instead of reflection, properly handle all
@SerializedNameannotations, and useTypeTokenfor correct generic type handling.Benefits
✅ No reflection warnings on Java 26+
✅ Future-proof - won't break when Java blocks final field mutation
✅ Keeps final fields for immutability and thread-safety
✅ GraalVM native-image friendly
✅ Maintains compatibility with Java 8+
Testing
$ jbang run --java 26 hello@jbangdev world Hello world # No warnings!Comparison with PR #2430
This approach is superior to the manifest workaround in #2430:
This is the recommended Gson pattern for immutable objects with final fields.