Posts

Showing posts from April, 2017

Getter Template with Optional<> for IntelliJ IDEA

Image
If you are working with Java 8 and Intellij IDEA, good practice to use Optional for getters, of course depending of your logic :) I'll show how to quick configure IDE for generation such getters. Create new template and place this code: #if($field.modifierStatic) static ## #end Optional<$field.type> ## #set($name = $StringUtil.capitalizeWithJavaBeanConvention( $StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))) #if ($field.boolean && $field.primitive) is## #else get## #end ${name}() { return Optional.ofNullable($field.name); }