uglycustard / buildergenerator

A tool to auto generate builders following the Builder pattern for an object graph of JavaBeans.
Apache License 2.0
5 stars 2 forks source link

Generate builders for interface based object heirarchies #35

Open azzoti opened 9 years ago

azzoti commented 9 years ago

Given:

 public class Basket {
    private List<IBasketProduct> products = new ArrayList<>();
    public List<IBasketProduct> getProducts() {
        return products;
    }
    public void setProducts(List<IBasketProduct> products) {
        this.products = products;
    }

Where IBasketProduct is an interface with a single implementation BasketProduct, then the generator generates the following withProduct builder method (that does not take a builder parameter):

public T withProduct(com.acme.IBasketProduct product) {
    getTarget().getProducts().add(product);
    return (T) this;
}

whereas the generated code using a concrete BasketProduct takes a BasketProductBuilder parameter and generates the BasketProductBuilder:

public T withProduct(com.acme.generatedbuilders.BasketProductBuilder product) {
    getTarget().getProducts().add(product.build());
    return (T) this;
}

It would be very useful in such scenarios to tell the generator the name of the concrete class to use for each interface found, so that a builder would be created and used. For example:

    generator.addInterfaceImplementation(IBasketProduct.class, BasketProduct.class);
uglycustard commented 8 years ago

Hi Tim, Issue #36 just changed BG such that instead of any concrete builders passed to the with methods - an interface is used instead (Builder) which the concrete builders then implement. This allows for other implementations to be used if required. However the behaviour for properties that are interfaces is for the parameter to be an instance of the expected type, i.e not a builder. Now we have the interface, we could replace or perhaps overload this with method with a Builder method. What do you think? Cheers.

azzoti commented 8 years ago

Sounds very reasonable I think. I will have to have a look at the new BG.