Mandroid
Mandroid

Reputation: 7494

To have lombok @Value, @NoArgsConstructor, @AllArgsConstructor together

I am using lombok annotations on a class:

@Value
@NoArgsConstructor
@AllArgsConstructor
public class TestClass implements Serializable {

    private String x;
    private String y;
    private String z;

}

I need @Value as I want fields to be immutable, and constructor annotations as ORM framework I am using needs both a no arg constructor as well as constructor for initializing fields.

But IntelliJ marks this as error stating fields need to be initialized, while we have a noarg constructor defined by lombok.

How to work around this issue?

Upvotes: 3

Views: 4810

Answers (2)

Dai
Dai

Reputation: 407

With @Value, the lombok mark all fields as final, without default values, the @NoArgsConstructor will result in fields never initialized, that's why the IntelliJ complains.

You can config the lombok with

lombok.noArgsConstructor.extraPrivate=true

As a result, the @NoArgsConstructor will generate contractor with default values. The default value can be set with @FieldDefaults.

See documents: @Value @FieldDefaults

Upvotes: 3

Alexey Romanov
Alexey Romanov

Reputation: 170745

Why not just define the no-args constructor with the default values you want?

@Value
@AllArgsConstructor
public class TestClass implements Serializable {

    private String x;
    private String y;
    private String z;

    TestClass() {
        x = "";
        y = "";
        z = "";
    }
}

Upvotes: 0

Related Questions