类是原始类型.对泛型类型 Class<T> 的引用应该参数化 [英] Class is a raw type. References to generic type Class&lt;T&gt; should be parameterized

查看:48
本文介绍了类是原始类型.对泛型类型 Class<T> 的引用应该参数化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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!");
            }
        }

    }
}

其中 Validator 类是 Spring 2.5 中的 org.springframework.validation.Validator 类.

Where the Validator class is the org.springframework.validation.Validator class from Spring 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) ...

我收到以下错误:

CarValidator 类型的support(Class<?>) 方法与Validator 类型的supports(Class) 具有相同的擦除,但不会覆盖它

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

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!

推荐答案

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

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

问题的根源在于 Spring 接口被声明为符合 Java 1.4.请注意,Spring 3.0 应该以符合 Java 1.5 的方式交付所有类,这样就可以解决您的问题.在升级之前,我猜您必须接受警告或 @SuppressWarning.

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.

这篇关于类是原始类型.对泛型类型 Class<T> 的引用应该参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆