简体   繁体   English

Eclipse警告 - 类是原始类型。 对泛型类的引用 <T> 应该参数化

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

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. 这是在之前的问题中提出的 ,但答案是针对Spring Framework的。 But I am getting this warning even without having anything to do with Spring. 但即使没有与Spring有任何关系,我也会得到这个警告。 So what is the problem? 那么问题是什么?

I suspect its complaining that Class is a raw type. 我怀疑它抱怨Class是原始类型。 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);
}

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. Class也是一个参数化类型,如果你没有声明一个类型参数,那么你会得到一个警告,要求使用期望参数化类型的原始类型。

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. 你没有做得更好,因为除了基元之外,所有东西都扩展了Java中的Object。 I ignore this hints, or turn them of. 我忽略了这些提示,或者转过它们。

暂无
暂无

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

相关问题 如何在Eclipse中禁用警告 - 'Class是原始类型。对泛型类型<T>的引用应该参数化' - How to disable warning in Eclipse - 'Class is a raw type. References to generic type Class<T> should be parameterized' 警告-类是原始类型。 对泛型类型Class的引用 <T> 应该参数化 - Warning - Class is a raw type. References to generic type Class<T> should be parameterized 为什么我收到警告:Class是原始类型。对泛型类型<T>的引用应该参数化“? - Why am I getting the warning :Class is a raw type. References to generic type Class<T> should be parameterized"? 类型类是原始类型。 对泛型类型Class的引用 <T> 应该参数化 - Type Class is a raw type. References to generic type Class<T> should be parameterized 类是原始类型。 对泛型类型Class的引用 <T> 应该参数化 - Class is a raw type. References to generic type Class<T> should be parameterized AddLinkEntry类是原始类型。 对泛型类型AddLinkEntry类的引用 <T> 应该参数化 - AddLinkEntry Class is a raw type. References to generic type AddLinkEntry Class<T> should be parameterized 如何解决此警告? -是原始类型。 泛型类型的引用应参数化 - How to address this warning? - Is a raw type. References to generic type should be parameterized 原始类型。 对泛型类型的引用应该被参数化 - Raw type. References to generic types should be parameterized AdapterView是原始类型。 泛型类型AdapterView的引用 <T> 应该参数化 - AdapterView is a raw type. References to generic type AdapterView<T> should be parameterized 可比是原始类型。 对通用类型Comparable的引用 <T> 应该参数化 - Comparable is a raw type. References to generic type Comparable<T> should be parameterized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM