In order to get my builders into a factory (like that):
public Class<? extends ILink> getLinkImplClass(){
return LinkJPA.class;
}
public LinkBuilder getLinkBuilder() throws BuilderException {
return new LinkBuilder(getLinkImplClass());
}
I use a delegateBuilder :
public class DelegateBuilder<T> {
private final Constructor<T> constructor;
public DelegateBuilder(Class<? extends T> constructorClass, Class<?>[] expectedTypes) throws BuilderException {
try {
constructor = (Constructor<T>) constructorClass.getConstructor(expectedTypes);
} catch (NoSuchMethodException e) {
throw new BuilderException(e);
}
}
public T build(Object[] params) throws BuilderException {
try {
return constructor.newInstance(params);
} catch (Exception e) {
throw new BuilderException(e);
}
}
}
It works fine for several builder, but when I've to use a Generic List it doesn't.
This is my InventoryBuilder :
public class InventoryBuilder {
private DelegateBuilder<IInventory> delegateBuilder;
/**
* List of items to set to the inventory to build
*/
private List<IItem> items;
/**
* Public constructor of the InventoryBuilder
*/
public InventoryBuilder(Class<? extends IInventory> constructorClass) throws BuilderException {
items = new ArrayList<>();
delegateBuilder = new DelegateBuilder<>(constructorClass,
new Class[]{items.getClass()});
}
public InventoryBuilder addItems(List<IItem> items)
{
this.items.addAll(items);
return this;
}
public InventoryBuilder addItems(IItem ... items)
{
this.items.addAll(Arrays.asList(items));
return this;
}
/**
* Function used to build the new Inventory
*
* @return
* The new Inventory
*/
public IInventory build() throws BuilderException {
return delegateBuilder.build(new Object[]{items});
}
}
(Sorry I don't find how to put correctly the previous code)
items.getClass()
seems to be an ArrayList
and not a ArrayList<IITem>
.
Is there a solution for this ?
Aucun commentaire:
Enregistrer un commentaire