What step can I take to fix the warning of "Raw use of parameterized class 'Class' "?

Viewed 2653

In the program below, I have class Cow, class Dragon derived from class Cow and class IceDragon derived from class Dragon.

There is also the HeiferGenerator class, responsible for creating an array containing instances of Cow, Dragon and IceDragon.

However, in the HeifeferGenerator class, I get the warning "Raw use of parameterized class 'Class'" on the line:

private static final Class[] dragonTypes = {Dragon.class, IceDragon.class};

I tried looking at the answer at Raw use of parameterized class, but it was not of any help. What can I do to fix this warning?

// Cow class

public class Cow {

    // Declaring attributes name and image
    private final String name;
    private String image;

    // Constructor to create a new Cow object with parameter name
    public Cow (String name) {
        this.name = name;
        this.image = null;
    }

    // Accessor to return the name of the cow
    public String getName() {
        return this.name;
    }

    // Accessor to return the image used to display the cow after the message
    public String getImage() {
        return this.image;
    }

    // Mutator to set the image used to display the cow after the message
    public void setImage(String _image) {
        this.image = _image;
    }
}
enter code here

// Dragon class derived from the Cow class

public class Dragon extends Cow {
    // Constructor to create a new Dragon object with parameters name and image
    public Dragon (String name, String image) {
        super(name);
        setImage(image);
    }

    // Function to return true for the default Dragon type
    public boolean canBreatheFire() {
        return true;
    }
}



// IceDragon class derived from the Dragon class

public class IceDragon extends Dragon {
    // Constructor to create a new IceDragon object with parameters name and image
    public IceDragon (String name, String image) {
        super(name, image);
    }

    // Function to return false for the IceDragon type
    public boolean canBreatheFire() {
        return false;
    }
}
  


import java.lang.reflect.Constructor;

public class HeiferGenerator
{
    public static Cow[] getCows()
    {
        if (cows == null)
        {
            cows = new Cow[cowNames.length + dragonNames.length];

            // Add the "regular" cows
            for (int index = 0; index < cowNames.length; index++)
            {
                cows[index] = new Cow(cowNames[index]);
                cows[index].setImage(quoteLines + cowImages[index]);
            }

            // Add the dragons
            for (int offset = cowNames.length, index = 0; index < dragonNames.length; index++)
            {
                try
                {
                    @SuppressWarnings("unchecked")
                    Constructor<Dragon> constructor = dragonTypes[index].getConstructor(String.class, String.class);
                    cows[offset + index] = constructor.newInstance(dragonNames[index], quoteLines + dragonImage);
                }
                catch (Exception ignored) { }
            }
        }

        return cows;
    }

    // Hard-coded values for some of the cows
    private static final String[] cowNames = { "heifer", "kitteh" };

    private static final String quoteLines =        "       \\\n" +
            "        \\\n" +
            "         \\\n";

    private static final String[] cowImages = { "        ^__^\n" +
            "        (oo)\\_______\n" +
            "        (__)\\       )\\/\\\n" +
            "            ||----w |\n" +
            "            ||     ||\n",


            "       (\"`-'  '-/\") .___..--' ' \"`-._\n" +
                    "         ` *_ *  )    `-.   (      ) .`-.__. `)\n" +
                    "         (_Y_.) ' ._   )   `._` ;  `` -. .-'\n" +
                    "      _.. `--'_..-_/   /--' _ .' ,4\n" +
                    "   ( i l ),-''  ( l i),'  ( ( ! .-'\n"
    };

    private static final  String[] dragonNames = { "dragon", "ice-dragon" };
    private static final Class[] dragonTypes = {Dragon.class, Dragon.class};


    private static final String dragonImage =     "           |\\___/|       /\\  //|\\\\\n" +
            "           /0  0  \\__   /  \\// | \\ \\\n" +
            "          /     /  \\/_ /   //  |  \\  \\\n" +
            "          \\_^_\\'/   \\/_   //   |   \\   \\\n" +
            "          //_^_/     \\/_ //    |    \\    \\\n" +
            "       ( //) |        \\ //     |     \\     \\\n" +
            "     ( / /) _|_ /   )   //     |      \\     _\\\n" +
            "   ( // /) '/,_ _ _/  ( ; -.   |    _ _\\.-~       .-~~~^-.\n" +
            " (( / / )) ,-{        _      `.|.-~-.          .~         `.\n" +
            "(( // / ))  '/\\      /                ~-. _.-~      .-~^-.  \\\n" +
            "(( /// ))      `.   {            }                 /      \\  \\\n" +
            " (( / ))     .----~-.\\        \\-'               .~         \\  `.   __\n" +
            "            ///.----..>        \\            _ -~            `.  ^-`  \\\n" +
            "              ///-._ _ _ _ _ _ _}^ - - - - ~                   `-----'\n";

      private static Cow[] cows = null;

  }
2 Answers

It is not allowed to instantiate an array of generic types (unless the type parameter is an unbound wildcard, for example List<?>. This article has explanation.

As you have to use arrays. You can use:

private static final Class<?>[] dragonTypes = {Dragon.class, IceDragon.class};

As for the error, cast the return object to Constructor<Dragon> constructor:

@SuppressWarnings("unchecked")
Constructor<Dragon> constructor =(Constructor<Dragon>) dragonTypes[index].getConstructor(String.class, String.class);

You have already suppressed the warning.

TL;DR: Do you HAVE to use arrays? If not, use lists

Replace this:

Constructor<Dragon> constructor = dragonTypes[index].getConstructor(String.class, String.class);

With this:

Constructor<Dragon> constructor = dragonTypes.get(index).getConstructor(String.class, String.class);


And this:

private static final Class[] dragonTypes = {Dragon.class, Dragon.class};

With this:

private static final List<Class<Dragon>> dragonTypes = Arrays.asList(Dragon.class, Dragon.class);

Related