Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse warning - Class is a raw type. References to generic type Class<T> should be parameterized

Tags:

java

generics

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?

like image 286
CodeBlue Avatar asked Aug 09 '12 13:08

CodeBlue


2 Answers

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);
}
like image 136
Peter Lawrey Avatar answered Sep 28 '22 04:09

Peter Lawrey


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.

like image 38
Edwin Dalorzo Avatar answered Sep 28 '22 04:09

Edwin Dalorzo