java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.Object java.util.Optional.value accessible: module java.base does not "opens java.util" to unnamed module @6b26e945
I am getting this error when I run my JUnit code. Error arises around the following code block.
Optional<RolesDTO> roleDTOEmployee = roles.stream()
.filter(r -> r.getName().equals(RolesEnum.valueOf(roleName).getRoleName())).findFirst();
if (logger.isInfoEnabled()) {
logger.info("roleDTOEmployee {}", gson.toJson(roleDTOEmployee));
}
stacktrace:
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 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:157)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.Gson.toJson(Gson.java:661)
at com.google.gson.Gson.toJson(Gson.java:648)
at com.google.gson.Gson.toJson(Gson.java:603)
at com.google.gson.Gson.toJson(Gson.java:583)
on removing logger.info(), code JUnit runs perfectly. I would like to understand this behaviour. And is there a workaround it, so I don't have to remove logger.
gson.toJson(roleDTOEmployee.orElse(null))work? It seems like gson doesn't supportOptional.