Reputation: 3114
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.
Upvotes: 14
Views: 3638
Reputation: 3636
With Java Records in-place, Intellij has come up with in-built template for this purpose Records style
Upvotes: 1
Reputation: 145
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;
}
Upvotes: 0
Reputation: 2519
Here are some slightly improved templates based on @Ed .'s previous answer:
public ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
${field.name}() {
return ##
#if (!$field.modifierStatic)
this.##
#else
$classname.##
#end
$field.name;
}
#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;
}
Upvotes: 3
Reputation: 6403
Neat question! Just to clarify @Danny Dan's answer since IntelliJ 15 has been released...
fluent-getter
public ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
${field.name}() {
return $field.name;
}
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, butvaluePercentage()
is a fluent human-readable language expression.
Upvotes: 13
Reputation: 1245
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.
Upvotes: 3