Reputation: 4346
I know there are a lot of plugins for generating Builder pattern classes. But what if I have class:
public class User {
private String name;
}
and wanted to add method:
public class User {
private String name;
public User withName(String name){
this.name = name;
return this;
}
}
Is it possible to generate such withXYZ method?
SOLVED
Go to Template Dialog for setters and add your own template:
#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)
static void ##
#else
$classname ##
#end
with$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) {
#if ($field.name == $paramName)
#if (!$field.modifierStatic)
this.##
#else
$classname.##
#end
#end
$field.name = $paramName;
#if(!$field.modifierStatic)
return this;
#end
}
Upvotes: 0
Views: 789
Reputation: 3491
Sure, you just need to add a proper getter/setter template. See:
https://www.jetbrains.com/help/idea/2016.2/generating-getters-and-setters.html https://www.jetbrains.com/help/idea/2016.2/getter-and-setter-templates-dialog.html
Upvotes: 1