Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the `get` and `set` from IntelliJ getter/setter generation

Given I have a POJO like:

public class Person {
    private String name;
}

How can I have IntelliJ generate getters and setters without the prefix get and set?

To get this:

public String name() {
    return this.name;
}

public void name(String name) {
    this.name = name;
}

Instead of this:

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}
like image 390
Emiliano Zilocchi Avatar asked Feb 03 '16 13:02

Emiliano Zilocchi


1 Answers

Disclaimer: the type of getter/setter you want goes against naming conventions, best practices and specifications (see section 7 starting on page 40). Many libraries, such as those used for serialization or marshalling expect the getFoo and setFoo type getters and setters, and those libraries will not work as expected if you use the type of getters and setters that you want to generate.

That being said, it is possible to change the default getter/setter generation in IntelliJ.

As you may know, you can select Code|Generate from the IntelliJ menu bar, or use the Alt+Ins shortcut to bring up the Generate window. From there, you can select Getter, Setter, or Getter and Setter. When you select one of those options, a dialog will open to select a template and which fields to generate getters/setters for.

If you select the ... next to the template dropdown, it will open a separate template management dialog.

I would recommend copying the IntelliJ Default template, using the copy icon, or the Ctrl+D shortcut, and giving your new template a different name, such as 'Variable Name Only'.

Then, you can modify the template to fit your needs.

Here is the IntelliJ Default getter template:

public ##
#if($field.modifierStatic)
  static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
  #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
    #set($name = $StringUtil.decapitalize($name))
  #else
    is##
#end
#else
  get##
#end
${name}() {
  return $field.name;
}

You should be able to remove the boolean check altogether, since for your template, the method name won't depend on the type of the variable. The following template should give you the getter that you want:

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

And this should give you the desired setter:

#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)
  static ##
#end
void $StringUtil.decapitalize($StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))($field.type $paramName) {
  #if ($field.name == $paramName)
    #if (!$field.modifierStatic)
      this.##
    #else
      $classname.##
    #end
  #end
  $field.name = $paramName;
}
like image 53
Andrew Mairose Avatar answered Oct 29 '22 17:10

Andrew Mairose