java:unchecked调用getConstructor(java.lang.Class<?> ...) [英] java: unchecked call to getConstructor(java.lang.Class<?>...)
本文介绍了java:unchecked调用getConstructor(java.lang.Class<?> ...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用反射来构造一个以File作为参数的类(ConfigBuilder):
I am using reflection to construct a class (ConfigBuilder) that takes a File as argument:
Class myClassType = Class.forName(className);
Class[] types = new Class[] { File.class };
Constructor cons = myClassType.getConstructor(types);
Object[] constructorArgs = new Object[] { myFile };
cb = (ConfigBuilder) cons.newInstance(constructorArgs);
但我收到此警告:
warning: [unchecked] unchecked call to getConstructor(java.lang.Class<?>...) as a member of the raw type java.lang.Class
Constructor cons = myClassType.getConstructor(types);
显然,似乎getConstructor需要一个泛型类型,所以我尝试了类似的东西:
Obviously, it seems that getConstructor expects a generic type so I tried something like:
Class<?>[] types = new Class<?>[] { File.class };
但我收到相同的警告信息
but I get the same warning message
任何想法?
David
推荐答案
警告实际上是指 myClassType
。您还需要对其进行参数化(以及 cons
)。
The warning actually refers to myClassType
. You need to parameterize it (and the cons
) as well.
Class<?> myClassType = Class.forName(className);
Class<?>[] types = new Class[] { File.class };
Constructor<?> cons = myClassType.getConstructor(types);
这篇关于java:unchecked调用getConstructor(java.lang.Class<?> ...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文