Для того, чтобы получить мои строитель в фабрику (что):Отражения в строитель и ArrayList
public Class<? extends ILink> getLinkImplClass(){
return LinkJPA.class;
}
public LinkBuilder getLinkBuilder() throws BuilderException {
return new LinkBuilder(getLinkImplClass());
}
Я использую 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);
}
}
}
Он отлично работает в течение несколько строителя, но когда Я должен использовать общий список, которого нет.
Это мой 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});
}
}
(К сожалению, я не нашел, как правильно выразился предыдущий код)
items.getClass()
, как представляется, ArrayList
и не ArrayList<IITem>
.
Есть ли решение для этого?
В чем проблема? Вы получаете ошибку компиляции? Есть ли какое-то исключение? – Radiodef
Решение может заключаться в использовании некоторых typetoken, см. Https://github.com/google/guava/wiki/ReflectionExplained –
Да, процесс выдает ошибку: fr.univtln.procrastinateurs.m1.dapm.Utils.Builder .Exception.BuilderException: fr.univtln.procrastinateurs.m1.dapm.Entities.Inventory.InventoryJPA. (java.lang.Object), когда я попытался использовать Object.class. –