Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can IntelliJ generate getters without the "get" prefix?

IntelliJ has the cool feature to generate Java getters. For example, for a field private final String foo, it will generate a getter getFoo().

Is there any way I can configure IntelliJ to generate getters in the format String foo()? I am working mainly with immutable objects and prefer this syntax.

like image 612
Hbf Avatar asked Oct 24 '14 21:10

Hbf


Video Answer


2 Answers

Here are some slightly improved templates based on @Ed .'s previous answer:

fluent-getter:

public ##
#if($field.modifierStatic)
  static ##
#end
$field.type ##
${field.name}() {
return ##
#if (!$field.modifierStatic)
this.##
#else
  $classname.##
#end
$field.name;
}

fluent-setter:

#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)
  static ##
#end
void ##
${field.name}($field.type $paramName) {
#if ($field.name == $paramName)
  #if (!$field.modifierStatic)
  this.##
  #else
    $classname.##
  #end
#end
$field.name = $paramName;
}
like image 191
Renan Ferrari Avatar answered Oct 22 '22 21:10

Renan Ferrari


Neat question! Just to clarify @Danny Dan's answer since IntelliJ 15 has been released...

To set this up:

  • Alt+Insert
  • Select Getter
  • Open the template configuration from '...' on the RHS
  • Create a new template from the LHS - see example below
  • Ok and select your new template

Example Template: fluent-getter

 public ##
 #if($field.modifierStatic)
   static ##
 #end
 $field.type ##
 ${field.name}() {
   return $field.name;
 }

Why would you want to do this?

Checkout Implementing Domain-Driven Design:

The simple but effective approach to object design keeps the Value Object faithful to the Ubiquitous Language. The use of getValuePercentage() is a technical computer statement, but valuePercentage() is a fluent human-readable language expression.

like image 28
Ed . Avatar answered Oct 22 '22 20:10

Ed .