Java 类是原始类型。对泛型类型 Class<T> 的引用应该被参数化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1372595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:17:04  来源:igfitidea点击:

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

javagenericswarningssuppress-warningstype-erasure

提问by

I have the following class (from a simple Spring tutorial)

我有以下课程(来自一个简单的 Spring 教程)

public class CarValidator implements Validator {

    public boolean supports(Class aClass) {
        return Car.class.equals(aClass);
    }

    public void validate(Object obj, Errors errors) {
        Car car = (Car) obj;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "model", "field.required", "Required field");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "price", "field.required", "Required field");

        if( ! errors.hasFieldErrors("price")) {
            if (car.getPrice().intValue() == 0) {
                errors.rejectValue("price", "not_zero", "Can't be free!");
            }
        }

    }
}

Where the Validator class is the org.springframework.validation.Validatorclass from Spring 2.5.

其中 Validator 类是org.springframework.validation.ValidatorSpring 2.5 中的类。

The supports method is showing a warning (Class is a raw type. References to generic type Class should be parameterized), if I try to add parameters to this such as

如果我尝试向其中添加参数,则支持方法显示警告(类是原始类型。对泛型类型类的引用应参数化)

public boolean supports(Class<?> aClass) ...

I get the following error:

我收到以下错误:

The method supports(Class<?>) of type CarValidator has the same erasure as supports(Class) of type Validator but does not override it

The method supports(Class<?>) of type CarValidator has the same erasure as supports(Class) of type Validator but does not override it

There are lots of threads about this type of question, but I want to get a complete answer and actually understand it without 'hiding' the problem with a @SupressWarnings!

关于这种类型的问题有很多线索,但我想得到一个完整的答案并真正理解它而不用“隐藏”问题@SupressWarnings

采纳答案by KLE

The interface declares the method with a raw type. In that case, you can't override it nicely without having the warning.

接口使用原始类型声明方法。在这种情况下,您不能在没有警告的情况下很好地覆盖它。

The origin of your problem is that the Spring interface was declared to be Java 1.4 compliant. Note that Spring 3.0 is supposed to deliver all classes as Java 1.5 compliant, so that would fix your problem. Before you upgrade, I guess you would have to live with either the warning or the @SuppressWarning.

您的问题的根源是 Spring 接口被声明为符合 Java 1.4。请注意,Spring 3.0 应该以符合 Java 1.5 的方式提供所有类,这样可以解决您的问题。在升级之前,我想您将不得不忍受警告或@SuppressWarning.

回答by Joachim Sauer

Since the interface forces you to use the raw type (i.e. doesn't allow you to specify the correct type information) you can't implement it without warnings unless you use @SupressWarnings.

由于接口强制您使用原始类型(即不允许您指定正确的类型信息),除非您使用@SupressWarnings.

The only real fix is to fix the interface (i.e. make it define boolean supports(Class<?> aClass)).

唯一真正的修复是修复接口(即让它定义boolean supports(Class<?> aClass))。