0

How can I define custom code generation like Getters/Setters in IntelliJ. I took a look on their docs but they don't specify where I can do this. The code I would like IntelliJ to generate for me is like below:

public class Person {
   private String name;
   private String username;

   //I want IntelliJ to propose me to generate this after Alt+Insert
   public Person withName(String name){
      setName(name);
      return this;
   }
   //and this 
   public Person withUsername(String username){
      setUsername(username);
      return this;
   }
}

Thanks a lot

4

1 回答 1

1

当您按下 alt+insert 时,您可以单击 Getter 和 Setter。您可以选择 Getter 模板和 Setter 模板下拉菜单。单击...,您可以创建新模板。 在此处输入图像描述

看来您正在尝试遵循builder pattern。IntelliJ 已经为此提供了一个名为“Builder”的设置器模板。您可以从 setter 下拉列表中选择它,您应该很好。

于 2018-07-23T16:32:09.437 回答