简体   繁体   English

Java 无法访问 Kotlin 的 Companion

[英]Java cannot access Kotlin's Companion

I am trying to use kotlinx serialization in java code.我正在尝试在 java 代码中使用 kotlinx 序列化。 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.我遇到的问题是我无法在 java 代码中使用 Companion 对象来访问它的静态 serializer() 方法,该方法由 kotlin serializaton 插件生成。 Here is the kotlin code that decalres a serializable class:这是 decalres 可序列化类的 kotlin 代码:

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

My mavem pom.xml file is shows below我的 mavem pom.xml 文件如下所示

<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):我写的java代码如下(它没有编译,因为它找不到Companion对象):

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.我可以访问其他 kotlin 类的 Companion 对象(例如,JsonElement 的 Companio.serializer()),但无法访问由 kotlinx 序列化插件自动生成的 Companion。 Also, the target class that is autogenerated contains the Companion object, here is the code:此外,自动生成的目标类包含 Companion 对象,代码如下:

    // 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:我可以访问我尝试过的自动生成的复制方法。

UPD: Here is the screenshot from intellij UPD:这是来自 intellij 的屏幕截图在此处输入图像描述

This seems to be only a problem with the Kotlin compiler plugin in IntelliJ IDEA.这似乎只是 IntelliJ IDEA 中 Kotlin 编译器插件的问题。 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.即使 IDEA 认为您的 Java 代码有错误,您实际上仍然可以在命令行上编译它(或使用 Maven 或 Gradle)并且它运行得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM