Is it possible to reflectively instantiate a generic type in Java? Using the technique described here I get an error because class tokens cannot be generic. Take the example
You don't need that line. Nor do you need the constructor as you're just using the default one. Just instantiate the class directly:
public static void main(String[] args) throws Exception {
Class> someClass = Class.forName(args[0]);
Creator creator = (Creator) someClass.newInstance();
}
If you insist, you'll only be able to get halfway there:
public static void main(String[] args) throws Exception {
Class> someClass = Class.forName(args[0]);
Class extends Creator> creatorClass = someClass.asSubclass(Creator.class);
Constructor extends Creator> creatorCtor = creatorClass.getConstructor((Class>[]) null);
Creator creator = (Creator) creatorCtor.newInstance((Object[]) null);
}