Java – Accidental Override: The following declarations have the same JVM signature error on specific classes hierarchy

Accidental Override: The following declarations have the same JVM signature error on specific classes hierarchy… here is a solution to the problem.

Accidental Override: The following declarations have the same JVM signature error on specific classes hierarchy

Here I found a lot of similar problems, but no one solution helped me due to the specifics of my problem.
I have the following class hierarchy:

com.package.ParentInterface.java

public interface ParentInterface {
    void setMessages(Collection<String> var1);
}

com.package.ParentClass.java

public class ParentClass {
    protected Collection messages;

public void setMessages(Collection messages)
    {
        this.messages = messages;
    }
}

com.package.ChildClass.java

public class ChildClass extends ParentClass implements ParentInterface {
}

com.package.KotlinClass.kt

class KotlinClass: ChildClass()

In the last Kotlin class I have the following error: ‘Class ‘KotlinClass’ is not abstract and does not implement base class member public abstract fun setMessages(var1: (Mutable)Collection!): Unit defined in com. package. ChildClass.

When I accepted the offer to use the IDE build method implementation, I had:

override fun setMessages(var1: MutableCollection<String>?) {
}

I get the following error on the generated method:

Unintended overwrite: The following declarations have the same JVM signature (setMessages(Ljava/util/Collection;)V:

  • public open fun setMessages(messages: (MutableCollection.. Collection?)): A unit defined in com.package.KotlinClass
  • public open fun setMessages(var1: MutableCollection?): A unit defined in com.package.KotlinClass

I had to change to KotlinClass, because the other classes are all classes of third-party libraries in Java. Please help someone, I’ve spent a lot of time on this issue.

Solution

Well, the answer isn’t actually as strict as it seems – yes and no.
You can overcome this limitation with plain kotlin, but you’ll lose some functionality in the process/may introduce some unwanted but obvious bugs along the way, so you should really investigate where to use this method before proceeding, as you need to basically “cut off” this method all about fixing compilation errors.

So in your specific case, I know you developed the Atlassian plugin for Jira on Kotlin. This is important because we know that this method can be avoided in your case.

The question classes are:
com.atlassian.jira.web.action.JiraWebActionSupport

It implements the interface:
com.atlassian.jira.util.ErrorCollection

The culprits are:
void setErrorMessages(Collection<String> var1);

The parent class is webwork.action.ActionSupport

It contains protected Collection errorMessages;

This is how you cut off this dead limb:

open class SpecificAction : JiraWebActionSupport() {
    override fun setErrorMessages(p0: MutableCollection<String>?) = TODO()
    ...
}

In your case, Java overwriting is certainly preferable because you don’t lose anything and don’t introduce potential bugs, but if you only need kotlin and make sure you don’t/won’t use this method – this dirty little trick will help.

Related Problems and Solutions