是否有可能在Java中反射性地实例化泛型类型?使用这里描述的技术我得到一个错误,因为类标记不能是通用的.以下面的例子为例.我想实例化一些实现Creator的Creator子类.实际的类名称作为命令行参数传入.我们的想法是能够在运行时指定Creator的实现.还有另一种方法来完成我在这里要做的事情吗?
public interface Creator{ T create(); } public class StringCreator implements Creator { public String create() { return new String(); } } public class FancyStringCreator implements Creator { public String create() { return new StringBuffer().toString(); } } public static void main(String[] args) throws Exception { Class> someClass = Class.forName(args[0]); /*ERROR*/Class extends Creator > creatorClass = someClass.asSubclass(Creator.class); Constructor extends Creator > creatorCtor = creatorClass.getConstructor((Class>[]) null); Creator creator = creatorCtor.newInstance((Object[]) null); }
编辑:我喜欢马库斯的方法是最简单务实的,不会绕过整个仿制品.我可以在我的情况下使用它,因为我可以指定传递的类必须是StringCreator的子类.但是,正如Ericson指出的那样,通用信息仍然存在于类型级别,而不是在运行时级别,因此仍然可以反思地检查给定的类是否实现了正确的泛型类型.
通用信息在运行时丢失.没有运行时等效的Creator
public interface Creator{ T create(); } public interface StringCreator extends Creator { } public class StringCreatorImpl implements StringCreator { public String create() { return new String(); } } public class FancyStringCreator implements StringCreator { public String create() { return new StringBuffer().toString(); } } public static void main(String[] args) throws Exception { Class> someClass = Class.forName(args[0]); Class extends StringCreator> creatorClass = someClass.asSubclass(StringCreator.class); Constructor extends StringCreator> creatorCtor = creatorClass.getConstructor((Class>[]) null); Creator creator = creatorCtor.newInstance((Object[]) null); }
但是当然你会失去一点灵活性,因为你不能使用以下创建者类:
public class AnotherCreator implements Creator{ public String create() { return ""; } }
这将在您提供类型安全的同时完成您要做的事情.没有办法避免未经检查的警告,但是这里完成的类型检查证明了它的抑制.
public static void main(String[] args) throws Exception { Class extends Creator> clz = load(argv[0], String.class); Constructor extends Creator > ctor = clz.getConstructor(); Creator creator = ctor.newInstance(); System.out.println(creator.create()); } public static Class extends Creator > load(String fqcn, Class type) throws ClassNotFoundException { Class> any = Class.forName(fqcn); for (Class> clz = any; clz != null; clz = clz.getSuperclass()) { for (Object ifc : clz.getGenericInterfaces()) { if (ifc instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) ifc; if (Creator.class.equals(pType.getRawType())) { if (!pType.getActualTypeArguments()[0].equals(type)) throw new ClassCastException("Class implements " + pType); /* We've done the necessary checks to show that this is safe. */ @SuppressWarnings("unchecked") Class extends Creator > creator = (Class extends Creator >) any; return creator; } } } } throw new ClassCastException(fqcn + " does not implement Creator "); }
您必须遵守的主要限制是层次结构中的类必须指定类型参数.例如class MyCreator implements Creator
.你不能用它class GenericCreator
.
它目前不处理创建新接口的有效情况interface StringCreatorIfc extends Creator
,并且具有类实现.可以通过增强来做到这一点,但我会将其作为锻炼对象.