You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
The text was updated successfully, but these errors were encountered:
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.
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
With Java 16,
toJson(obj)
produces error.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.The text was updated successfully, but these errors were encountered: