Stack Exchange
Reflection into Builders and ArrayList
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 ?

What is the actual problem you're having? Do you get a compilation error? Is an exception thrown somewhere?

The solution might be to use some typetoken, see https://github.com/google/guava/wiki/ReflectionExplained

Yes, the process throw an error :
fr.univtln.procrastinateurs.m1.dapm.Utils.Builder.Exception.BuilderException: fr.univtln.procrastinateurs.m1.dapm.Entities.Inventory.InventoryJPA.(java.lang.Object) when I've tried with Object.class instead.

@RC I don't understand the logic behind. Do I have to create TypeToken only for the builder's attributes and use getClass() for the DelegateBuilder constructor ?

I tried :
Type listType = new TypeToken<ArrayList>(){}.getType();
    System.out.println(listType.getTypeName());
    delegateBuilder = new DelegateBuilder<>(constructorClass,
            new Class[]{listType.getClass()});
}

But it's still not working :(

The problem is related to type erasure.
Basically the compiler apply type erasure to:

Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.


I already tried with Object.class instad of Arraylist.class, but it's still not working.
