[LANG-1685] reflectionToString falls back to toString if reflective access is not permitted.
Description
The ToStringBuilder.reflectionToString() implementation previously threw an exception when access to a field was prohibited by JPMS. This patch adds defensive handling so that, when reflective access is not permitted, the method falls back to invoking the target object's native toString().
Additionally, the change is implemented to work on JVMs that do not support JPMS (JDK 8).
- [x] Read the contribution guidelines for this project.
- [x] Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
- [ ] I used AI to create any part of, or all of, this pull request.
- [x] Run a successful build using the default Maven goal with
mvn; that'smvnon the command line by itself. - [x] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
- [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
- [x] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.
I made a minor code change to refactor. Verified on JDK 8, 11, and 21.
Hi @garydgregory , could you please take a look at this PR and let me know your thoughts? I'd appreciate any feedback you have. Please let me explain our context. I am a programmer working for a consulting company, and we have some clients who are planning to migrate their legacy systems to the latest JDK due to the EOL of older JDKs or middlewares they depend on. However, most of those systems depend on this library, and the ReflectionToString class is used for logging or other features. As a result, this issue is becoming a critical issue for those projects. Of course, --add-opens is a solution to this problem. However, some clients refuse to set those flags for security reasons, although I understand that the main goal of the JPMS is improving modularity, not security. I believe that there must be many other projects that have the same issue."
Hi,@garydgregory ,@iwauo,, is there any contribution that you can assighn to me as a beginner , who only knows java DSA in this project , I would like to contribute, or can you guide me?
Jira is https://issues.apache.org/jira/browse/LANG-1685