mardi 25 août 2015

create array list of instances of annotated classes

I have a problem with creating an arraylist of instances. I got a Set<Class<?>> and then I check if they implement a specific interface. If they do I want to add an instance of this class to an arraylist of the specific interface.

This is my code:

ArrayList<MyInterface> list = new ArrayList<>();
for (Class clazz : annotatedClasses) {
    if(MyInterface.class.isAssignableFrom(clazz)) {
        Object instance = clazz.getConstructor().newInstance();
        list.add(object); //ERROR: Object != MyInterface
    }
}

How can I solve this?





Aucun commentaire:

Enregistrer un commentaire