I'm trying to achieve to get a class from his name.
In my case all classes I want to get implements the generic interface Prototype<T>
.
But the delicate part of my problem is that I'm "Warningophobe".
I wrote:
// Warning: Usage of interface raw type
Class<? extends Prototype> prototypeClass;
try {
Class<?> clazz = Class.forName("mova.game.monster.Goblin");
prototypeClass = clazz.asSubclass(Prototype.class);
System.out.println(prototypeClass);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
This code work, but here, my IDE (IntellIJ) tell me that I'm using my "raw interface".
I know I can do that:
Class<? extends Prototype<?>> prototypeClass;
try {
Class<?> clazz = Class.forName("mova.game.monster.Goblin");
// Warning: unchecked cast
prototypeClass = (Class<? extends Prototype<?>>) clazz.asSubclass(Prototype.class);
System.out.println(prototypeClass);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
But now, I have an uncheked cast.
So, is there a proper way to achieve this problem without warning?