More fine grained control over constructors #152

Closed
lombokissues opened this Issue Jul 14, 2015 · 6 comments

Projects

None yet

2 participants

@lombokissues
Collaborator

Migrated from Google Code (issue 79)

@lombokissues
Collaborator

๐Ÿ‘ค scharlip ย  ๐Ÿ•— Dec 07, 2009 at 16:54 UTC

As far as I can tell, lombok will only generate a single constructor for a
@ Data annotated class, based on which fields are final or annotated with
@ NonNull.

I would love a way to be able to generate this constructor as well as a
no-argument constructor (in particular, for things like hibernate). Maybe
@ Data could have an additional parameter (something like
@ Data(additionalNoArgumentConstructor=true)).

Right now, I have a lot of boilerplate code that looks like this:

@ Data
public class Foo
{
private String bar;

public Foo(String bar) {
    this.bar = bar;
}

public Foo() {}

}

I have to write both, because lombok only wants to generate one constructor
from the @ Data annotation.

I'd love to be able to have lombok generate the constructor code for me:

@ Data(extraNoArgumentConstructor=true)
public class Foo
{
@ ConstructorParam
private String bar;
}

Lombok is great. Keep up the good work!

@lombokissues
Collaborator

๐Ÿ‘ค reinierz ย  ๐Ÿ•— Jul 19, 2010 at 15:00 UTC

We'll like tackle this for the 0.9.3 release. Link to the discussion on the googlegroups:

http://groups.google.com/group/project-lombok/browse_thread/thread/553c771b5006e18f๏นŸ

@lombokissues lombokissues added this to the 0.9.3 milestone Jul 14, 2015
@lombokissues
Collaborator

๐Ÿ‘ค reinierz ย  ๐Ÿ•— Jul 19, 2010 at 20:01 UTC

Issue #181 has been merged into this issue.

@lombokissues
Collaborator

๐Ÿ‘ค reinierz ย  ๐Ÿ•— Jul 19, 2010 at 20:14 UTC

We've now added this to lombok 0.9.3 (Burrowing Whale). We still have to update the site documentation, but, in short:

@ NoArgsConstructor
@ RequiredArgsConstructor(access=AccessLevel.PROTECTED, staticName="of")
@ AllArgsConstructor

will generate no args constructor (and error out if that's not possible due to final fields), required args constructor (all final fields and all fields with constraints such as @ NonNull - this is what @ Data does by default), and a constructor for ALL fields. You can choose the access level (public by default) for any of them, and you can turn any of them into a static constructor via the staticName annotation argument.

You can play with this feature by installing the edge release of Burrowing Whale, available here:

https://projectlombok.org/download-edge.html

Leaving this issue open until we update the docs.

Done in commit 067ecf7

@lombokissues
Collaborator

๐Ÿ‘ค reinierz ย  ๐Ÿ•— Jul 20, 2010 at 05:44 UTC

Docs have been updated in aaf8547

@lombokissues lombokissues removed the accepted label Jul 14, 2015
@lombokissues
Collaborator

End of migration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment