0

Automated delegation in Java

In the link above, the user asks a similar question but the answer given required using reflection.

My question is whether is a refactoring tool that will automatically generate all the one-liner method for each of the delegating class automatically.

Intellij offers a "Replace inheritance with delegation" tool, but it doesn't do the job properly - when I use it creates a big mess of duplicate methods and other weird behavior that makes it worse than actually doing it manually (I think it's because multiple interfaces it tries to delegate that are clashing with each other and causing a whole mess.

Am I doing it wrong or perhaps it's a bug or limitation in IntelliJ?

Any workaround that I can use instead of spending my time copy-pasting code?

EDIT:

I've isolated the problem as follow (each class is in a separate file of course):

class TestClass extends MyClass {
    void function() {}
}
class MyClass extends MyAbstractClass implements MyInterface {
    @Override
    void function() {}
}
abstract class MyAbstractClass {
    abstract void function();
}
interface MyInterface {
    void function();
}

Now, if I use "Replace Inheritance With Delegation" on TestClass I get the following result:

class TestClass implements MyInterface {
    private final MyMyClass myClass = new MyMyClass();

    void function() {
        myClass.function();
    }

    void function() {
        myClass.function();
    }

    private class MyMyClass extends MyClass {
        void function() {}
    }
}

So function() appears twice, which I believe should not since naturally the code can't compile with two function with identical signature, forcing me to manually remove one of them which is tedious with actual classes with many functions with the same signature

Try it for example with MyClass<E> extends HashSet<E> and see what a mess it becomes. I could select the specific functions to delegate and make sure I don't select only 1 method with identical signatures, but this makes this whole refactoring pointless - it would be faster for me to just write the forwarding functions myself...

Community
  • 1
  • 1
traveh
  • 2,700
  • 3
  • 27
  • 44
  • That doesn't _seem_ like it should be that much harder to write than the other stuff IDEs implement, like intellitype. "Bug in IntelliJ" is my knee-jerk instinct. – Vyross Apr 04 '16 at 17:18
  • It's impossible to say whether it's a bug in IntelliJ without seeing the code on which you invoke the refactoring. – yole Apr 04 '16 at 21:00
  • @yole I have added a simplified code example – traveh Apr 05 '16 at 08:05
  • Yes, this looks like a bug. You may want to file an issue at http://youtrack.jetbrains.com/ – yole Apr 05 '16 at 12:00

1 Answers1

1

Generate a child class overriding all methods, and then replace super. with delegate.. Some IDEs even have this option.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Awesome, that a great workaround, I will use it. though the original problem I described is a bug in IntelliJ refactoring, right? – traveh Apr 05 '16 at 08:28