62

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

    }
}

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) ...

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

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!

4

2 回答 2

33

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

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

于 2009-09-03T10:26:36.537 回答
9

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.

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

于 2009-09-03T10:17:03.783 回答