Eric
Eric

Reputation: 5402

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.

Upvotes: 10

Views: 11951

Answers (2)

k24
k24

Reputation: 81

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

Upvotes: 8

maba
maba

Reputation: 48045

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!

Upvotes: 13

Related Questions