Search code examples
templatesintellij-idealive

InteliJ Live Template


I have a question regarding inteliJ live template , i have a member in my class :

@JsonProperty("CA_DL_Active_Carriers_NR_SM_DL_UE1")
    private Float caDlActiveCarriersNrSmDlUe1;

and i would like to create a getter and setter (custom setter) like this:

  public Float getCaDlActiveCarriersNrSmDlUe1() {
        return caDlActiveCarriersNrSmDlUe1;
    }



   public void setCaDlActiveCarriersNrSmDlUe1(Float caDlActiveCarriersNrSmDlUe1) {
        if (Objects.equals(getCaDlActiveCarriersNrSmDlUe1(), caDlActiveCarriersNrSmDlUe1)) {
            return;
        }
        this.caDlActiveCarriersNrSmDlUe1 = caDlActiveCarriersNrSmDlUe1;
        if (DocKind.ORIG == docKind) {
            ((McpttIterationDocument) prepareDirtyDocument()).setCaDlActiveCarriersNrSmDlUe1(caDlActiveCarriersNrSmDlUe1);
        }

i created a Live Templeate for the setter :

public void set$CAP_SELECTION$(java.lang.Float $SELECTION$) {
        if (java.util.Objects.equals(get$CAP_SELECTION$, $SELECTION$)) {
            return;
        }
        this.$SELECTION$ = $SELECTION$;
        if (com.att.tlv.arc.backend.api.persistence.documents.BaseDocument.DocKind.ORIG == docKind) {
            (($CLASSNAME$) prepareDirtyDocument()).set$CAP_SELECTION$($SELECTION$);
        }

[![var settings][1]][1] [1]: https://i.sstatic.net/L4YPN.png
but my $CAP_SELECTION$ & $SELECTION$ are empty when i applty the custom setter:

can anyone help me undestand what i'm doing wrong ? also can i highlight my member (double click it) and apply my seeter ? like clicking "generate" but insted of the regular option ,,, i'll use my live template...


Solution

  • To create a custom setter it is better to use the Code | Generate action. You can add custom getter or setter templates there to create the kind of method you want.

    Here's what to do:

    1. Invoke Code | Generate (+N on Mac)
    2. Select Getter and Setter in the popup that appears
    3. Click on the ... button after the Setter template dropdown
    4. Add a new template using the + button
    5. Add something like the following template text and give it a name:
    #set($paramName = $helper.getParamName($field, $project))
    #set($methodSuffix = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
    #if($field.modifierStatic)
    static ##
    #end
    void set$methodSuffix($field.type $paramName) {
    if (java.util.Objects.equals(get$methodSuffix(), $paramName)) {
    return;
    }
    #if ($field.name == $paramName)
        #if (!$field.modifierStatic)
        this.##
        #else
            $classname.##
        #end
    #end
    $field.name = $paramName;
    if (DocKind.ORIG == docKind) {
    ((McpttIterationDocument) prepareDirtyDocument()).set$methodSuffix($paramName);
    }
    }
    

    You may want to fully qualify the reference to DocKind and McpttIterationDocument.

    1. Click OK on the template dialog and click OK on the generate getters and setters dialog

    Result: a reusable custom setter template.