Accidental override when using third party Java interface and extending RuntimeException in kotlin

Hi,

I’ve been searching on google and stackoverflow and even here for how to handle the error I’m running, but I haven’t found something that addresses the particular thing I’m running into.

I have a Spring Boot (1.5.X) application that I’m converting from Java to Kotlin. Prior to the migration I followed this tutorial in order to add GraphQL support to my service: https://www.pluralsight.com/guides/building-a-graphql-server-with-spring-boot

The issue comes when trying to convert the following Java class to Kotlin:

Java Version:

public class BookNotFoundException extends RuntimeException implements GraphQLError {

    private Map<String, Object> extensions = new HashMap<>();

    public BookNotFoundException(String message, Long invalidBookId) {
        super(message);
        extensions.put("invalidBookId", invalidBookId);
    }

    @Override
    public List<SourceLocation> getLocations() {
        return null;
    }

    @Override
    public Map<String, Object> getExtensions() {
        return extensions;
    }

    @Override
    public ErrorType getErrorType() {
        return ErrorType.DataFetchingException;
    }
}

Kotlin version (converted via IntelliJ):

class BookNotFoundException(message: String, invalidBookId: Long?): RuntimeException(message), GraphQLError {

    private val extensions = HashMap<String, Any>()

    init {
        extensions["invalidBookId"] = invalidBookId as Any
    }

    override fun getMessage(): String {
        return super.message
    }

    override fun getLocations(): List<SourceLocation>? {
        return null
    }

    override fun getExtensions(): Map<String, Any> {
        return extensions
    }

    override fun getErrorType(): ErrorType {
        return ErrorType.DataFetchingException
    }
}

The GraphQLError interface is a third party interface from the graphql-java project.

When I try to implement the getMessage() method I get the following error:

Accidental override: The following declarations have the same JVM signature (getMessage()Ljava/lang/String;):
fun <get-message>(): String? defined in graphqlpoc.exception.BookNotFoundException
fun getMessage(): String defined in graphqlpoc.exception.BookNotFoundException

And if I don’t attempt to implement the getMessage() method I get the following error:

Class ‘BookNotFoundException’ is not abstract and does not implement abstract member public abstract fun getMessage(): String! defined in graphql.GraphQLError

Is there an easy way to get around this? Or should I simply keep the Exception class in Java?

1 Like

Looks more like a bug, than a feature… I think we should create issue for this.

This issue is tracked as KT-6653, there’s a comment covering the case about implementing exception message: https://youtrack.jetbrains.com/issue/KT-6653#comment=27-2666539

1 Like

And there are currently no workaround for it?

2 Likes

Thank you for linking the ticket, @ilya.gorbunov! Like @Beholder I am curious: is there a workaround for this particular use case? Looking at KT-6653 it doesn’t seem as if there’s any way around this.

1 Like

If you can use Java alongside Kotlin in your project, a sad “workaround” is to write this particular class in Java.

1 Like