Skip to content

Builder factory method is generated inside Builder instead of the outer class #68

Description

@cherepavel

According to the plugin documentation, the generated builder API should expose a static newBuilder() method on the outer class.

The documentation shows the following structure:

public class JavaBean {
    private JavaBean(Builder builder) {
        // ...
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static final class Builder {
        private Builder() {
        }

        public JavaBean build() {
            return new JavaBean(this);
        }
    }
}

So the expected usage is:

JavaBean.newBuilder()
        .foo("value")
        .build();

However, the plugin currently generates newBuilder() inside the nested Builder class instead of the outer class.

Actual generated code

public class TestDataClass2 {

    private TestDataClass2(Builder builder) {
        string = builder.string;
        integer = builder.integer;
        doubleNumber = builder.doubleNumber;
        floatNumber = builder.floatNumber;
    }

    public static final class Builder {
        private String string;
        private int integer;
        private Double doubleNumber;
        private float floatNumber;

        private Builder() {
        }

        public static Builder newBuilder() {
            return new Builder();
        }

        public TestDataClass2 build() {
            return new TestDataClass2(this);
        }
    }
}

Because of this, the generated API requires the following call:

TestDataClass2.Builder.newBuilder()
        .string("value")
        .integer(1)
        .build();

Expected behavior

The generated code should match the documentation and place newBuilder() in the outer class:

public class TestDataClass2 {

    private TestDataClass2(Builder builder) {
        string = builder.string;
        integer = builder.integer;
        doubleNumber = builder.doubleNumber;
        floatNumber = builder.floatNumber;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static final class Builder {
        private Builder() {
        }

        public TestDataClass2 build() {
            return new TestDataClass2(this);
        }
    }
}

Expected usage:

TestDataClass2.newBuilder()
        .string("value")
        .integer(1)
        .build();

Why this is a problem

The generated code does not match the documented API.

Also, since the Builder constructor is private, users cannot instantiate it directly. The only available factory method is currently located inside the nested Builder class, which makes the public API less convenient and inconsistent with the documentation.

Possible fix

Move the generated newBuilder() method from the nested Builder class to the outer generated class.

Alternatively, if the current behavior is intentional, the documentation should be updated to show the actual generated API:

TestDataClass2.Builder.newBuilder()

instead of:

TestDataClass2.newBuilder()

Environment

  • Plugin version: 1.3.0
  • IDE version: IntelliJ IDEA 2026.1.1
  • Java version: 17
  • Generated class: TestDataClass2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions