IntelliJ getter/setter format (single-line versus multi-line)

I’m using IntelliJ IDEA 14.1.0 and you can customise this behaviour.

Just use the “Generate…” option, or use Alt+Insert shortcut, and select “Getter and Setter”.

In the “Select Fields” window that gets opened, you have the “Getter Template” option at the top. Use the “…” button next to the dropdown, to edit the template.

Select “IntelliJ Default” and click the “Copy” button to create a new one named “AlwayStartWithGet”, which you can edit.

Just remove the following section:

#if ($field.boolean)
  #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
    #set($name = $StringUtil.decapitalize($name))
  #else
    is##
#end
#else
  get##
#end

And replace it with a simple

get##

You should be left with:

public ##
#if($field.modifierStatic)
  static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}() {
  return $field.name;
}

Now you can use the custom template when generating code, by selecting it in the getter template dropdown.

Leave a Comment

tech