8

I'm working on a Swing look&feel using kotlin. In order to create a UI, Swing requires to have a static method createUI with the following signature:

class ButtonUI: BasicButtonUI() {
    ...
    companion object {
        @JvmStatic fun createUI(p0: JComponent): ComponentUI {
           ...
        }
    }
}

and then it is called via reflection in Swing code:

m = uiClass.getMethod("createUI", new Class[]{JComponent.class});

Unfortunately, the code above cannot be compiled by the kotlin compiler because of:

Error:(88, 9) Kotlin: Accidental override: The following declarations have the same JVM signature (createUI(Ljavax/swing/JComponent;)Ljavax/swing/plaf/ComponentUI;):
    fun createUI(c: JComponent): ComponentUI
    fun createUI(p0: JComponent!): ComponentUI!

Is there a workaround for this case?

2
  • Did you try @JvmStatic override fun createUI?
    – shiftpsh
    Jun 17, 2017 at 17:57
  • @shiftpsh it's not applicable within the object declaration.
    – dimafeng
    Jun 17, 2017 at 20:11

2 Answers 2

5

it's a kotlin bug KT-12993. Unfortunately, the bug is not fixed yet. just using implements your ButtonUI or switch between and kotlin to solving the problem if you want to let kotlin implements your ui logic. for example, you should define a peer between and kotlin.

the java code as below:

public class ButtonUI extends BasicButtonUI {
    private ButtonUIPeer peer;

    public ButtonUI(ButtonUIPeer peer) {
        this.peer = peer;
    }

    @Override
    public void installUI(JComponent c) {
        peer.installUI(c, () -> super.installUI(c));
    }

    // override other methods ...


    public static ComponentUI createUI(JComponent c) {
        // create the peer which write by kotlin 
        //                        |
        return new ButtonUI(new YourButtonUIPeer());
    }
}


interface ButtonUIPeer {
    void installUI(Component c, Runnable parentCall);
    //adding other methods for the ButtonUI
}

the kotlin code as below:

class YourButtonUIPeer : ButtonUIPeer {
    override fun installUI(c: Component, parentCall: Runnable) {
      // todo: implements your own ui logic
    }
}

IF you have more than half dozen methods to implements, you can using the Proxy Design Pattern just delegate request to the target ButtonUI which implemented in kotlin (many IDE support generates delegate methods for a field). for example:

public class ButtonUIProxy extends BasicButtonUI {
    private final BasicButtonUI target;
    //1. move the cursor to here ---^
    //2. press `ALT+INSERT`
    //3. choose `Delegate Methods` 
    //4. select all public methods and then click `OK`

    public ButtonUIProxy(BasicButtonUI target) {
        this.target = target;
    }

    public static ComponentUI createUI(JComponent c){
        // class created by kotlin     ---v
        return new ButtonUIProxy(new ButtonUI());
    }
}
2
  • thank you for the reply. I'm not sure that the bug you're referring to describes the same problem. I'm using fallback to java as a workaround right now, but I'm looking for a more solid approach.
    – dimafeng
    Jun 17, 2017 at 20:22
  • @dimafeng this is the actually bug.
    – holi-java
    Jun 17, 2017 at 20:33
3

In latest version of Kotlin 1.3.70 the error can be suppressed with @Suppress("ACCIDENTAL_OVERRIDE"). I am not sure since which version it works.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.