简体   繁体   中英

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. 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. 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 .

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.

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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