Lombok Can @Builder and @Value be used together in the same class

First of all, thanks to Lombok, our Java code is now much more elegant and cleaner. My use case I want to create an immutable class. For this, I would use the @Value annotation. I also want to use the capabilities of builder, for this I would use the @Builder annotation. My question is whether both @Builder and @Value can be used together in the same class. This is a good practice recommended by Lombok users / developers.

+9
source share
3 answers

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

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

After delombification, the following occurs:

 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 + ")"; } } } 

This way you can clearly use @Value and @Builder

+8
source

Starting with version 1.16.10, the constructor is not public if you use both.

You can add @AllArgsConstructor to compensate for this.

+6
source

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

Long answer: you can use them together. The only thing you should remember is that the AllArgsConstructor provided by @Value will be closed for the package starting with lombok v1.16.0. So, if you want this to be publicly available, you will have to access the management of this additionally with @AllArgsConstructor. In my case, though, a private constructor is what I really wanted. My goal of using @Builder was to really allow the creation of an object using only the linker and not use the 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; } 

@ Type value from eclipse

@Builder Tyoe from eclipse

0
source

All Articles