mardi 7 juillet 2015

find the classes which implement an interface and call the abstract method

I have an interface i.e. Template and it has one method. Below is the signature of the interface.

public interface Template {
    public void load();
}

Some of the classes those implement the interface are: TemplateImpl1 and TempplateImpl2. TemplateImpl1 and TempplateImpl2 and implemented as enum singleton pattern.

public enum TemplateImpl1 implements Template {
    INSTANCE;

    @Override
    public void load() {
        // some logic of loading from DB
    }
}

And now what i want is the logic by which i can find the classes or enums those implement the interface and call the load on them. I can use reflections library and could get hold of the class that implement the interface.

Reflections reflections =  new Reflections(<package information>);
Set<Class<? extends Template>> subTypes = reflections.getSubTypesOf(Template.class); 

But how could i call the method load on the class ? (need to be careful becuase TemplateImpl1 and TemplateImpl2 are enums).





Aucun commentaire:

Enregistrer un commentaire