Skip to content

Lightspots/intellij-builder-generator

Repository files navigation

Intellij Builder Generator

Build Version Downloads

Plugin which adds a Builder action to the generation menu. The action generates a builder as inner class.

import java.util.Objects;

public class TestDto {
  @Nullable
  private final String field1;
  @NonNull
  private final String field2;

  private TestDto(Builder builder) {
    this.field1 = builder.field1;
    this.field2 = Objects.requireNonNull(builder.field2);
  }

  @NonNull
  public static Builder builder() {
    return new Builder();
  }

  public static final class Builder {
    private String field1;
    private String field2;

    private Builder() {
      // use static builder method
    }

    @NonNull
    public Builder field1(@Nullable String field1) {
      this.field1 = field1;
      return this;
    }

    @NonNull
    public Builder field2(@NonNull String field2) {
      this.field2 = field2;
      return this;
    }

    @NonNull
    public TestDto build() {
      return new TestDto(this);
    }
  }
}

Features

  • Optional static method for getting the builder
    • Custom name for that static method
  • Custom prefix for the builder methods
  • Infer nullable / nonNull annotations from field / getters
    • Currently, it is necessary to manually configure the fully qualified class name of the used annotations
  • Add requireNonNull for fields (or getters) marked with @NonNull annotation

Usage

You can invoke it via Alt+Insert (generation dialog) and choose the builder action or directly with Shift+Alt+B. Then you need to choose the members for which you want to generate builder methods and configure the features.

Installation

  • Using IDE built-in plugin system:

    Preferences > Plugins > Marketplace > Search for " intellij-builder-generator" > Install Plugin

  • Manually:

    Download the latest release and install it manually using Preferences > Plugins > ⚙️ > Install plugin from disk...


Plugin based on the IntelliJ Platform Plugin Template.