Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Accidental override: The following declarations have the same JVM signature" when implementing Java interface

I faced the following error trying to extend RuntimeException and implement GraphQLError interface, defined in Java, from my Kotlin code. This is the error:

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

The following is my code:

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

where GraphQLError is an interface, defined in Java as shown bellow:

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

Seems like it clashes with getMessage() defined in Throwable.

I can't change code of the interface, because it comes from a library.

How can I create my own runtime exception, that will implement GraphQLError?


PS: I also tried the following, and received a very similar error:

class NegativeCountException(override val message: String?) : RuntimeException(), GraphQLError {
  <...>
}
like image 638
Victor Dombrovsky Avatar asked Feb 09 '18 04:02

Victor Dombrovsky


2 Answers

This is a graphql problem. My workaround:

Reimplement GraphQLError, ExceptionWhileDataFetching and DataFetcherErrorHandler.

KGraphQLError is a "fixed" kotlin interface (with val instead of getters) that you use for your custom errors.

in KDataFetcherErrorHandler: Replace ExceptionWhileDataFetching in this line with KExceptionWhileDataFetching:
val error = ExceptionWhileDataFetching(path, exception, sourceLocation)

KExceptionWhileErrorHandling implements GraphQLError. Look through the code and replace all instances of if (exception is GraphQLError) with (exception is KGraphQLError)

Pass the new KDataFetcherErrorHandler to your queryExecutionStrategy and mutationExecutionStrategy.

Your custom errors can now extend Throwable and implement KGraphQLError, and get handled properly.

More info here: http://graphql-java.readthedocs.io/en/latest/execution.html

like image 74
Togrias Avatar answered Oct 13 '22 09:10

Togrias


I think it would work, when message was not a field in the one class (Throwable) and a method in the other. But it seems kotlin can not resolve the ambiguity when message is a field in the class and a method in the interface. If you have control over your GraphQlError you could do this:

class NegativeCountException() : RuntimeException(), GraphQLError {
    override val message: String?
        get() = super.message
}

interface GraphQLError {
    val message: String?
}
like image 41
Hendrik Marx Avatar answered Oct 13 '22 10:10

Hendrik Marx