1

I am trying to use kotlinx serialization in java code. The problem I encounter is that I cannot use Companion object in java code to access it's static serializer() method which is generated by kotlin serializaton plugin. Here is the kotlin code that decalres a serializable class:

@kotlinx.serialization.Serializable
data class MyData(private val data: String)

My mavem pom.xml file is shows below

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>TestProject</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>18</maven.compiler.source>
    <maven.compiler.target>18</maven.compiler.target>
    <kotlin.version>1.7.0</kotlin.version>
    <serialization.version>1.3.3</serialization.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
        <version>${kotlin.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test</artifactId>
        <version>${kotlin.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-serialization-protobuf-jvm -->
    <dependency>
        <groupId>org.jetbrains.kotlinx</groupId>
        <artifactId>kotlinx-serialization-json-jvm</artifactId>
        <version>1.3.3</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jvmTarget>1.8</jvmTarget>
                <compilerPlugins>
                    <plugin>kotlinx-serialization</plugin>
                </compilerPlugins>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-serialization</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>17</source>
                <target>17</target>
            </configuration>
        </plugin>
    </plugins>
</build>

And java code I wrote is below(it does not compile because it cannot find Companion object):

public class Main {

public static void main(String[] args) {
    MyData.Companion.serializer();
}

}

I can access Companion objects of other kotlin classes(for example, the Companio.serializer() of JsonElement) but cannot access Companion that is autogenerated by kotlinx serialization plugin. Also, the target class that is autogenerated contains the Companion object, here is the code:

    // IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available

@kotlinx.serialization.Serializable public final data class MyData public constructor(data: kotlin.String) {
    public companion object {
    }

    @kotlin.Deprecated public constructor(seen1: kotlin.Int, data: kotlin.String?, serializationConstructorMarker: kotlinx.serialization.internal.SerializationConstructorMarker?) { /* compiled code */ }

    private final val data: kotlin.String /* compiled code */

    private final operator fun component1(): kotlin.String { /* compiled code */ }

    @kotlin.Deprecated public object `$serializer` : kotlinx.serialization.internal.GeneratedSerializer<MyData> {
    }
}

UPD: I can access the auto-generated copy method that I tried.

UPD: Here is the screenshot from intellij enter image description here

Barracuda
  • 477
  • 5
  • 19
  • This is very interesting. The companion object shown in the decompiled class in your screenshot does not have any members. However, if you go Tools -> Kotlin -> Show Kotlin Bytecode, and then click Decompile, you'll get a companion object that _does_ have the serializer() method. And yet it's still not available from Java. Very strange. – k314159 Jun 22 '22 at 13:16

1 Answers1

0

This seems to be only a problem with the Kotlin compiler plugin in IntelliJ IDEA. Even though IDEA thinks you have an error in your Java code, you can actually still compile it on the command line (or using Maven or Gradle) and it runs just fine.

k314159
  • 5,051
  • 10
  • 32