IntelliJ's Live Template

less than 1 minute read

If you remember, I like to write my getters this way (long story here)

public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
private String name;

I have been doing it manually for a while but it turns out IntelliJ has a super nice feature for that: Live Templating which allows you to use parameterized and contextual templates to generate code.

Go to your Settings->Live Templating and create a new one.

Add this template:

public $TYPE$ get$UpperName$() { return $lowername$; }
public void set$UpperName$($TYPE$ $lowername$) { this.$lowername$ = $lowername$; }
private $TYPE$ $lowername$;

Then edit the variables:

  • TYPE: the expression should be classNameComplete() to refer to the class name context
  • lowername: the expression should be decapitalize(UpperName). Also tick skip if defined to not have to validate the computed value (this saves you one key stroke).

I've named my Live Template "get". When I type "get" and TAB, I am asked to type the property type (which is suggested). The second variable asked is the capitalized property name. Then the Live Template infers the rest (decapitalize my property name to fill up lowername).

Pretty nice feature!

Comments