Can I have a public NoArgsConstructor when using @Builder?

19,757 views
Skip to first unread message

Edvin Syse

unread,
Jun 15, 2014, 1:36:23 PM6/15/14
to project...@googlegroups.com
I would like to use the @Builder annotation on my classes, but this seems to remove the default no-args constructor. Even if I add the @NoArgsConstructor annotation to my class, the constructor still isn't generated.

import lombok.experimental.Builder;

@Builder
public class User {
private Long id;
public static void main(String[] args) {
User u = new User();
}
}

Trying to compile this class gives me:

User.java:8: error: constructor User in class User cannot be applied to given types;

User u = new User();

        ^

  required: Long

  found: no arguments

  reason: actual and formal argument lists differ in length

1 error


Is this expected behavior, and is there a workaround?

Martin Grajcar

unread,
Jun 17, 2014, 3:54:06 PM6/17/14
to project...@googlegroups.com
I guess, the logic is simple: The @Builder generates an @AllArgsConstructor, unless there's another @XConstructor. Adding @NoArgsConstructor makes the @Builder generate no constructor. So add also an explicit @AllArgsConstructor and you're done.



--
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-lombo...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joachim Seminck

unread,
Apr 9, 2015, 9:22:41 AM4/9/15
to project...@googlegroups.com
Any update on this?

We ran into the same issue today, which eventually caused me to find this post. I agree that adding both annotations for @NoArgsConstructor and @AllArgsConstructor works, but it would be a tiny bit nicer if the @Builder annotation just also provides the empty constructor. As you know there are other libraries which require entities to have an empty constructor, hence the request.

Thanks and keep up the awesome work!
Joachim.

Reinier Zwitserloot

unread,
Apr 9, 2015, 10:16:40 AM4/9/15
to project-lombok
@Builder most definitely will never just plain dump a no-args constructor in there. The libraries that need these tend to be ugly (such as JPA), and there are lots of lombok users who don't use such libraries at all, it would inconvenience them highly if we just threw such things at them.

I see some utility in changing the behaviour of @Builder so that just '@Builder @NoArgsConstructor' will do what you want, that is, that just those two would do what currently requires all 3: "@Builder @NoArgsConstructor @AllArgsConstructor'.


 --Reinier Zwitserloot

--

Marius Kruger

unread,
Apr 9, 2015, 11:07:44 AM4/9/15
to project...@googlegroups.com
On 9 April 2015 at 16:15, Reinier Zwitserloot <rei...@zwitserloot.com> wrote:
...

I see some utility in changing the behaviour of @Builder so that just '@Builder @NoArgsConstructor' will do what you want, that is, that just those two would do what currently requires all 3: "@Builder @NoArgsConstructor @AllArgsConstructor'.

that doesn't make any sense to me at all, but what does make sense is to be able to configure such a composition:
lombok.custom.MyBuilder += @Builder
lombok.custom.MyBuilder += @NoArgsConstructor
lombok.custom.MyBuilder += @AllArgsConstructor

Reinier Zwitserloot

unread,
Apr 12, 2015, 6:53:21 PM4/12/15
to project-lombok
Composition is still a long way off.

 --Reinier Zwitserloot

Reply all
Reply to author
Forward
0 new messages