简体   繁体   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?

I'm working on a program that utilizes evolutionary algorithms. 我正在开发一个利用进化算法的程序。 This program was written prior to the introduction of Java generics. 该程序是在引入Java泛型之前编写的。 One of the many tasks I've been given is to remove the “raw type” warnings this legacy code is producing. 我获得的许多任务之一是删除该旧代码正在生成的“原始类型”警告。

I've looked at several Java reference books and read all the answers to questions concerning the removal of these type warnings and haven't found what I'm looking for. 我看了几本Java参考书,并阅读了有关删除这些类型警告的所有问题的答案,但是没有找到我想要的东西。 I can't simply add the ? 我不能简单地添加?? wildcard to the variable declarations to remove the warnings unless that is the only valid solution. 除非声明是唯一有效的解决方案,否则对变量声明使用通配符删除警告。

Here is an example snippet of code that is issuing warnings. 这是发出警告的示例代码片段。 Knowing what the contents of the three variables that are issuing the “raw type” warnings are going to be, how do I determine what to place inside the <> to eliminate the warnings? 知道发出“原始类型”警告的三个变量的内容将是什么,我如何确定在<>中放置什么以消除警告?

Thanks in advance. 提前致谢。

/**
 * 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() returns Class<?> , see documentation . Class.forName()返回Class<?> ,请参阅文档

It can't return a Class<String> even when you specify java.lang.String since there is no mapping between the string name and the actual class at compile time. 即使指定java.lang.String它也不会返回Class<String> ,因为在编译时字符串名称和实际类之间没有映射。

The same applies to constructors, parameters, .. You can only specify a concrete class type if you have a real runtime type (obtainable via String.class for example). 构造函数,参数等也是如此。只有在具有真正的运行时类型时,才能指定具体的类类型(例如,可以通过String.class获得)。

I'd do it roughly like: 我大概会这样:

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);

There is no need to create extra arrays, the methods take a comma separated list of things (varargs). 无需创建额外的数组,这些方法采用逗号分隔的事物列表(变量参数)。 You also don't need to specify forName("java.lang.String") since you can use the actual type here. 您也不需要指定forName("java.lang.String")因为您可以在此处使用实际的类型。 And since you seem to know the type, you can use the getConstructor method instead of hoping that the first constructor is the correct one. 而且,由于您似乎知道类型,因此可以使用getConstructor方法,而不希望第一个构造函数是正确的。

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

相关问题 如何在不知道我返回的对象类型的情况下实现一个在Java中返回的方法? - How do I implement a method that returns in Java without knowing what object type I'm returning? 如何在不知道字段是什么的情况下创建ElasticSearch查询? - How do I create an ElasticSearch query without knowing what the field is? 对于浮动变量变为无穷大的嵌套if语句,我还有什么其他选择? - What other alternatives do I have for nested if statements for float variables going to infinity? 我如何仅在知道该方法应该做什么的情况下找到一种在Java中使用的方法? - How do I find a certain method to use in java knowing only what the method should do? 如何确定哪个android小部件正在调用我的函数? - How do I determine what android widget is calling my function? 如何确定前景中正在使用哪个应用程序? - How do I determine what app is currently in the foreground? 如何确定android中输入设备的来源? - How do I determine what is the source of Input Device in android? 什么是“类型不匹配”,如何解决? - What is “Type mismatch” and how do I fix it? 这些ANTLR警告是什么意思 - What do these ANTLR warnings mean GSON:知道要转换为什么类型的对象? - GSON: knowing what type of object to convert to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM