public <T> Entity<T> build() {
return new Entity<T>(body);
}
public String toString() {
return "com.audi.mobility.booking.model.Entity.EntityBuilder(body=" + this.body + ")";
}
}
}As you can see, it does use a type definition in the de-lomboked builder, but it uses the same type parameter name <T>, which the compiler then complains about since that one is defined in the context of the Entity class and access from the context of the static builder class.
Two changes are necessary:
1. The created builder class needs to define its own type parameter and use it in the generated fluid builder methods:
public static class EntityBuilder<K> {
private K body;
EntityBuilder<K>() {
}
public Entity.EntityBuilder<K> body(K body) {
My example deals with a single type parameter. You could imagine that someone uses more than one parameter, though. I'm not sure how the code generation works with lombok, but a solution to this problem will probably have to keep track of every type parameter individually. In order to avoid naming conflicts I would suggest a naming scheme. For example if the entity has a type parameter of name T, the builder could use a type parameter of name TT.
Here's an additional example covering two parameters:
public class Entity<K, V> {
K key;
V value;
@java.beans.ConstructorProperties({"key", "value"})
Entity(K key, V value) {
this.key = key;
this.value = value;
}
public static <KK, VV> EntityBuilder<KK, VV> builder() {
return new EntityBuilder<>();
}
public static class EntityBuilder<KK, VV> {
private KK key;
private VV value;
EntityBuilder() {
}
public Entity.EntityBuilder<KK, VV> key(KK key) {
this.key = key;
return this;
}
public Entity.EntityBuilder<KK, VV> value(VV value) {
this.value = value;
return this;
}
public Entity<KK, VV> build() { return new Entity<>(key, value);
}
public String toString() {
return "com.audi.mobility.booking.model.Entity.EntityBuilder(key=" + this.key + ", value=" + this.value + ")";
}
}
}
Let me know if I can provide additional information, I'm happy to help!
Cheers,
Reinhard