Java Lombok: Omitting One Field in @Allargsconstructor

Java Lombok: Omitting one field in @AllArgsConstructor?

No that is not possible. There is a feature request to create a @SomeArgsConstructor where you can specify a list of involved fields.

Full disclosure: I am one of the core Project Lombok developers.

Lombok: Generate 2 constructors with specified fields

Due to Lombok documentation, you can't do this. in another language you can not have different constructors with combinations of some fields. a better solution is to use @Builder

How can I specify a one-argument constructor using Lombok?

I didn't find in documentation

Use this: http://projectlombok.org/features/Constructor.html

You have to initialize all variables which should not be part of the constructor.

@RequiredArgsConstructor generates a constructor with 1 parameter for each field that requires special handling. All non-initialized final fields get a parameter, as well as any fields that are marked as @NonNull that aren't initialized where they are declared. For those fields marked with @NonNull, an explicit null check is also generated.

So the following should create an one argument (param) constructor:

@RequiredArgsConstructor class MyClass {
private String param;
private Integer count = -1;
}

Combining lombok @AllArgsConstructor with Spring-boot MongoDb @PersistenceConstructor

According to https://projectlombok.org/features/constructor, to put annotations on the generated constructor, you can use onConstructor=@__({@AnnotationsHere})

So it should be

@AllArgsConstructor(onConstructor=@__({@PersistenceConstructor}))
public class ExampleDoc {
...
}

Different constructors (LOMBOK)

Yeah, you should just add them yourself. Years ago there was already a discussion to add the @SomeArgsConstructor annotation, but since the Lombok team never did add that annotation, I think it is unlikely they will ever do it.

Or, as stated in the comments, use the builder pattern with the @Builder annotation. Then you could write something like: StatusUpdate.builder().text("text").date(new Date()).build();.


Btw, if you do annotate all your fields with @Getter, @Setter and use the @EqualsAndHashCode and @RequiredArgsConstructor on class level, I think the @Data annotation could be a good fit for this class.

How to exclude property from Lombok builder?

Yes, you can place @Builder on a constructor or static (factory) method, containing just the fields you want.

Disclosure: I am a Lombok developer.



Related Topics



Leave a reply



Submit