Sometimes, I create a decorator class like this:
class MyInterfaceDecorator implements MyInterface { private final MyInterface delegate; ... constructor taking a MyInterface instance ... @Override public Object someInterfaceMethod(Some argument) { return delegate.someInterfaceMethod(argument); } ... etc, more methods here... }
Can IntelliJ automatically create this class for me?
From the main menu, select Tools | Generate JavaDoc. In the dialog that opens, select a scope — a set of files or directories for which you want to generate the reference, and set the output directory where the generated documentation will be placed.
Try either Ctrl-click or Shift-click instead of Ctrl-Shift. All right..
Update//
I noticed that IntelliJ has a "Generate" option for generating delegate methods. Create a new class:
public class MyDecoratorClass { private MyInterfaceWithManyMethods myInterface; }
Then mark myInterface, go to Menu > Code > Delegate Methods, select all methods you want to wrap and that's it.
//End of update
You could try the "Refactoring" -> "Replace inheritance with delegation" refactoring. It should be able to do this, like this. I call this "Code with Alt+Enter"
Go to the interface you want to generate a decorator for.
public interface MyInterfaceWithManyMethods { void method1(); void method2(); void method3(); }
Press Alt+Enter, select "Implement Interface", give a name to your Decorator like "MyDecorator". This gives you
public class MyDecorator implements MyInterfaceWithManyMethods { public void method1() { } public void method2() { } public void method3() { } }
In new class, select the class name, then "Refactor" -> "Replace inheritance with delegation", select your interface, tick all method names, press enter. You'll get:
public class MyDecorator { private final MyObject object = new MyObject(); public void method1() { object.method1(); } public void method2() { object.method2(); } public void method3() { object.method3(); } private class MyObject implements MyInterfaceWithManyMethods { public void method1() { } public void method2() { } public void method3() { } } }
Delete the inner class and the object initializer manually. You get:
public class MyDecorator { public void method1() { object.method1(); } public void method2() { object.method2(); } public void method3() { object.method3(); } }
Press Alt+Enter on the "object" which is now marked red, select "Create field", select MyInterfaceWithManyMethods.
public class MyDecorator { private MyInterfaceWithManyMethods object; public void method1() { object.method1(); } public void method2() { object.method2(); } public void method3() { object.method3(); } }
Select the object variable, press Alt+Enter, select "Add constructor Parameter":
public class MyDecorator { private MyInterfaceWithManyMethods object; public MyDecorator(MyInterfaceWithManyMethods object) { this.object = object; } public void method1() { object.method1(); } public void method2() { object.method2(); } public void method3() { object.method3(); } }
You see it's all done with a few strokes of Alt+Enter. Reads like a lot of work but it can be done in less than 20 seconds. If you just have like 2 or 3 methods you might be faster with a live template, however if you have many methods with complex signatures you'll get a working result in about 20 seconds with this method. Alt+Enter simply rocks :D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With