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

Runtime Hint for unsafeAllocated #29561

Closed
akefirad opened this issue Nov 23, 2022 · 5 comments
Closed

Runtime Hint for unsafeAllocated #29561

akefirad opened this issue Nov 23, 2022 · 5 comments
Labels
status: declined A suggestion or change that we don't feel we should currently apply theme: aot An issue related to Ahead-of-time processing

Comments

@akefirad
Copy link

akefirad commented Nov 23, 2022

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 23, 2022
@bclozel
Copy link
Member

bclozel commented Nov 24, 2022

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!

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2022
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply theme: aot An issue related to Ahead-of-time processing and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 24, 2022
@akefirad
Copy link
Author

akefirad commented Nov 24, 2022

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.

@mhalbritter
Copy link
Contributor

mhalbritter commented Nov 25, 2022

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?

@bclozel
Copy link
Member

bclozel commented Nov 25, 2022

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.

@akefirad
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply theme: aot An issue related to Ahead-of-time processing
Projects
None yet
Development

No branches or pull requests

4 participants