@Builder and @NoArgsConstructor together

7,226 views
Skip to first unread message

SoMa

unread,
Mar 7, 2017, 11:17:15 AM3/7/17
to Project Lombok
Hi,

My pojo needs to have a builder class and default constructor as well so I added @Builder and @NoArgConstructor to my class but I got an exception:

java: constructor Configuration in class Abc cannot be applied to given types;
required: no arguments
found: java.lang.Long, java.lang.String, java.lang.String, java.lang.Long, java.lang.String, java.lang.Instant
reason: actual and formal argument lists differ in length


This is my class:

@NoArgsConstructor
@EqualsAndHashCode
@ToString
@Builder
public class MyPojo implements Cloneable {
    @Getter @Setter private Long id;
    @Getter @Setter private String key;
    @Getter @Setter private String value;
    @Getter @Setter private Long alternativeId;
    @Getter @Setter private String description;
    @Getter @Setter private Instant timestamp;

    public MyPojo clone() {
        ....
    }
}

What is wrong with this?


Martin Grajcar

unread,
Mar 7, 2017, 11:42:47 AM3/7/17
to project...@googlegroups.com
Your @Builder needs an @AllArgsConstructor (add it; feel free to make it private).

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

SoMa

unread,
Mar 8, 2017, 1:21:37 PM3/8/17
to Project Lombok
Hi Martin,

Thank you for your help.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.

Rick Herrick

unread,
Oct 3, 2017, 1:03:05 PM10/3/17
to Project Lombok
I just spent a while working around this problem. How is this not a bug, at the very least in terms of the message that's displayed? In fact, I ended up joining this discussion group because I was trying to figure out how to report a bug in Lombok.

Looking at it again, the message completely contradicts the solution:

required: no arguments
found: java.lang.Long, java.lang.String, java.lang.String, java.lang.Long, java.lang.String, java.lang.Instant

It found the all-args constructor, but required the no-args constructor, which exists due to the @NoArgsConstructor annotation. I had the same situation: I also tried removing @NoArgsConstructor and explicitly defining a no-args constructor for the class. Same result. It was only by accident that I added @AllArgsConstructor, given that the message was saying that it had an all-args constructor.

So is this a known (and tracked) bug?
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.

Martin Grajcar

unread,
Oct 3, 2017, 1:58:28 PM10/3/17
to project...@googlegroups.com
On Fri, Sep 29, 2017 at 6:45 AM, Rick Herrick <jrhe...@wustl.edu> wrote:
I just spent a while working around this problem. How is this not a bug, at the very least in terms of the message that's displayed? In fact, I ended up joining this discussion group because I was trying to figure out how to report a bug in Lombok.

There's https://github.com/rzwitserloot/lombok/issues. I probably agree that @Builder should generate a constructor it needs, even in spite of the presence of @NoArgsConstructor. OTOH things are pretty complicated, so I wouldn't bother reporting something having such a simple workaround.

Note: I'm not a project maintainer, just a fan. 

Looking at it again, the message completely contradicts the solution:

required: no arguments
found: java.lang.Long, java.lang.String, java.lang.String, java.lang.Long, java.lang.String, java.lang.Instant

It found the all-args constructor, but required the no-args constructor, which exists due to the @NoArgsConstructor annotation.

This message might be misleading, but it's not produced by Lombok. There's a constructor, which requires no arguments, but gets a bunch of them. The message is perfectly right from the POV of the call site (which you don't see as it's generated).
 
I had the same situation: I also tried removing @NoArgsConstructor and explicitly defining a no-args constructor for the class. Same result. It was only by accident that I added @AllArgsConstructor, given that the message was saying that it had an all-args constructor.

So is this a known (and tracked) bug?

Maybe. I can't recall it, but the issue list is pretty long.
Reply all
Reply to author
Forward
0 new messages