Designed by Freepik

How often do you create POJO’s? You start with some fields: private String mFirstName, mAdress, mLastName… All starting with m, because you follow Android code conventions, right? You created all those fields and as a pro Intellij Idea platform user you press Alt+Insert -> Getters And Setters, selects all fields and BOOM! What a mess! Half of methods is a mess, you need to change all those names. In this post we’ll fix it. Let’s get started!

Code Template

So what happens is for getters you get this stupid name like getmName. For setters you get setmName with paremeter of mName. That’s awful. What I found myself is manually fixing all this mess so it looks like this in the end

public String getName() { return mName; } public void setName(String name) { mName = name; } 1 2 3 4 5 6 7 public String getName ( ) { return mName ; } public void setName ( String name ) { mName = name ; }

It’s not that time consuming to manually change, but rather annoying, especially for big number of fields. Makes you feel like a robot. Can we make it generate this way? Yep.

Freemarker?

When you press Generate Getters/Setters there’s a customization button

Over there you can see source code for this template. By the way there’s a default builder template for setters. It looks like Freemarker, but with different syntax, there’s no angle brackets. And it seems to not support else if statements and functions.

Getters

For getters we just need to change the method name by removing m letter. Create a new template with this code

#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 #if($StringUtil.startsWithIgnoreCase($name, 'm')) #set($name = $name.substring(1, $name.length())) #end ${name}() { return $field.name; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # 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 # if ( $ StringUtil . startsWithIgnoreCase ( $ name , 'm' ) ) # set ( $ name = $ name . substring ( 1 , $ name . length ( ) ) ) # end $ { name } ( ) { return $ field . name ; }

What starts with # is an expression, set method creates new variable. Each variable you need to start with $ to get value, horrible syntax. I just added three lines 15-17. If variable name starts with m, then we remove first letter.

Public Variable

We’re done, right? But what is we have a public variable without m prefix and starts with m? Like menu. This code will generate getenu method. But the thing is you won’t need a getter/setter for public fields, so we’re good with this code. Anyway, you can just change template to default.

Setter

Just create one more setter template with this code

#set($paramName = $helper.getParamName($field, $project)) #if ($StringUtil.startsWithIgnoreCase($paramName, 'm')) #set($paramName = $paramName.substring(1, 2).toLowerCase() + $paramName.substring(2, $$paramName.length())) #end #if($field.modifierStatic) static ## #end #set($v = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))) #if ($StringUtil.startsWithIgnoreCase($v, 'm')) #set($v = $v.substring(1, $v.length())) #end void set$v($field.type $paramName) { #if ($field.name == $paramName) #if (!$field.modifierStatic) this.## #else $classname.## #end #end $field.name = $paramName; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # set ( $ paramName = $ helper . getParamName ( $ field , $ project ) ) # if ( $ StringUtil . startsWithIgnoreCase ( $ paramName , 'm' ) ) # set ( $ paramName = $ paramName . substring ( 1 , 2 ) . toLowerCase ( ) + $ paramName . substring ( 2 , $ $ paramName . length ( ) ) ) # end # if ( $ field . modifierStatic ) static # # # end # set ( $ v = $ StringUtil . capitalizeWithJavaBeanConvention ( $ StringUtil . sanitizeJavaIdentifier ( $ helper . getPropertyName ( $ field , $ project ) ) ) ) # if ( $ StringUtil . startsWithIgnoreCase ( $ v , 'm' ) ) # set ( $ v = $ v . substring ( 1 , $ v . length ( ) ) ) # end void set $ v ( $ field . type $ paramName ) { # if ( $ field . name == $ paramName ) # if ( ! $ field . modifierStatic ) this . # # # else $ classname . # # # end # end $ field . name = $ paramName ; }

What I added here is lines 2-4 and 9-11. And use a variable in line 12.

Easier Way

Actually there’s even easier way to do that. Go to File -> Settings -> Code Style -> Java. In Code Generation tab add m as name prefix for field.

DRY

That’s a short post, but I feel like it’s super useful for Android Studio guys. If you want to automate the rest of Android Studio stuff and feel like doing same boiler plate code all the time, then check this blog post.

Ok, thanks for reading! You can get gist here. Don’t forget to subscribe, follow me on G+, Twitter, Facebook and share with friends if you think they will benefit from it!