类是原始类型。对泛型类型Class的引用应该参数化
我有以下类(来自简单的 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!");
}
}
}
}
其中 Validator 类是 Spring 2.5 中的 org.springframework.validation.Validator 类。
support 方法显示警告(Class 是原始类型。对泛型类型 Class 的引用应该参数化),如果我尝试向此添加参数,例如
public boolean supports(Class<?> aClass) ...
出现以下错误:
The methodsupports(Class) 类型的 CarValidator 与 Validator 类型的supports(Class) 具有相同的擦除功能,但不会覆盖它
关于此类问题有很多线程,但我想得到完整的答案并真正理解它无需使用 @SupressWarnings
来“隐藏”问题!
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
!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该接口使用原始类型声明该方法。在这种情况下,您无法在没有警告的情况下很好地覆盖它。
问题的根源在于 Spring 接口被声明为兼容 Java 1.4。
请注意,Spring 3.0 应该将所有类提供为兼容 Java 1.5,这样就可以解决您的问题。在升级之前,我想您将不得不忍受警告或@SuppressWarning。
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
.由于该接口强制您使用原始类型(即不允许您指定正确的类型信息),因此您无法在没有警告的情况下实现它,除非您使用
@SupressWarnings
。唯一真正的修复是修复接口(即使其定义booleansupports(ClassaClass))。
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)
).