Reduce code duplication#1
Merged
MukjepScarlet merged 4 commits intoNov 8, 2025
Merged
Conversation
Marcono1234
commented
Nov 7, 2025
| stringToConstant = new HashMap<>(); | ||
| constantToName = new EnumMap<>(classOfT); | ||
| // Don't use `EnumMap` here; that can break when using ProGuard or R8 | ||
| constantToName = new HashMap<>(); |
Author
There was a problem hiding this comment.
Have reverted this to a HashMap because the EnumMap was causing issues for the test-shrinker integration tests; the JDK was unable to create the EnumMap for the enum
Owner
There was a problem hiding this comment.
What issue does EnumMap have? I'm not sure because we have the classOfT here
Author
There was a problem hiding this comment.
Run mvn clean verify --projects test-shrinker --also-make, it will fail with the following error when using EnumMap:
...
Caused by: java.lang.RuntimeException: Test failed: Write: Enum
at com.example.ai.a(Unknown Source)
at com.example.Main.runTests(Unknown Source)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
... 39 more
Caused by: java.lang.NullPointerException: Cannot read the array length because "this.keyUniverse" is null
at java.base/java.util.EnumMap.<init>(EnumMap.java:139)
at com.a.a.b.a.i.<init>(Unknown Source)
at com.a.a.b.a.i.<init>(Unknown Source)
...I think the issue is that ProGuard and R8 shrink the enum class so much that the JDK cannot obtain the enum constants. This is also why EnumTypeAdapter uses class#getDeclaredFields() and isEnumConstant() instead of simply Class#getEnumConstants().
MukjepScarlet
approved these changes
Nov 8, 2025
82e935a
into
MukjepScarlet:gson_default
10 of 11 checks passed
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.
Resolves some of the issues with google#2864, mainly:
GsonBuilderHelperis not creating the list of factories anymoreSee also my review comments below for additional explanations for the changes.
Feel free to adjust the changes here in case you don't want to directly integrate them as is into your PR.
Generally I really like your approach though!