I have created a Set of classes which implements a certain interface using Reflections like that:
Reflections reflections = new Reflections("com.project.cookies");
Set<Class<? extends Cookie>> setOfCookies = reflections.getSubTypesOf(Cookie.class);
Now for all cookie i want to bake it eg.:
setOfCookies.stream().forEach(cookie -> cookie.bake());
When I call
cookie.getName()
I get:
com.project.cookies.Brownie
Brownie
class implements the interface Cookie
and has a method bake()
.
I've tried to cast cookie element of type Class<? extends Cookie>
into Cookie
or Brownie
, but I get:
java.lang.ClassCastException: Cannot cast java.lang.Class to com.project.cookies.Cookie
I've also tried
cookie.getSuperclass().getConstructor(Cookie.class).newInstance()
but ite gives me some weird type capture of ? super capture of ? extends Cookie
and i cannot call method on it.
My question is how can I access bake()
method having a <Class<? extends Cookie>
?