3

I would like to use value class in native image:

@JvmInline
value class MyValueClass(val id: UUID)

I marked it for reflection:

@Configuration(proxyBeanMethods = false)
@RegisterReflectionForBinding(MyValueClass::class, ...)
class BeanConfiguration {

However, when running the image I get:

org.graalvm.nativeimage.MissingReflectionRegistrationError: The program tried to reflectively invoke method public final java.util.UUID com.example.MyValueClass.unbox-impl() without it being registered for runtime reflection. Add it to the reflection metadata to solve this problem. See https://www.graalvm.org/latest/reference-manual/native-image/metadata/#reflection for help.
    at org.graalvm.nativeimage.builder/com.oracle.svm.core.reflect.MissingReflectionRegistrationUtils.forQueriedOnlyExecutable(MissingReflectionRegistrationUtils.java:97) ~[na:na]

How I can use inline value classes with GraalVM?

pixel
  • 24,905
  • 36
  • 149
  • 251

1 Answers1

2

That would mean GraalVM Native Image has limitations when working with Kotlin value classes due to its handling of reflection. It might not correctly identify methods and fields that need to be registered for reflection.

In your case, when you use @JvmInline with value classes, Kotlin compiler generates extra methods to "unbox" the value (e.g., unbox-impl).
GraalVM is not aware of this method, and it is not registered for reflection. Hence, you are getting a MissingReflectionRegistrationError.

To solve this problem, you could manually register the unbox-impl method for reflection. However, it is not the best practice because these generated method names could be changed in future Kotlin compiler versions, and it is an internal detail.

For the time being, a safer workaround might be to avoid inline classes where reflection is needed and use a regular class instead. That is obviously a trade-off, as you lose the benefits of inline classes.

Your MyValueClass would become:

class MyValueClass(val id: UUID)

That way, you do not need to register any synthetic methods for reflection in GraalVM Native Image.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250