lombok.Builder @Builder compilation Error:(xx,xx)java: cannot find symbol class Builder

catalogue

1. Preface (nonsense)

2. Test code

2.1 basic heading

2.2 add a static method in OuterClass: the input parameter is User and the return value is OuterClass.java (compiled correctly)

2.3 modify OuterClass to add static methods, and add some common business logic judgments (compilation error java: no symbols found)

1. Preface (nonsense)

In daily software development, the creator mode (or creator mode) is a very practical design mode, which is used to easily create an object, reduce the time to write methods such as get, set and parameterless constructor, especially when there are many members of the class, and greatly reduce the length of the code (how many get and set methods there are).

Through the @ Builder annotation in lombok package, we can easily implement the get, set and other methods of a class. Unfortunately, in actual use, if there is still a static internal class in the class, Error:(xx,xx)java: can't find the symbol problem (this is related to the order of use of external classes and internal classes in the code) may occur during compilation. Click the error in the prompt bar, and idea will automatically move the cursor to the @ Builder annotation.

2. Test code

2.1 basic heading

User.java

import lombok.Data;

@Data
public class User {
    private String name;
}

OuterClass.java and InnerClass.java

import lombok.Builder;
import lombok.Getter;


@Builder
@Getter
public class OuterClass {

    // A static inner class is a member variable of an outer class
    private InnerClass innerClass;

    @Builder
    @Getter
    public static class InnerClass {
        private User user;
    }
}

2.2 add a static method in OuterClass: the input parameter is User and the return value is OuterClass.java (compiled correctly)

import com.test.OuterClass.InnerClass.InnerClassBuilder;

import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class OuterClass {

    private InnerClass innerClass;

    public static OuterClass toOuterClass(User user) {
        return OuterClass.builder()
                .innerClass(InnerClass.builder()
                        .user(user)
                        .build())
                .build();
    }

    @Builder
    @Getter
    public static class InnerClass {
        private User user;
    }
}

2.3 modify OuterClass to add static methods, and add some common business logic judgments (compilation error java: no symbols found)

Add a judgment to judge whether the user is null. If not, we will build the user object into the InnerClass object.

import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class OuterClass {

    private InnerClass innerClass;


    public static OuterClass toOuterClass(User user) {

        InnerClassBuilder builder = InnerClass.builder();
        if (user != null) {
            // Judge whether user is empty
            builder.user(user);
        }
        return OuterClass.builder()
                .innerClass(builder.build())
                .build();
    }

    @Builder
    @Getter
    public static class InnerClass {
        private User user;
    }
}

I found a similar problem on the Internet, Lombok The data @ data annotation explains the parsing of static classes. Before the annotation parser generates the builder, the symbol cannot be found: cannot find symbol class xxx.

cannot find symbol class Data if import inner inner class · Issue #1684 · projectlombok/lombok · GitHub

My solution is to implement the creator mode code of Builder by myself. The simplest way is to find the first successfully compiled code and find the automatically generated XXX Class file, directly copy the relevant code in it, paste it into the source code, and remove the @ Builder annotation on the class.

Tags: Java servlet Lombok programming language

Posted by php_coder_dvo on Sun, 04 Sep 2022 02:34:19 +0930