Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Support custom builder methods referring to fields with defaults #3810

Open
scf37 opened this issue Jan 17, 2025 · 0 comments
Open

Comments

@scf37
Copy link

scf37 commented Jan 17, 2025

Describe the feature
add protected getters to builders so custom builder methods can use them.

Current situation:

@Builder
public final class Response {
    private final int statusCode;
    @Builder.Default private final Headers headers = Headers.newInstance();
    
    public static class ResponseBuilder {
        public ResponseBuilder header(String name, String value) {
            if (!headers$set) headers$value = $default$headers();
            headers$value.put(name, value);
            return this;
        }
    }
}

After the change:

@Builder
public final class Response {
    private final int statusCode;
    @Builder.Default private final Headers headers = Headers.newInstance();
    
    public static class ResponseBuilder {
        public ResponseBuilder header(String name, String value) {
            headers().put(name, value);
            return this;
        }
    }
}

Implementation:

    public static class ResponseBuilder {
        public ResponseBuilder header(String name, String value) {
            headers().put(name, value);
            return this;
        }
        protected Headers headers() { // protected since default Builder is not final - can be extended?
            if (!headers$set) headers$value = $default$headers();
            return headers$value;
        }
    }

Describe the target audience
Anyone needing custom methods in their builders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant