java - "Accidental override: The following declarations have the same JVM signature"实现Java接口(interface)时

标签 java kotlin kotlin-interop

我在尝试扩展 RuntimeException 并实现 GraphQLError 时遇到了以下错误接口(interface),用 Java 定义,来 self 的 Kotlin 代码。这是错误:

Accidental override: The following declarations have the same JVM signature (getMessage()Ljava.lang.string;):

public open fun <get-message>(): String? defined in NegativeCountException public open fun getMessage(): String? defined in NegativeCountException

以下是我的代码:

class NegativeCountException() : RuntimeException(), GraphQLError {
  override fun getMessage(): String? {
    TODO("not implemented")
  }
  <...>
}

在哪里 GraphQLError是一个接口(interface),在Java中定义如下:

public interface GraphQLError {
  String getMessage();
  <...>
}

似乎与 getMessage() 冲突定义于 Throwable .

我无法更改接口(interface)的代码,因为它来自库。

如何创建自己的运行时异常,它将实现 GraphQLError ?


PS:我也尝试了以下,并收到了一个非常相似的错误:

class NegativeCountException(override val message: String?) : RuntimeException(), GraphQLError {
  <...>
}

最佳答案

这是一个graphql问题。我的解决方法:

重新实现 GraphQLError、ExceptionWhileDataFetching 和 DataFetcherErrorHandler。

KGraphQLError 是用于自定义错误的“固定”kotlin 接口(interface)(使用 val 而不是 getter)。

在 KDataFetcherErrorHandler 中:将此行中的 ExceptionWhileDataFetching 替换为 KExceptionWhileDataFetching:
val error = ExceptionWhileDataFetching(path, exception, sourceLocation)

KExceptionWhileErrorHandling 实现 GraphQLError。查看代码并将 if (exception is GraphQLError) 的所有实例替换为 (exception is KGraphQLError)

将新的 KDataFetcherErrorHandler 传递给您的 queryExecutionStrategy 和 mutationExecutionStrategy。

您的自定义错误现在可以扩展 Throwable 并实现 KGraphQLError,并得到正确处理。

更多信息在这里:http://graphql-java.readthedocs.io/en/latest/execution.html

关于java - "Accidental override: The following declarations have the same JVM signature"实现Java接口(interface)时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48698699/

相关文章:

android - Kotlin 内部类访问外部类?

java - getLifecycle().getState() 在 onStart() 之后仍然是 CREATED

kotlin - 在 iOS/XCode 的内置 .frameworks 中包含 Kotlin/Native KDocs 文档

java - 部署 bundle 时 Apache-karaf 容器的性能

java - AlphaComposite Transparency 重绘重叠成黑色

Kotlin 协同例程按顺序执行,但仅在生产机器上执行

kotlin - Kotlin中的Jinq-如何将Lambda转换为Java SerializedLambda?

java - setLocation 不适用于扩展 JPanel 的类

HttpClient 中的 JavaScript 上下文

java - 如何在 Kotlin 生成的 Java 代码中禁用 @NonNull/@Nullable 注释