intellij-ideacode-generationcode-templates

IntelliJ: custom code generation templates


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


Solution

  • When you press alt+insert you can click Getter and Setter. There are Getter template and Setter template drop-downs that you can select. Click the ... and you can create new templates. enter image description here

    It appears you're trying to follow the builder pattern. IntelliJ already has a setter template for this called "Builder". You can select it from the setter drop-down and you should be good.