Search code examples
javaintellij-ideacode-templates

IntelliJ Live Template: modified setters template


Is anyone know how to set up a live template in intellij for doing specialized setters - i'm using v5, but I accept with pleasure knowledge for a more recent release) -

My first need is a firePropertyChange setter:

public final static String $PROPERTY$ = "$property$"
public void set$Property$($TYPE$ $property$) {
    Object oldValue = this.$property$;
    this.$property$ = $property$;
    firePropertyChange($PROPERTY$, oldValue, $property$);
}

I have a semi-working version that generate the implementation with variables defined like this: $property$ --> completeSmart() $PROPERTY$ --> completeSmart()

My second need is a builder style setter that call the regular setter and then return this after the set:

public $THIS_TYPE$ with$Property$($TYPE$ $property$) {
    set$Property$($property$); 
    return this;
}

For this one I have nothing really good: I still have to type a lot !

Any suggestion ?


Solution

  • Something like this

    private $TYPE$ $NAME$;
    public $THIS$ set$BNAME$($TYPE$ $NAME$) {
        this.$NAME$ = $NAME$;
        return this;
    }
    

    where

    Type = complete()
    NAME = suggestVariableName()
    BNAME = capitalize(NAME)
    THIS = className()
    

    The only Problem ist that className will not work in nested classes as it will return "Outer$Inner" but it should work good enough.