Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can IntelliJ automatically create a decorator class?

Tags:

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?

like image 961
daveb Avatar asked Dec 01 '10 15:12

daveb


People also ask

How do I create a method description in Intellij?

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.

How do I select all constructors in Intellij?

Try either Ctrl-click or Shift-click instead of Ctrl-Shift. All right..


1 Answers

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

like image 72
Jan Thomä Avatar answered Oct 15 '22 10:10

Jan Thomä