开发者

Can IntelliJ automatically create a decorator class?

Sometimes, I create a decorator class like this:

class MyInterfaceDecorator implements MyInterface {
   private final MyInterface delega开发者_如何学编程te;
   ... 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?


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


You can perhaps add a file template like:

class ${NAME} implements ${INTERFACE} {
   private final ${INTERFACE} delegate;

   public ${NAME}(final ${INTERFACE} delegate) {
       this.delegate = delegate;
}

and then when you have created the file using this template, just use Alt+Inser and choice delegate Methods. It's not perfect, but this could be a shortcut

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜