Affects: 6.0.1
Can we have the ability to add unsafeAllocated
runtime hints in org.springframework.aot.hint.ReflectionHints
. Example use case, Gson. It uses sun.misc.Unsafe
. See also this.
I didn't find anything in the documentation regarding this being left out intentionally, so I'm assuming this ticket is a feature request. Thanks.
Comment From: bclozel
The ReflectionHints
API is not meant to be a 1 to 1 mapping of all the metadata available with GraalVM.
I assume that this piece of metadata could be contributed to the community or in the Gson project directly? The feature you're pointing to is also another way to deal with this problem.
Without knowing about the actual issue, I don't know if the feature is missing something or if Gson needs additional metadata. In both cases, I don't think this is a good reason for adding this API to ReflectionHints
. None of the Spring projects required this so far so we shouldn't publish new public API without a strong use case behind it.
Thanks!
Comment From: akefirad
Right, makes sene. However unfortunately it's not quite straightforward. The hint is not needed for Gson itself. It's needed for the code using Gson.
A less strong use case is for libraries that uses Gson but don't provide runtime hints (e.g. stripe-java). Having ability to use the nice API of Spring to add hints during AoT is just wonderful.
A more stronger use case would be the normal use of Gson as the JSON mapper in Spring Boot application. At least there's one crucial piece of code in Gson that need such reflection for the application code; TypeAdapter
(There are other cases but can't reproduce it right now). That means a normal Spring Boot application (using Gson over Jackson) won't work in native image, if the code is using any TypeAdapter
implementation. Here's a sample project, build the native image, run and call :8080/user
:
Type com.example.gsonnativeimage.UserJsonAdapter is instantiated reflectively but was never registered. Register the type by adding "unsafeAllocated" for the type in reflect-config.json.
Right now, the only solution is to manually write a reflect-config.json
file for such cases.
Comment From: mhalbritter
I've seen this message in the past and it is misleading, because in my cases I could get it working without unsafeAllocated. Are you sure that it doesn't work without unsafeAllocated
just by registering the type and its constructors?
Comment From: bclozel
Good point @mhalbritter - libraries can use the unsafe allocation as a fallback mechanism if they don't find a constructor.
I've tested your sample with the following change and it works:
@RestController
@RequestMapping("/users")
@RegisterReflectionForBinding(UserJsonAdapter.class) // registering your adapter for reflection
public class UserController {
Since the adapter itself is not present anywhere in the controller signature we cannot detect it. Looking at GraalVM's GsonFeature, I'm not sure this is supposed to be supported at all.
Comment From: akefirad
(For some reason I cannot reproduce the exact error message I pasted here, it still fails but without the stacktrace.)
You're right. The minimal hint needed is the default constructor: "methods":[{"name":"<init>","parameterTypes":[] }]
.
Thanks.