-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Follow-up for java.time adapter changes
#2972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Follow-up for java.time adapter changes
#2972
Conversation
gson/src/main/java/com/google/gson/internal/bind/JavaTimeTypeAdapters.java
Show resolved
Hide resolved
| // TODO: update this when we are on at least Android API Level 24. | ||
| // TODO: switch to `Math#toIntExact` when we are on at least Android API Level 24. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: Math#toIntExact throws ArithmeticException (a direct subclass of RuntimeException) instead of IllegalArgumentException which is currently used here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe we should throw ArithmeticException here too then.
| // Print this to console to make troubleshooting Maven test execution easier | ||
| debugPrint("java.time fields are accessible: " + JAVA_TIME_FIELDS_ARE_ACCESSIBLE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that you see in the console for the java-time-test execution:
[INFO] Running com.google.gson.functional.JavaTimeTest
java.time fields are accessible: true
...
and can be sure that it actually compared the results with the reflection-based adapter.
test-graal-native-image/src/test/java/com/google/gson/native_test/JavaTimeTest.java
Show resolved
Hide resolved
|
|
||
| #### Minimum Android API level | ||
|
|
||
| - Gson 2.14.0 and newer: API level 23 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only added this because the animal-sniffer-maven-plugin config had been changed to use the API Level 23 signatures.
But it seems we don't actually use any Android 23 API yet? So alternatively we could revert this here and used the API Level 21 signatures again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think that would make sense. Do you want to add it to this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean revert to 21 API Level signatures again, right?
eamonnmcmanus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is great! Just a few small things.
|
|
||
| #### Minimum Android API level | ||
|
|
||
| - Gson 2.14.0 and newer: API level 23 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think that would make sense. Do you want to add it to this PR?
| @@ -1,10 +1,29 @@ | |||
| /* | |||
| * Copyright (C) 2026 Google Inc. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I should have caught that. :-)
| * recent JDK versions. So here we freeze the representation that was current with JDK 21, in a way | ||
| * that does not use reflection. | ||
| * | ||
| * <p>This class should not directly be used, instead the type adapter factory should be obtained |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fair, since apparently the presence of .internal. in the package name is not enough to discourage people from reaching in and grabbing things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My main concern was actually usage from within Gson (since TypeAdapters is internal as well), to avoid having other places directly access this class and forgetting to account for the potential reflection exceptions.
gson/src/main/java/com/google/gson/internal/bind/JavaTimeTypeAdapters.java
Show resolved
Hide resolved
| // TODO: update this when we are on at least Android API Level 24. | ||
| // TODO: switch to `Math#toIntExact` when we are on at least Android API Level 24. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe we should throw ArithmeticException here too then.
gson/src/test/java/com/google/gson/functional/DefaultTypeAdaptersTest.java
Show resolved
Hide resolved
| * Test for {@code java.time} classes. | ||
| * | ||
| * <p>If reflective access to JDK classes is possible, this test also verifies that the custom | ||
| * adapters behave identical to the reflection-based approach (to ensure backward compatibility), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: s/identical/identically/.
test-graal-native-image/src/test/java/com/google/gson/native_test/JavaTimeTest.java
Show resolved
Hide resolved
| /** | ||
| * Adapter factory for {@code java.time} classes. Returns {@code null} if not supported by the | ||
| * current environment (e.g. too old Android version, without desugaring). | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, maybe this comment I added is incorrect? When java.time is unavailable, I am not sure if trying to load JavaTimeTypeAdapters and accessing the factory would really trigger reflection exceptions. Maybe those would only occur on usage of the factory (which would be bad) or for all non-java.time classes the getName().startsWith("java.time.") would not succeed and the factory returns null (which would be fine).
Do you have an internal test which covers this, and know how this behaves?
(I will see if I can also test this with a dummy Android project)
Follow-up for #2948
java.timetests to separate test classjava.timetest with--add-opens(see also my GitHub review comments in the changed code below)
If you think some of this is not needed or want something changes, please let me know.