类是一种原始类型。对泛型类型的引用< T>应该参数化 [英] Class is a raw type. References to generic type Class<T> should be parameterized
问题描述
我有以下类(从一个简单的Spring教程)
$ b $ pre $ 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);如果(car.getPrice()。intValue()== 0){
errors.rejectValue(b.b
$ b if(!errors.hasFieldErrors(price))价格,not_zero,不能免费!);
}
}
}
}
其中Validator类是来自Spring 2.5的 org.springframework.validation.Validator
类。
支持方法显示一个警告(类是一个原始类型,泛型类的引用应该被参数化),如果我尝试添加参数到这个例如
public boolean supports(Class<?> aClass)...
我得到以下错误:
$ b
类型CarValidator的方法支持(Class <?>)与支持(Class)类型Validator,但不覆盖它
关于这种类型的问题有很多线索,但我想要得到一个完整的答案,并且实际上理解它没有用 @SupressWarnings
来隐藏问题!
接口用一个raw声明了该方法类型。在这种情况下,你不能很好地覆盖它,而不会有警告。
问题的根源在于Spring接口被声明为符合Java 1.4。
请注意,Spring 3.0应该将所有类作为Java 1.5兼容,以便解决您的问题。在升级之前,我想你必须忍受警告或 @SuppressWarning
。
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
!
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
.
这篇关于类是一种原始类型。对泛型类型的引用< T>应该参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!