AlikElzin-kilaka
AlikElzin-kilaka

Reputation: 36001

How to generate getters for lombok Builder?

I have the following definition:

@Value
@Builder(toBuilder = true)
public class MyEntity {
  String name;
}

When trying to use a getter on a builder, e.g. MyEntityBuilder.getName(), IDEA states that it "Cannot resolve method". Also, IDEA doesn't auto-complete it.

The only available method (except build()) is the setter: MyEntityBuilder name(String name);

Is there a way to generate getter on lombok generated builders? Thanks.

Using Lombok 1.8.16.

Upvotes: 8

Views: 7225

Answers (5)

AlikElzin-kilaka
AlikElzin-kilaka

Reputation: 36001

There is a way to have a getter for a builder (but not generated).

We can just create an inner Builder class and add a getter method, and let Lombok generate the rest.

Example:

@AllArgsConstructor
@Builder
@Value
public class Yes {
    String fieldA;

    String fieldB;

    public static class YesBuilder {
        public String getFieldA() {
            return fieldA;
        }

        // Remaining builder methods generated by Lombok
    }
}

Execution example:

public static void main(String[] args) {
    Yes.YesBuilder builder = Yes.builder();
    builder.fieldA("Yes A");
    builder.fieldB("Yes B");
    // System.out.println(builder.fieldA); - compile error
    System.out.println(builder.getFieldA());
    // System.out.println(builder.getFieldB()); - compile error
    System.out.println(builder.build());
}

Res:

Yes A
Yes(fieldA=Yes A, fieldB=Yes B)

Upvotes: 2

pirho
pirho

Reputation: 12215

As answer from DwB suggests builder pattern does not need nor use getters.

You can have a getter for Lombok builder values but I think that it would not be very useful. You can customize builder this way:

@Value
@Builder(toBuilder = true)
public class MyEntity {
    String name;
    String name2; // just something to set also
    // Customized builder
    public static class MyEntityBuilder {
        private String name;
        public String getName() {
            return this.name;
        }
        // This is not actually needed but just as an example howto
        // customise a setter.
        public MyEntityBuilder name(String name) {
            this.name = name;
            return this;
        }
    }
}

And to test it (Junit5):

@Test
void test() {
    MyEntityBuilder meb = MyEntity.builder(); 
    var myEntity = meb
            // You need to set this first to access it later
            .name("Name #1")
            // The benefit having a getter ?
            .name2(meb.getName())
            .build();
    assertEquals(myEntity.getName(), myEntity.getName2());
}

Upvotes: 1

DwB
DwB

Reputation: 38300

Builders do not, generally, have getters. The builder pattern is this:

  1. Create the builder.
  2. Set values in the builder.
  3. Call build().

Upvotes: 1

Ben M. Ward
Ben M. Ward

Reputation: 85

You mentioned you were using IntelliJ IDEA as your IDE. It so happens that there is an IDEA plugin exactly for this: https://plugins.jetbrains.com/plugin/6317-lombok

Once the plugin is installed, IDEA should recognize Lombok's annotation syntax.

Upvotes: -3

Arthur Klezovich
Arthur Klezovich

Reputation: 2819

There seems to be no option to do it out of the box according to the Lombok docs or the source code.

If you want to inspect the contents of the builder, the best you can do, it seems, is to build the object and inspect the field from there.

Upvotes: 8

Related Questions