feign
feign copied to clipboard
InaccessibleObjectException with CompletableFuture on JDK16
Using a basic interface :
public interface SomeResourceClient {
@RequestLine("GET /resources")
CompletableFuture<SomeResource> all();
}
With a basic configuration :
@Configuration
public class FeignConfig {
@Bean
public SomeResourceClient zeltyRoomClient(Gson gson) {
return AsyncFeign.builder()
.client(new OkHttpClient())
.encoder(new GsonEncoder(gson))
.decoder(new GsonDecoder(gson))
.target(SomeResourceClient.class, "https://api.example.org/2.4");
}
}
I have an exception on runtime with JDK 16 :
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field volatile java.lang.Object java.util.concurrent.CompletableFuture.result accessible: module java.base does not "opens java.util.concurrent" to unnamed module @6895a785
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:177)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:171)
at com.google.gson.internal.reflect.UnsafeReflectionAccessor.makeAccessible(UnsafeReflectionAccessor.java:44)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:159)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at com.google.gson.Gson.getAdapter(Gson.java:458)
at com.google.gson.Gson.fromJson(Gson.java:931)
at com.google.gson.Gson.fromJson(Gson.java:897)
at feign.gson.GsonDecoder.decode(GsonDecoder.java:50)
at feign.AsyncResponseHandler.decode(AsyncResponseHandler.java:115)
... 72 more
Everything works perfectly without the CompletableFuture. Is async calls supported on JDK16 ?
Well, we wrote it targeting JDK11, seems JDK16 broke the API somehow.
But, are you sure GSON works with JDK16?
Yes, it works, we were already using it :)
That's really strange that a JDK related item needs to be set accessible. Feels odd and it's not Feign that's responsible here. Anyway, we'll do some testing and see.
Here is your answer, java.util.concurrent
is not open by default: https://www.sebastianbuza.com/2021/02/03/jep-396-and-you-strong-encapsulation-of-the-jdk-internals-is-the-default-java-code-geeks/
Regardless, we'll still see if something related to what's in the CompleteableFuture