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

KotlinReflectionInternalError in a GraalVM image #2088

Open
sgammon opened this issue Nov 7, 2022 · 2 comments
Open

KotlinReflectionInternalError in a GraalVM image #2088

sgammon opened this issue Nov 7, 2022 · 2 comments
Assignees

Comments

@sgammon
Copy link

sgammon commented Nov 7, 2022

Describe the bug
While running KotlinX serialization-equipped code in a GraalVM native image with given definition:

@Serializable
data class GCEInstanceMetadata(
  val id: Long,
  val name: String,
  val hostname: String,
  // ...
)

... this exception is raised at class-loading time, without invoking the serializer:

kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class com.some.package.path.GCEInstanceMetadata$$serializer
	at kotlin.reflect.jvm.internal.KClassImpl.reportUnresolvedClass(KClassImpl.kt:328)
	at kotlin.reflect.jvm.internal.KClassImpl.access$reportUnresolvedClass(KClassImpl.kt:44)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:56)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.invoke(KClassImpl.kt:48)
	at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:93)
	at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.getDescriptor(KClassImpl.kt:48)
	at kotlin.reflect.jvm.internal.KClassImpl.getDescriptor(KClassImpl.kt:182)
	at kotlin.reflect.full.KClasses.getCompanionObject(KClasses.kt:48)

Obviously, one must include this class for reflection (as per #1125), so in my reflect-config.json, I have:

  {
    "name": "com.some.package.path.GCEInstanceMetadata",
    "queryAllDeclaredMethods": true,
    "allDeclaredFields": true,
    "queryAllDeclaredConstructors": true,
    "methods": [
      {
        "name": "<init>",
        "parameterTypes": [
          "long",
          "java.lang.String",
          "java.lang.String",
          "java.lang.String",
          "java.util.List",
          "java.lang.String",
          "java.lang.String",
          "java.lang.String",
          "java.util.Map",
          "java.util.Map",
          "java.util.Map"
        ]
      }
    ]
  },
  {
    "name": "com.some.package.path.GCEInstanceMetadata$$serializer"
  },
  {
    "name": "com.some.package.path.GCEInstanceMetadata$Companion",
    "queriedMethods": [
      {
        "name": "serializer",
        "parameterTypes": [

        ]
      }
    ]
  }

This reflection configuration is generated by the GraalVM agent. Is there anything missing that would preclude Kotlin from loading it via reflection?

Note that we are not actually using the serializer yet, this is actually reached just by trying to acquire a ::class reference which is marked @Serializable.

I can cross-file with GraalVM but I'm curious if the Kotlin team sees anything wrong with this agent-generated config. Maybe it's missing something simple.

To Reproduce
Will attach if needed

Expected behavior
Should load and reflect properly

Environment

  • Kotlin version: 1.7.20
  • Library version: 1.4.1
  • Kotlin platforms: JVM
  • Gradle version: 7.5.1
@sandwwraith
Copy link
Member

Am I right that your issue is that this exception happens even if you explicitly add the $serializer class in the config?

I think the exception is raised on obtaining ::class reference because companion object info is needed for constructing KClass, but I can't say for sure with this short stacktrace.

I don't see any problems with GraalVM config, but @qwwdfsad has had more experience with it so he may have an idea.

@sgammon
Copy link
Author

sgammon commented Nov 9, 2022

@sandwwraith yes. for now we've avoided it by serializing these classes using Jackson which seems to work okay but we'll keep digging to find a root cause.

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

3 participants