2
头图

Question 1: @Data and @Builder cause loss of parameterless construction

Phenomenon

  • Using the @Data annotation alone will generate a parameterless construction method.
  • Using the @Builder annotation alone, it was found that a full-attribute construction method was generated.
  • @Data and @Builder used together: we found that there is no default construction method. If you manually add a no-argument constructor or annotate with @NoArgsConstructor, an error will be reported!

Two solutions

1. The construction method is annotated with @Tolerate, so that lombok pretends that it does not exist (not aware)

@Builder
@Data
public class TestLombok {

    @Tolerate
    TestLombok() {
    }
    ......
} 

2. Add these 4 notes directly

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestLombok {
    ......
}    

Question 2: @Builder annotation causes the default value to be invalid

Phenomenon

Using Lombok annotations can greatly simplify the amount of code. In addition to the easy-to-use annotations that remove @Data, there is also the @Builder annotation, which allows you to easily use the builder mode to build objects, but today I found that the @Builder annotation will The default value of the object is cleared.

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestLombok {

    private String aa = "zzzz";
    
    public static void main(String[] args) {
        TestLombok build = TestLombok.builder().build();
        System.out.println(build);
    }

Output: TestLombok(aa=null)

solution

Just add @Builder.Default annotation to the field

@Builder.Default
private String aa = "zzzz";

Cause Analysis

We use annotations. The underlying essence is that reflection generates a series of setters and getters for us, so we directly open the .class file under the compiled target package, and all the reasons above are clear at a glance!
Source File:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestLombok {

    private String aa = "zzzz";
    
    public static void main(String[] args) {
        TestLombok build = TestLombok.builder().build();
        System.out.println(build);
    }
}

Corresponding class bytecode:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.apple.ucar;

public class TestLombok {
    private String aa = "zzzz";

    public static void main(String[] args) {
        TestLombok build = builder().build();
        System.out.println(build);
    }
    
    public static TestLombok.TestLombokBuilder builder() {
        return new TestLombok.TestLombokBuilder();
    }
    
    public String getAa() {
        return this.aa;
    }
    
    public void setAa(String aa) {
        this.aa = aa;
    }
    
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof TestLombok)) {
            return false;
        } else {
            TestLombok other = (TestLombok)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$aa = this.getAa();
                Object other$aa = other.getAa();
                if (this$aa == null) {
                    if (other$aa != null) {
                        return false;
                    }
                } else if (!this$aa.equals(other$aa)) {
                    return false;
                }
    
                return true;
            }
        }
    }
    
    protected boolean canEqual(Object other) {
        return other instanceof TestLombok;
    }
    
    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $aa = this.getAa();
        int result = result * 59 + ($aa == null ? 43 : $aa.hashCode());
        return result;
    }
    
    public String toString() {
        return "TestLombok(aa=" + this.getAa() + ")";
    }
    
    public TestLombok() {
    }
    
    public TestLombok(String aa) {
        this.aa = aa;
    }
    
    public static class TestLombokBuilder {
        private String aa;
    
        TestLombokBuilder() {
        }
    
        public TestLombok.TestLombokBuilder aa(String aa) {
            this.aa = aa;
            return this;
        }
    
        public TestLombok build() {
            return new TestLombok(this.aa);
        }
    
        public String toString() {
            return "TestLombok.TestLombokBuilder(aa=" + this.aa + ")";
        }
    }
}

We want to know what the bottom layer of @Data, @Builder and other annotations does. Just compile the current file directly and check the specific code in the generated .class bytecode file.

For example, the second point above, when using @Builder, this aa does not have a default value, so it will be empty! !

  public TestLombok.TestLombokBuilder aa(String aa) {
            this.aa = aa;
            return this;
        }

Summarize

I personally think that if you want to use @Builder, the easiest way is to write these 4 comments directly. If there are default values, add @Builder.Default directly. Normally, there is no problem!

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TestLombok {

    @Builder.Default
    private String aa = "zzzz";
    
    public static void main(String[] args) {
        TestLombok build = TestLombok.builder().build();
        System.out.println(build);
    }
}

skyarthur
1.6k 声望1.3k 粉丝

技术支持业务,技术增强业务,技术驱动业务