rafakob
rafakob

Reputation: 4346

Generate builder methods instead of setters

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

Answers (1)

Related Questions