Lombok Can we use @Builder and @Value together on a single class

Short Answer: Yes, you can always use @Value and @Builder together. I have used this in my production code and it works fine.

Long Answer: You can use them together. The one thing you want to keep in mind is that the AllArgsConstructor provided by @Value will be private to package since lombok v1.16.0. So, if you want that to be public you will have to access control that additionally by @AllArgsConstructor. In my case though, private constructor is what I really wanted. My aim of using @Builder was to really allow the the object instantiation only using the builder and not use constructor or setter methods later.

Here is my code:


@Value
@Builder
@ApiModel(description = "Model to represent annotation")
public class Annotation {

    @NonNull
    @ApiModelProperty(value = "Unique identifier")
    private String id;

}

@Value type from eclipse

@Builder tyoe from eclipse


Of course you can. To check, simply delombok your code and see what it generates. Take this example:

@Builder
@Value
public class Pair {
    private Object left;
    private Object right;
}

After delombokification, this produces:

public class Pair {
    private Object left;
    private Object right;

    @java.beans.ConstructorProperties({ "left", "right" })
    Pair(Object left, Object right) {
        this.left = left;
        this.right = right;
    }

    public static PairBuilder builder() {
        return new PairBuilder();
    }

    public Object getLeft() {
        return this.left;
    }

    public Object getRight() {
        return this.right;
    }

    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof Pair)) return false;
        final Pair other = (Pair) o;
        final Object this$left = this.left;
        final Object other$left = other.left;
        if (this$left == null ? other$left != null : !this$left.equals(other$left)) return false;
        final Object this$right = this.right;
        final Object other$right = other.right;
        if (this$right == null ? other$right != null : !this$right.equals(other$right)) return false;
        return true;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $left = this.left;
        result = result * PRIME + ($left == null ? 0 : $left.hashCode());
        final Object $right = this.right;
        result = result * PRIME + ($right == null ? 0 : $right.hashCode());
        return result;
    }

    public String toString() {
        return "Pair(left=" + this.left + ", right=" + this.right + ")";
    }

    public static class PairBuilder {
        private Object left;
        private Object right;

        PairBuilder() {
        }

        public Pair.PairBuilder left(Object left) {
            this.left = left;
            return this;
        }

        public Pair.PairBuilder right(Object right) {
            this.right = right;
            return this;
        }

        public Pair build() {
            return new Pair(left, right);
        }

        public String toString() {
            return "Pair.PairBuilder(left=" + this.left + ", right=" + this.right + ")";
        }
    }
}

So you can clearly use both @Value and @Builder


As of version 1.16.10, the constructor is not public when you use both.

You can add @AllArgsConstructor to compensate for this.

Tags:

Java

Lombok