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.
“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.”
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.
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.
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
:
Now open up Live Templates
in the settings and create a new template called getu
under other
:
Press the little link called Define
at the bottom to define when this template is valid. Then choose Java | Declaration
:
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:
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:
Press Enter and the getter generation has finished:
Now if you had had some more variables with underscores you will get a list to choose from so here is an example:
And the result is just beautiful:
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With