Reputation: 4514
I have defined a class with a builder and now I would like to limit boilerplate code using Lombok's @Builder
annotation.
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));
}
}
However, the annotation will not generate clientId(String)
method, only clientId(UUID)
. How can I generate it with Lombok?
Upvotes: 3
Views: 7670
Reputation: 3882
I could not get the @Tolerate
method to work (I may be doing it wrong).
/**
* 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));
}
}
But if you are using java 8 you can workaround the fact that lombok matches only the method name and not the entire signature when generating builder methods by using an interface with a default method.
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());
}
}
Upvotes: 0
Reputation: 34452
Well, lombok will not generate this for you but you can use @Builder
with and have a ClientAppBuilder class containing the one method that accepts a String and routes it to the other, to be generated, method. Possibly you need to mark your method with @Tolerate, otherwise Lombok will not generate the UUID accepting method.
Disclosure: I am a Lombok developer.
Upvotes: 4