Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code generation for composition using Eclipse

Effective Java, along with other sources suggest that we should consider using composition over inheritance. I have often found my self achieving such composition by using the Decorator pattern and implementing forwarding methods that delegate invocations to a wrapped object.

However, I often find myself writing many simple forwarding methods of the type:

public void myMethod(String name) {
    instance.myMethod(name);
}

Is there anyway of auto-generating these forwarding methods within Eclipse (3.4.x)?

like image 330
teabot Avatar asked Nov 19 '09 13:11

teabot


2 Answers

In the Source menu choose "Generate Delegate Methods". It will give you a list of methods available on any of your fields that you can have the current object forward to.

like image 155
Jason Gritman Avatar answered Oct 15 '22 23:10

Jason Gritman


You could write a custom template (Window > Preferences > filter on "Template") that has a placeholder for the method name and parameter name.

like image 38
Jeff Storey Avatar answered Oct 15 '22 22:10

Jeff Storey