我已经使用构建器定义了一个类,现在我想使用Lombok的@Builder
注释来限制样板代码。
public class ClientApp {
private UUID clientId;
ClientApp(UUID clientId) {
this.clientId = clientId;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private UUID clientId;
public Builder clientId(String clientId) {
return clientId(UUID.fromString(clientId));
}
public Builder clientId(UUID clientId) {
this.clientId = clientId;
return this;
}
public ClientApp build() {
return new ClientApp(this.clientId);
}
}
public Builder clientId(String clientId) {
return clientId(UUID.fromString(clientId));
}
}
但是,注释不会生成clientId(String)
方法,只会生成clientId(UUID)
。如何使用Lombok生成它?
答案 0 :(得分:4)
好吧,lombok不会为你生成这个,但你可以使用@Builder
并且有一个ClientAppBuilder类,其中包含一个接受String的方法,并将其路由到另一个要生成的方法。可能需要使用@Tolerate标记方法,否则Lombok不会生成UUID接受方法。
披露:我是龙目岛的开发者。
答案 1 :(得分:0)
我无法使用@Tolerate
方法(我可能做错了)。
/**
* Fails with The method clientId(String) in the type So33943193.ClientApp.ClientAppBuilder is not applicable
* for the arguments (UUID)
*/
public static class ClientAppBuilder {
@Tolerate
public ClientAppBuilder clientId(String clientId) {
return this.clientId(UUID.fromString(clientId));
}
}
但是如果您使用的是Java 8,则可以通过使用具有默认方法的接口生成构建器方法时,解决lombok仅匹配方法名称而不匹配整个签名的事实。
package lombok.javac.handlers.stackoverflow;
import static org.junit.Assert.*;
import java.util.UUID;
import lombok.Builder;
import lombok.Value;
import org.junit.Test;
public class So33943193 {
/**
* The Interface WithClientUUID.
*
* @param <BLDR> the builder type
*/
public static interface WithClientUUID<BLDR extends WithClientUUID<BLDR>> {
/**
* Client id.
*
* @param clientId the client id
* @return the bldr
*/
default BLDR clientId(String clientId) {
return this.clientId(UUID.fromString(clientId));
}
/**
* Client id.
*
* @param clientId the client id
* @return the bldr
*/
BLDR clientId(UUID clientId);
}
/**
* The Class ClientApp.
*/
@Builder(toBuilder = true)
@Value
public static class ClientApp {
/** The client id. */
private final UUID clientId;
/**
* Instantiates a new client app.
*
* @param clientId the client id
*/
ClientApp(UUID clientId) {
this.clientId = clientId;
}
/**
* Implement the builder and extend the interface.
*/
public static class ClientAppBuilder implements WithClientUUID<ClientAppBuilder> {}
}
/**
* Test all scenarios
*/
@Test
public void testClientApp() {
UUID expected = UUID.randomUUID();
final ClientApp fromUuid = ClientApp.builder()
.clientId(expected)
.build();
final ClientApp fromString = ClientApp.builder()
.clientId(expected.toString())
.build();
assertEquals(expected, fromUuid.getClientId());
assertEquals(expected, fromString.getClientId());
}
}