Question

How can you get IntelliJ to generate getter/setters on one line like this:

public String getAbc() { return abc; }

Instead of

public String getAbc() {
   return abc;
}

??

Was it helpful?

Solution

There are no templates neither for getters nor for equals/hashcode. These are hardcoded in IDEA.

Source

You can see that in this IntelliJ Wishlist

OTHER TIPS

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.

You didn't mention what version of IDEA you are using, so I assume the recent 8 or 9.

Check your Code Style settings, under "Alignment and Braces". You should find a "Simple methods in one line" option there.

For Idea 2016.

Getter template

Merge the last 3 lines into a single line:

${name}() { return $field.name; }

Setter template

Add double hash (without a space) at the end of the longest line:

[...] ($field.type, $paramName) {##

Merge the last 2 lines into a single line:

$field.name = $paramName; }

Note: as commented by @mindas, you will probably want instead the visual auto folding that doesn't get versioned.

I don't know why you want to do this, presumably for saving visual space. If so, just use IntelliJ's feature to fold trivial getters/setters and forget about how lines does it take. Folding feature can be found in

Settings -> IDE Settings -> Editor -> Code Folding -> Show code folding outline -> Simple property accessors

Alex G and laffuste provided answers that helped me, but even after you follow these instructions, IntelliJ may still automatically format your code. To stop this from happening, go to Settings -> Editor -> Code Style -> Java (or other language). Click on the tab titled "Wrapping and Braces". Under the title "Keep when reformatting" click the checkbox next to "Simple methods in one line".

For further brevity, you can eliminate the blank lines between the getter and setter methods. To do this, click on the tab titled "Blank Lines". In the section titled "Minimum Blank Lines", find the text box next to "Around method". Enter 0 in the text box.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top