Skip to content
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

WARNING: Illegal reflective access by com.google.gson.internal.reflect.ReflectionHelper #2506

Closed
2 tasks
agitrubard opened this issue Oct 5, 2023 · 3 comments
Closed
2 tasks
Labels

Comments

@agitrubard
Copy link

agitrubard commented Oct 5, 2023

Gson version

We are using Gson version is 2.10.1

Java version

Java11

Used tools

  • Maven; version: 3.9.4
  • Spring; version: 2.7.5

In our Spring application, we are receiving a warning as follows. We were hoping not to encounter such an error without any additional development on this issue because there have been issues opened on this topic before. Even though we applied the solution shared in issue #2220, it did not help in any way to resolve this warning. Can you provide a concrete solution or suggestion to address this issue?

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.gson.internal.reflect.ReflectionHelper (file:/Users/{username}/.m2/repository/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar) to field java.time.LocalDateTime.date
WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.reflect.ReflectionHelper
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
@agitrubard agitrubard added the bug label Oct 5, 2023
@Marcono1234
Copy link
Collaborator

Gson currently does not have built-in adapters for java.time classes yet (see #1059).

Even though we applied the solution shared in issue #2220, it did not help in any way to resolve this warning.

Do you mean you have created a custom TypeAdapter for java.time.LocalDateTime and registered it with Gson? In that case you should not see this warning anymore. Maybe for some reason your custom adapter is not used then.

Could you please try to reduce this to a small self-contained example and provide the code here (or create a demo repository if necessary)?

Also note that there are some third-party extensions to Gson which provide support for java.time classes, for example https://github.com/gkopff/gson-javatime-serialisers. However, I don't know how well they work.

@agitrubard
Copy link
Author

agitrubard commented Oct 6, 2023

Hello, first of all, thank you for your prompt response. I haven't encountered a structure like TypeAdapter before. Could you provide some details about this structure? If possible, do you have a code snippet as an example? Additionally, what kind of need can arise for this?

To make the warning clearer, I prepared a demo application. You can perform your tests with this demo application and provide feedback. If you'd like, you can set up the TypeAdapter structure based on this demo and share it with us again. I would greatly appreciate it.

gson-warning-demo.zip

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Oct 6, 2023

Your classes use java.time.LocalDateTime. Gson has no built-in adapter for that class at the moment. In these cases it falls back to using Java reflection to access the fields of the class. While this is desired for you custom classes User and Role, this should be avoided for third-party classes because Gson accesses even private fields, which normally represent implementation details of these third-party classes and they could change at any point. This can be seen here with LocalDateTime; the JSON output you currently receive represents the internal structure of that class. The JDK developers decided to disallow such reflective access to the internal implementation details. In the JDK version you are using this only results in a warning, in later versions such reflective access to implementation details will result in an exception (see JEP 403). The Gson Troubleshooting Guide also has a related section about this.

A Gson TypeAdapter can be used to customize how Gson converts a class from or to JSON. In this example you could use it to overwrite the default reflection-based serialization for LocalDateTime.

If you'd like, you can set up the TypeAdapter structure based on this demo and share it with us again. I would greatly appreciate it.

Maybe it would be easiest if you used a third-party library which adds support for this to Gson, such as https://github.com/gkopff/gson-javatime-serialisers mentioned above (though I cannot make any guarantees about how well it works). They have created these custom TypeAdapter implementations for the java.time classes. You can also have a look at Stack Overflow, there are some related questions about other java.time classes, such as this one about LocalDate.

I hope this answer wasn't too verbose.

I will close this issue since java.time support is tracked in the other issue. Though feel free to comment here again if the solution mentioned above does not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants