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

error on latest openjdk #1916

Closed
chengweiou opened this issue Jul 4, 2021 · 4 comments
Closed

error on latest openjdk #1916

chengweiou opened this issue Jul 4, 2021 · 4 comments

Comments

@chengweiou
Copy link

Summary:

work on oracle jdk 11, docker openjdk 11.
not work on docker openjdk latest

Request info:

String: {"person":{"id":1}}
Method: new Gson().fromJson(accountJson, Account.class);
Account.class: https://github.com/chengweiou/carina/blob/dev/src/main/java/chengweiou/universe/carina/base/converter/Account.java

Solution for now: (pick one)

  1. using jdk11.
  2. using gsonbuilder to add localdate and localdatetime typeadapter

Exception Stack:

Unable to make field private final java.time.LocalDate java.time.LocalDateTime.date accessible: module java.base does not "opens java.time" to unnamed module @27abe2cd'} java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.time.LocalDate java.time.LocalDateTime.date accessible: module java.base does not "opens java.time" to unnamed module @27abe2cd
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.fromJson(Gson.java:931) ~[gson-2.8.7.jar!/:?]
at com.google.gson.Gson.fromJson(Gson.java:897) ~[gson-2.8.7.jar!/:?]
at com.google.gson.Gson.fromJson(Gson.java:846) ~[gson-2.8.7.jar!/:?]
at com.google.gson.Gson.fromJson(Gson.java:817) ~[gson-2.8.7.jar!/:?]
at chengweiou.universe.carina.interceptor.AuthInterceptorMe.preHandle(AuthInterceptorMe.java:21) ~[classes!/:?]
at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:148) ~[spring-webmvc-5.3.7.jar!/:5.3.7]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1058) [spring-webmvc-5.3.7.jar!/:5.3.7]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) [spring-webmvc-5.3.7.jar!/:5.3.7]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.3.7.jar!/:5.3.7]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.3.7.jar!/:5.3.7]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:497) [jakarta.servlet-api-4.0.4.jar!/:4.0.4]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.3.7.jar!/:5.3.7]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:584) [jakarta.servlet-api-4.0.4.jar!/:4.0.4]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.3.7.jar!/:5.3.7]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.3.7.jar!/:5.3.7]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.3.7.jar!/:5.3.7]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.3.7.jar!/:5.3.7]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96) [spring-boot-actuator-2.5.0.jar!/:2.5.0]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.3.7.jar!/:5.3.7]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.3.7.jar!/:5.3.7]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.3.7.jar!/:5.3.7]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:117) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) [undertow-core-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60) [undertow-core-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43) [undertow-core-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.SendErrorPageHandler.handleRequest(SendErrorPageHandler.java:52) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99) [undertow-servlet-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:387) [undertow-core-2.2.7.Final.jar!/:2.2.7.Final]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:841) [undertow-core-2.2.7.Final.jar!/:2.2.7.Final]
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35) [jboss-threads-3.1.0.Final.jar!/:3.1.0.Final]
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2019) [jboss-threads-3.1.0.Final.jar!/:3.1.0.Final]
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1558) [jboss-threads-3.1.0.Final.jar!/:3.1.0.Final]
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1423) [jboss-threads-3.1.0.Final.jar!/:3.1.0.Final]
at java.lang.Thread.run(Thread.java:831) [?:?]

@Marcono1234
Copy link
Collaborator

The reason for this is that you are trying to serialize a JDK class (java.time.LocalDateTime) using Gson's reflection-based type adapter. That adapter is used if no built-in adapter exists (see also #1059) and you have not provided your own.

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).

using gsonbuilder to add localdate and localdatetime typeadapter

This is the proper solution to this problem. Alternatively you could look for third party projects adding support for LocalDateTime, for example https://github.com/gkopff/gson-javatime-serialisers, but that project might not be actively maintained anymore.

@chengweiou
Copy link
Author

Hi, @Marcono1234
Maybe I didn't explain clearly

Without any changes, the only thing I do to make it work again is switch back to jdk 11

At the beginning,
I was using: java11 + gson:2.8.7
and the api i was using: new Gson().fromJson(string, classType);
I didn't have any adapter.
It worked as expect for a long time.

And then,
Today, sorry, I mean yesterday. I used docker openjdk latest version deployed my project.
This exception was thrown.

And then,
I thought I have changed something made it didn't work,
so I added a localdate and localdatetime adapter. It worked again.

At the end,
I think I shouldn't made any changes that relate to this part of my project,
so I deleted this adapter and used my computer to run my project. It worked fine.
after this, I guess maybe is oraclejdk and openjdk's different. but I tried using docker jdk 11, It also worked.

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Jul 5, 2021

I think I shouldn't made any changes that relate to this part of my project,
so I deleted this adapter and used my computer to run my project. It worked fine.
after this, I guess maybe is oraclejdk and openjdk's different. but I tried using docker jdk 11, It also worked.

No, you really should write a custom TypeAdapter. Your application is currently relying on the internal representation of LocalDateTime; have a look at the JSON data and you should see that it uses the names of the private fields of that class. Your program could break at any point when the internal LocalDateTime implementation changes. And in fact, the exception you are experiencing is related to that. Since JDK 16, you cannot access JDK internals using reflection anymore by default (see JDK-8256358).

So if you want to make sure your application runs on JDK 16 and all future versions your only choice is to write a custom TypeAdapter.

@chengweiou
Copy link
Author

@Marcono1234
Thanks for shared the document. I understand now

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