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

Unable to make field private java.lang.String java.util.regex.Pattern.pattern accessible: module java.base does not "opens java.util.regex" to unnamed module #1898

Closed
Dacesilian opened this issue May 27, 2021 · 2 comments

Comments

@Dacesilian
Copy link

Dacesilian commented May 27, 2021

With Java 16, toJson(obj) produces error.

java.lang.reflect.InaccessibleObjectException: Unable to make field private java.lang.String java.util.regex.Pattern.pattern accessible: module java.base does not "opens java.util.regex" to unnamed module @223191a6
	at java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357) ~[?:?]
	at java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) ~[?:?]
	at java.lang.reflect.Field.checkCanSetAccessible(Field.java:177) ~[?:?]
	at java.lang.reflect.Field.setAccessible(Field.java:171) ~[?:?]
	at com.google.gson.internal.reflect.UnsafeReflectionAccessor.makeAccessible(UnsafeReflectionAccessor.java:44) ~[gson-2.8.7.jar:?]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:159) ~[gson-2.8.7.jar:?]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102) ~[gson-2.8.7.jar:?]
	at com.google.gson.Gson.getAdapter(Gson.java:458) ~[gson-2.8.7.jar:?]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117) ~[gson-2.8.7.jar:?]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166) ~[gson-2.8.7.jar:?]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102) ~[gson-2.8.7.jar:?]
	at com.google.gson.Gson.getAdapter(Gson.java:458) ~[gson-2.8.7.jar:?]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117) ~[gson-2.8.7.jar:?]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166) ~[gson-2.8.7.jar:?]
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102) ~[gson-2.8.7.jar:?]
	at com.google.gson.Gson.getAdapter(Gson.java:458) ~[gson-2.8.7.jar:?]
	at com.google.gson.Gson.toJson(Gson.java:696) ~[gson-2.8.7.jar:?]
	at com.google.gson.Gson.toJson(Gson.java:683) ~[gson-2.8.7.jar:?]
	at com.google.gson.Gson.toJson(Gson.java:638) ~[gson-2.8.7.jar:?]
	at com.google.gson.Gson.toJson(Gson.java:618) ~[gson-2.8.7.jar:?]
	at com.mycode...

This produces error when working with obj of Class which has private Pattern myRegex field inside.
Workaround is to use private **transient** Pattern myRegex modifier.

@Marcono1234
Copy link
Collaborator

The reason for this is that you are trying to serialize a JDK class (java.util.regex.Pattern) using Gson's reflection-based type adapter. That adapter is used if no built-in adapter exists and you have not provided your own. Since Java 16 applications can by default no longer access JDK internals using reflection, see JDK-8256358.

This issue is similar to #1216; you should avoid using (implicitly) the reflective type adapter for third party classes because you make yourself dependent on the internal implementation of these classes (which could change at any point).
To solve this you should use a custom TypeAdapter for java.util.regex.Pattern.

However, if making the Pattern myRegex field transient in your case solves the issue, then that sounds like the proper solution because your application is apparently not dependent on the myRegex field in the JSON anyways.

imay added a commit to StarRocks/starrocks that referenced this issue Jun 17, 2022
Since Java 16 applications can by default no longer access JDK internals
using reflection, see JDK-8256358.

So I make some field transient to make GSON work.
Refer to google/gson#1898
@Marcono1234
Copy link
Collaborator

Closing because as mentioned above you should not rely on Gson's reflection-based serialization for third-party classes.

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

No branches or pull requests

2 participants