5
import java.util.ArrayList;

public class ListOfClasses
{

    private ArrayList<Class> classes;

    public ArrayList<Class> getClasses() 
    {
        return classes;
    }

    public void setClasses(ArrayList<Class> classes) 
    {
        this.classes = classes;
    }
}

For this, I get the following warning in eclipse -

Class is a raw type. References to generic type Class should be parameterized

This was asked in an earlier question, but the answer was specific to the Spring Framework. But I am getting this warning even without having anything to do with Spring. So what is the problem?

Community
  • 1
  • 1
CodeBlue
  • 13,033
  • 30
  • 82
  • 127

3 Answers3

9

I suspect its complaining that Class is a raw type. You can try

private List<Class<?>> classes;

or suppress this particular warning.

I would ignore the warning in this case. I would also consider using a defensive copy.

private final List<Class> classes = new ArrayList<>();

public List<Class> getClasses() {
    return classes;
}

public void setClasses(List<Class> classes) {
    this.classes.clear();
    this.classes.addAll(classes);
}
JamesD
  • 2,176
  • 19
  • 35
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
1

Try

public class ListOfClasses
{

    private ArrayList<Class<?>> classes;

    public ArrayList<Class<?>> getClasses() 
    {
        return classes;
    }

    public void setClasses(ArrayList<Class<?>> classes) 
    {
        this.classes = classes;
    }
}

Class is a parameterized type as well, and if you do not declare a type argument, then you get a warning for using raw types where parameterized types are expected.

Edwin Dalorzo
  • 70,022
  • 25
  • 131
  • 191
0

The problem is that you can not say anything about the type of returned class. This warning is not very useful. Since you dont knwow which type of class it is, you cannot add type arguments to it. If you do it like this:

public  Class<? extends Object> getClasses {}

You don't make it better, since everything extends Object in Java, except primitives. I ignore this hints, or turn them of.

Stimpson Cat
  • 1,131
  • 13
  • 37