Skip to content

Conversation

@Marcono1234
Copy link
Contributor

Follow-up for #2948

  • internal Javadoc additions and changes
  • moved java.time tests to separate test class
  • added additional Maven Surefire Plugin execution which runs java.time test with --add-opens
  • some small code changes

(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.

Comment on lines -887 to +902
// 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.
Copy link
Contributor Author

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.

Copy link
Member

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.

Comment on lines +294 to +295
// Print this to console to make troubleshooting Maven test execution easier
debugPrint("java.time fields are accessible: " + JAVA_TIME_FIELDS_ARE_ACCESSIBLE);
Copy link
Contributor Author

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.


#### Minimum Android API level

- Gson 2.14.0 and newer: API level 23
Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

@eamonnmcmanus eamonnmcmanus left a 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
Copy link
Member

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.
Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

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.

Comment on lines -887 to +902
// 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.
Copy link
Member

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.

* 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),
Copy link
Member

Choose a reason for hiding this comment

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

Nit: s/identical/identically/.

Comment on lines +964 to +967
/**
* 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).
*/
Copy link
Contributor Author

@Marcono1234 Marcono1234 Jan 13, 2026

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants