I like fluid APIs where you can join multiple method calls without repeating the target object. A classic example for this is the StringBuilder class:

final StringBuilder s = new StringBuilder()
    .append(": ")

another use case are builders:

final HttpOptions opt = new HttpOptions()

For this kind of builders, the setters do not return void but this.

public HttpOptions port(int port){
    this.port = port;
    return this;

With IntelliJ IDEA, normal getters and setters can be generated automatically, but not this style of fluid setters.

But there are live templates which provide some kind of macros: Type psvm and hit tab, and it will be completed into public static void main(String[] args){}.

It’s possible to create your own live templates and here’s the result of my experiments:

Type fs, hit tab, enter the type and the name of the attribute and voilà: The fluid setter is generated.

But there’s even more: Besides the predefined expressions like className() or suggestVariableName(), there is groovyScript() which executes any groovy script you like.

So I made a second template

public $CLASS_NAME$ $NAME$($TYPE$ $NAME$){
    this.$NAME$ = $NAME$;
    return this;

with these variable definitions:

CLASS_NAME    className()
NAME          groovyScript("_1.split(' ')[-1].replace(';','')", SELECTION)
TYPE          groovyScript("_1.split(' ')[-2]", SELECTION)

Put the cursor on a line with an attribute definition like private String name;, hit alt-cmd-J, select the right template, and the corresponding fluid setter will be generated.

Groovy is easy to learn when you know java and there’s a REPL under Tools -> Groovy Console where you can interactively try out scripts. There’s also the possibility to reference a groovy file instead of writing the code directly into the Template Variables dialog.

One more nice feature of IntelliJ IDEA.