there is

Map<Class<?>, AbstractClass> holder = new HashMap<>(); 

Is filled

 final Reflections s = new Reflections("reflection.components"); for(Class c : s.getSubTypesOf(AbstractComp.class)) { holder.put(c, (AbstractComp) c.newInstance()); } 

Displays the class inside:

 System.out.println(holder.get(Inner.class).getClass().getSimpleName()); 

Question: With each call, I need to get the component with its methods through an explicit type conversion: (Inner) holder.get(Inner.class) . How can I get this kind of application in code: holder.get(Inner.class).callMethod() without explicit type casting and without checking for instanceof, because java already knows what subTypes lie inside, and this means test the option of implicit type casting through generics or something else, but I still do not have enough brain to realize it.

Thank!

    1 answer 1

    All figured out! Thank you all! Output - method:

     <T> T get(Class<T> clazz) { return (T) holder.get(clazz); } 
    • nobody seemed to help you much) - michael_best
    • probably we were here the same rubber duck, about it, even someone wrote on the meta) - Stranger in the Q