Java – Lombok builder containing existing data value classes
•
Java
One library provides classes, such as org springframework. security. oauth2. provider. client. Baseclient details, which I want to wrap as a Lombok (or similar) builder
At present, I have derived such a wrapper class:
public static final class BaseClientDetailsWrapper extends BaseClientDetails { @Builder private BaseClientDetailsWrapper( final String clientId,final List<String> resourceIds,final List<GrantedAuthority> suthorities,final List<String> scopes,final List<String> autoApproveScopes,final List<String> authorizedGrantTypes,final String clientSecret) { super(); setClientId(clientId); setResourceIds(resourceIds); setAuthorities(authorities); setScope(scopes); setAuthorizedGrantTypes(authorizedGrantTypes); setAutoApproveScopes(autoApproveScopes); setClientSecret(clientSecret); } }
Is there any way to get rid of the annoying setXXX (...) code?
Solution
Is there a constructor that better meets your needs? No, there's no way to ask Lombok to treat other features as builder templates that you don't control
As a side note, there is no need to subclass baseclientdetails You can put @ builder on any function, no matter which class it is in The following are fully acceptable:
@Builder(builderMethodName = "fullname") private static String fullnameHelper(String forename,String middle,String surname) { Objects.requireNonNull(forename); Objects.requireNonNull(surname); return forename + " " + (middle == null ? "" : middle + " ") + surname; }
The name of the method is not important at all If necessary, you can hide it privately in the utility class You can use it this way:
fullname().forename("Alan").surname("Turing").build()); fullname().forename("Donald").middle("E.").surname("Knuth").build());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码