Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ: Generating getters/setters for fields with underscores in the name

Is there a way to get IntelliJ to be smarter about the getters/setters it generates? In particular, if a field name has underscores in it, can the getter strip them out and convert to CamelCase?

For instance, I would expect the below getter to be getCarneAsada():

public class Taco {
  private String carne_asada;

  public String getCarne_asada() {
    return carne_asada;
  }
}

I found the Code Style->Java->Code Generation area, but none of the options seem appropriate...it's not a prefix or a suffix I want to exclude. It's inner underscores.

like image 468
Eric Avatar asked May 29 '13 03:05

Eric


People also ask

Can getter and setter have same name?

“Another thing to keep in mind when using getter (and setter) methods is that properties cannot share the same name as the getter/setter function.”

How do you name a setter and getter?

The getter should start with 'get', followed by the member name, with its first letter capitalized. Also the latest conventions I heard of, say that we should avoid multiple capital letters one after another. For example getHTMLtooltip is wrong. it should be getHtmlTooltip instead.

Do encapsulation allows setters?

Advantage of Encapsulation in JavaBy providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.


2 Answers

You can use Live Templates to accomplish this. It will not be accessible from Code Generation but it will still be quite easy to use.

I have created a Main class with the property carne_asada:

enter image description here

Now open up Live Templates in the settings and create a new template called getu under other:

enter image description here

Press the little link called Define at the bottom to define when this template is valid. Then choose Java | Declaration:

enter image description here

Press the button Edit Variables to open a window where each of the live template variables can be defined. Now create the following variables:

VAR         : suggestFirstVariableName("Object")
TYPE        : typeOfVariable(VAR)
CAP_CAM_VAR : capitalize(underscoresToCamelCase(VAR))

The order is quite important so the VAR must come first. In my example CAP_CAM_VAR stands for Capitalize and CamelCase the VAR variable. Set the Skip if defined according to the image:

enter image description here

Now press OK and OK again to get back to the editor.

Try the new getu live template by typing getu and then press Tab:

enter image description here

enter image description here

Press Enter and the getter generation has finished:

enter image description here

Now if you had had some more variables with underscores you will get a list to choose from so here is an example:

enter image description here

And the result is just beautiful:

enter image description here

You can easily create the same setu live template and maybe some sgetu that creates them both at the same time.

Hope this helps a little bit!

like image 177
maba Avatar answered Oct 26 '22 05:10

maba


Here are templates for generating getter/setter working under IntelliJ IDEA 2016 and Android Studio 2.2.

Camelized Getter:

#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
#set($words = $StringUtil.split($name, "_"))
#set($name = "")
#foreach($word in $words)
#set($name = $name + $StringUtil.capitalize($word))
#end
${name}() {
  return $field.name;
}

Camelized Setter:

#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#set($words = $StringUtil.split($name, "_"))
#set($name = "")
#foreach($word in $words)
#set($name = $name + $StringUtil.capitalize($word))
#end
void set$name($field.type $StringUtil.decapitalize($name)) {
  #if ($field.name == $paramName)
    #if (!$field.modifierStatic)
      this.##
    #else
      $classname.##
    #end
  #end
  $field.name = $paramName;
}

If you want fluent getter/setter with above, please refer my gists.

https://gist.github.com/k24/72d0be3d76d4eb987c6d4eb1d8f42db2

like image 33
k24 Avatar answered Oct 26 '22 04:10

k24