Details
-
Sub-task
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
None
-
None
-
Unknown
Description
get exception like
Suppressed: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Map java.util.Collections$UnmodifiableMap.m accessible: module java.base does not "opens java.util" to unnamed module @bf20622 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178) at java.base/java.lang.reflect.Field.setAccessible(Field.java:172) at org.apache.camel.main.MainPropertyPlaceholderWithEnvTest.getFieldValue(MainPropertyPlaceholderWithEnvTest.java:276) at org.apache.camel.main.MainPropertyPlaceholderWithEnvTest.getEditableMapOfVariables(MainPropertyPlaceholderWithEnvTest.java:226) at org.apache.camel.main.MainPropertyPlaceholderWithEnvTest.doEnvVariable(MainPropertyPlaceholderWithEnvTest.java:206) at org.apache.camel.main.MainPropertyPlaceholderWithEnvTest.tearDown(MainPropertyPlaceholderWithEnvTest.java:182) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568)
This is because System.getEnv() returns a java.util.Collections$UnmodifiableMap which isn't supposed to be changed during runtime code. To workaround this, normally we need to use reflection way to make a private field in java.util.Collections$UnmodifiableMap accessible.
But this is kind of against the encapsulation(Strongly Encapsulate JDK Internals ) rule of java Modules since JDK9. Before JDK17, it is it possible to access internal APIs via the --illegal-access command line option which is enabled by default. However, since JDK17 this --illegal-access is removed and we need to either explicitly use --add-opens modules to enable deep reflection for JDK internal code, or we need to get rid of the way to use JDK Internals