I need to custom eclipse setter template to return this object, like this
public Person setName(String name){
this.name=name;
return this;
}
but Java->Code Style->Templates only allow me to custom setter body part, not method definition. Is there any way to do it?
I’m afraid I can’t offer a solution to allow you to modify the Eclipse template for this, however can I suggest that you rethink what you’re doing here?
If you check the JavaBeans spec you’ll see that when you define your methods in this way they are no longer valid property setters. Setters should have a
voidreturn type; you may regret creating these non-standard beans in the long run. For instance, try usingjava.beans.Introspectorto gather bean info for your class and you’ll see that your property ‘setters’ are not found.I know it’s nice to be able to quickly initialise your beans with chained calls like:
Can I suggest as an alternative you use standard setters (that return
void) and instead introduce builder methods like:Your quick single line construction then looks like:
I find the ‘with’ prefix reads nicely too.