14

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.

Hbf
  • 3,074
  • 3
  • 23
  • 32
  • 3
    Not sure if possible, but your desired syntax is against the [Java Coding Conventions](http://www.oracle.com/technetwork/java/codeconventions-150003.pdf). – Luiggi Mendoza Oct 24 '14 at 21:38
  • 4
    @LuiggiMendoza these are known as "fluent accessors" and are used more are more these days. Lombok even supports generating accessors that way out of the box. – Boris the Spider Oct 24 '14 at 21:43
  • 1
    @BoristheSpider TIL. Sadly, I'm not sure if these accessors are supported by frameworks that depend on [Java Bean Naming Convention](http://download.oracle.com/otn-pub/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/beans.101.pdf?AuthParam=1414187327_12eeae19bd08754d7401009a406362da) like Spring or JSF or technology like Expression Language. – Luiggi Mendoza Oct 24 '14 at 21:47
  • @LuiggiMendoza Yeah, I know. Many frameworks are leaning towards field access rather than method naming conventions these days - especially with the advent of annotations. Depends what the OP is going with the beans. Incidentally, this is an attempt to make Java a little more conformant with the [Uniform Access Principle](http://en.wikipedia.org/wiki/Uniform_access_principle). – Boris the Spider Oct 24 '14 at 21:52
  • @Hbf, if you're using something like [AutoValue](https://github.com/google/auto/tree/master/value) for your value classes, you can generate your getters at compile time rather than with your IDE, and you can specify any method names you want. – Louis Wasserman Oct 24 '14 at 23:12

4 Answers4

12

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.

Community
  • 1
  • 1
Ed .
  • 6,373
  • 8
  • 62
  • 82
3

If I understood right you cannot modify getters/setters in idea now. Issue on youtrack

P.S. Ok, now Fix version is 14.1, from this version of idea you can create and choose getter/setter template directly in Alt-Insert menu.

Danny Dan
  • 1,215
  • 1
  • 16
  • 32
3

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;
}
Renan Ferrari
  • 2,449
  • 5
  • 21
  • 26
0

I like to have isXxx for boolean (for example isConnected()), if you need this then the template would be:

#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))
#if ($field.boolean && $field.primitive)
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))is##
#else
  ##
#end
${name}() {
  return $field.name;
}