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

Kotlin serialization issue #996

Closed
mipastgt opened this issue Jan 19, 2025 · 1 comment
Closed

Kotlin serialization issue #996

mipastgt opened this issue Jan 19, 2025 · 1 comment

Comments

@mipastgt
Copy link

I am trying to convert some Kotlin code, which was written for the JVM and uses some Java libraries, to JavaScript (and if that works later to Wasm and C too). This code also uses Kotlin serialization (JSON) which I thought would be no problem because Kotlin serialization does not use reflection. But it turns out that this does not work because some standard Java methods are missing.

These are:

java.lang.Class.isAnonymousClass()Z
java.lang.Class.getEnclosingMethod()Ljava/lang/reflect/Method
java.lang.Class.getEnclosingConstructor()Ljava/lang/reflect/Constructor

Is there any chance that these three missing methods could be implemented?

The full error output is:

Executing 'build'…

> Task :copyWasmGCRuntime
> Task :checkKotlinGradlePluginConfigurationErrors SKIPPED
> Task :processResources NO-SOURCE
> Task :processTeavmResources NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :compileKotlin
> Task :compileJava NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTeavmKotlin NO-SOURCE
> Task :compileTeavmJava NO-SOURCE
> Task :teavmClasses UP-TO-DATE

> Task :generateWasmGC FAILED

[Incubating] Problems report is available at: file:///Users/mpaus/Projects/tmp/TeaVMTestGradle/build/reports/problems/problems-report.html

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.12/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
3 actionable tasks: 3 executed
Method java.lang.Class.isAnonymousClass()Z was not found
    at kotlin.jvm.internal.ClassReference$Companion.getClassSimpleName(ClassReference.kt:166)
    at kotlin.jvm.internal.ClassReference.getSimpleName(ClassReference.kt:12)
    at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:114)
    at kotlinx.serialization.PolymorphicSerializerKt.findPolymorphicSerializer(PolymorphicSerializer.kt:109)
    at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:250)
    at kotlinx.serialization.json.internal.JsonStreamsKt.encodeByWriter(JsonStreams.kt:99)
    at kotlinx.serialization.json.Json.encodeToString(Json.kt:125)
    at de.mpmediasoft.teavmtest.Client$Companion.dispatch(Client.kt:68)
    at de.mpmediasoft.teavmtest.Client.dispatch(-1)
    at de.mpmediasoft.teavmtest.Client.dispatch$exported$1
Method java.lang.Class.getEnclosingMethod()Ljava/lang/reflect/Method; was not found
    at kotlin.jvm.internal.ClassReference$Companion.getClassSimpleName(ClassReference.kt:169)
    at kotlin.jvm.internal.ClassReference.getSimpleName(ClassReference.kt:12)
    at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:114)
    at kotlinx.serialization.PolymorphicSerializerKt.findPolymorphicSerializer(PolymorphicSerializer.kt:109)
    at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:250)
    at kotlinx.serialization.json.internal.JsonStreamsKt.encodeByWriter(JsonStreams.kt:99)
    at kotlinx.serialization.json.Json.encodeToString(Json.kt:125)
    at de.mpmediasoft.teavmtest.Client$Companion.dispatch(Client.kt:68)
    at de.mpmediasoft.teavmtest.Client.dispatch(-1)
    at de.mpmediasoft.teavmtest.Client.dispatch$exported$1
Method java.lang.Class.getEnclosingConstructor()Ljava/lang/reflect/Constructor; was not found
    at kotlin.jvm.internal.ClassReference$Companion.getClassSimpleName(ClassReference.kt:170)
    at kotlin.jvm.internal.ClassReference.getSimpleName(ClassReference.kt:12)
    at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:114)
    at kotlinx.serialization.PolymorphicSerializerKt.findPolymorphicSerializer(PolymorphicSerializer.kt:109)
    at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:250)
    at kotlinx.serialization.json.internal.JsonStreamsKt.encodeByWriter(JsonStreams.kt:99)
    at kotlinx.serialization.json.Json.encodeToString(Json.kt:125)
    at de.mpmediasoft.teavmtest.Client$Companion.dispatch(Client.kt:68)
    at de.mpmediasoft.teavmtest.Client.dispatch(-1)
    at de.mpmediasoft.teavmtest.Client.dispatch$exported$1

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':generateWasmGC'.
> Errors occurred during TeaVM build

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 3s
11:50:34: Execution finished 'build'.

@konsoletyper
Copy link
Owner

Although technically it's possible to implement such methods, practically this does not makes sense and makes life for everyone just worse. Instead, I usually just patch Kotlin runtime bytecode to avoid such calls. In the example all three missing methods are called from AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered, so I'd patch this method instead.

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

2 participants