Lombok builder package scope

k13i :

I would like to generate package-scope builder using Lombok, but I'm not sure if it's possible (I didn't find any clues in documentation).

By default Lombok generates public builder, i.e. this code:

@Builder
class User {

    private final String name;
}

Is translated into this:

class User {
    private final String name;

    User(final String name) {
        this.name = name;
    }

    public static User.UserBuilder builder() { // <-- how to make it package-private?
        return new User.UserBuilder();
    }

    public static class UserBuilder { // <-- how to make it package-private?
        private String name;

        UserBuilder() {
        }

        public User.UserBuilder name(final String name) {
            this.name = name;
            return this;
        }

        public User build() {
            return new User(this.name);
        }

        public String toString() {
            return "User.UserBuilder(name=" + this.name + ")";
        }
    }
}

Is there any way to generate builder class without this leading public keyword?

Madhu Bhat :

Check out the below in the @Builder documentation:

@Builder(access = AccessLevel.PACKAGE) is legal (and will generate the builder class, the builder method, etc with the indicated access level) starting with lombok v1.18.8

And if you see the source code of Builder here, you'll see that by default, the access level of a @Builder would be lombok.AccessLevel.PUBLIC, but can be made package-private with @Builder(access = AccessLevel.PACKAGE).

Also FYI, the following access levels are supported by @Builder : PUBLIC, MODULE, PROTECTED, PACKAGE, PRIVATE. This is via the AccessLevel enum's source code here.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=126454&siteId=1