繁体   English   中英

知道变量的内容之后,如何确定要放置在<>中的内容以消除“原始类型”警告?

[英]Knowing what the contents of variables are going to be, how do I determine what to place inside the <> to eliminate “raw type” warnings?

我正在开发一个利用进化算法的程序。 该程序是在引入Java泛型之前编写的。 我获得的许多任务之一是删除该旧代码正在生成的“原始类型”警告。

我看了几本Java参考书,并阅读了有关删除这些类型警告的所有问题的答案,但是没有找到我想要的东西。 我不能简单地添加?? 除非声明是唯一有效的解决方案,否则对变量声明使用通配符删除警告。

这是发出警告的示例代码片段。 知道发出“原始类型”警告的三个变量的内容将是什么,我如何确定在<>中放置什么以消除警告?

提前致谢。

/**
 * Uses the information from the configuration file to instantiate
 * the right classes for the environment, statistics, algorithm slots
 */

private void reificate() {
try{
Raw type warning --> Class c =  null;
Raw type warning --> Class params1 [] =  new Class[1];
Raw type warning --> java.lang.reflect.Constructor  cons =  null;
                     Object [] params2 =  new Object[1];

// Instantiating a configuration object

c           =  Class.forName(cfgSB.toString());
params1[0]  =  Class.forName("java.lang.String");
cons        =  c.getConstructor(params1);
params2[0]  =  new String(cfgFileName);
cfg  =  cons.newInstance(params2);

(The following code will require additional Class type variables to be declared, but if I can 
learn how to eliminate the warnings above, I’ll know how to declare the new variables properly.)

// Instantiating an environment

c           =  Class.forName(envSB.toString());
params1[0]  =  Class.forName("jade.core.Config");
cons        =  c.getConstructors()[0];
params2[0]  =  cfg;
env  =  cons.newInstance(params2);

// Instantiating an Algo object to run

c           =  Class.forName(algSB.toString());
cons        =  c.getConstructors()[0];
params2[0]  =  cfg;
alg  =  (jade.core.EvolAlgo)cons.newInstance(params2);

// Creating statistics object and attaching it to cfg

// This has to be done last since statistics constructor is
// going to refer to the population of Algo via  a local attribute C

c           =  Class.forName(staSB.toString());
cons        =  c.getConstructors()[0];
params2[0]  =  cfg;
sta  =  (jade.stats.Statistics)cons.newInstance(params2);
// ********************* end of snippet **************************//

Class.forName()返回Class<?> ,请参阅文档

即使指定java.lang.String它也不会返回Class<String> ,因为在编译时字符串名称和实际类之间没有映射。

构造函数,参数等也是如此。只有在具有真正的运行时类型时,才能指定具体的类类型(例如,可以通过String.class获得)。

我大概会这样:

jade.core.Config cfg = (jade.core.Config) Class.forName(cfgSB.toString())
        .getConstructor(String.class)
        .newInstance(cfgFileName);

// not sure what type that is..
Object env = Class.forName(envSB.toString())
        .getConstructor(jade.core.Config.class)
        .newInstance(cfg);

jade.core.EvolAlgo alg = (jade.core.EvolAlgo) Class.forName(algSB.toString)
        .getConstructor(jade.core.Config.class)
        .newInstance(cfg);

jade.stats.Statistics sta = (jade.stats.Statistics) Class.forName(staSB.toString())
        .getConstructor(jade.core.Config.class)
        .newInstance(cfg);

无需创建额外的数组,这些方法采用逗号分隔的事物列表(变量参数)。 您也不需要指定forName("java.lang.String")因为您可以在此处使用实际的类型。 而且,由于您似乎知道类型,因此可以使用getConstructor方法,而不希望第一个构造函数是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM