Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Eclipse generate method-chaining setters

Tags:

I'd like to generate method-chaining setters (setters that return the object being set), like so:

public MyObject setField (Object value) {
    this.field = value;
    return this;
}

This makes it easier to do one-liner instantiations, which I find easier to read:

myMethod (new MyObject ().setField (someValue).setOtherField (someOtherValue));

Can Eclipse's templates be modified to do this? I've changed the content to include return this; but the signature is not changed.

like image 225
Chris R Avatar asked May 22 '09 15:05

Chris R


1 Answers

I confirm eclipse (up to 3.5RC1) does not support "method chaining" setter generation.
It only allows for comment and body customization, not API modification of a setter (meaning a generated setter still return 'void').

May be the plugin Builder Pattern can help here... (not tested though)

Classic way (not "goof" since it will always generate a "void" as return type for setter):
alt text
(source: eclipse.org)

Vs. new way (Builder Pattern, potentially used as an Eclipse plugin)
alt text http://www.javadesign.info/media/blogs/JDesign/DesignConcepts/DesignPatterns/GOF/Creational-BuilderPatternStructure.jpeg

like image 63
VonC Avatar answered Oct 13 '22 11:10

VonC