@SuppressWarnings的最终类型参数 [英] @SuppressWarnings for final type parameter
问题描述
在一个地方我有一个通用的VT扩展字符串的方法。显然这会产生一个警告:类型参数VT不应该被最终类型String限制。最终类型无法进一步扩展。
你知道是否有办法抑制这个警告(Eclipse)?如果你想知道我该如何做到这一点:
import java.util.ArrayList;
import java.util.List;
class A< T> {
T值;
B <超级T b;
void method(){
b.method(value,new ArrayList< T>());
}}
接口B< X> {
< VT扩展X> VT方法(VT p,List< VT> lst);
}
//工作正常
class C实现B< Number> {
public< VT extends Number> VT方法(最终VT p,最终列表< VT> lst){
return p;
}}
//引发警告
class D实现B< String> {
public< VT extends String> VT方法(最终VT p,最终列表< VT> lst){
return p;
}}
// error:类型E必须实现继承的抽象方法B< String> .method(VT,List< VT>)
class E实现B&串GT; {
@SuppressWarnings(unchecked)
public String method(final String p,final List< String> lst){
return p;
}}
谢谢!
Cristian
你的代码不编译,但这里有一些类似的东西,我假设你是想要的:
class A< T> {
T value;
B <超级T b;
void method(){
b.method(value);
}
}
接口B< X> {
< VT扩展X> VT方法(VT p);
}
//工作正常
class C实现B< Number> {
public< VT extends Number> VT方法(VT p){return p;}
}
//引起警告
类D实现B< String> {
public< VT extends String> VT方法(VT p){return p;}
}
在这里可以选择说 extends String
,我会说这是Eclipse中的一个错误。此外,Eclipse通常可以建议适当的 SuppressWarnings
,但不在此处。 (另一个错误?)
您可以做的是将返回值和参数类型更改为 String
,然后禁止(不相关)类型安全警告原因:
// no warnings
class D implements B< String> {
@SuppressWarnings(unchecked)
public String method(String p){return p;}
}
Hey guys. In a place I have a method with a generic "VT extends String". Obviously this generates a warning: The type parameter VT should not be bounded by the final type String. Final types cannot be further extended. Do you know if there's a way to suppress this warning (Eclipse)? If you're wondering how I got to have this:
import java.util.ArrayList;
import java.util.List;
class A<T> {
T value;
B<? super T> b;
void method() {
b.method(value,new ArrayList<T>());
}}
interface B<X> {
<VT extends X> VT method(VT p, List<VT> lst);
}
// works fine
class C implements B<Number> {
public <VT extends Number> VT method(final VT p, final List<VT> lst) {
return p;
}}
// causes warning
class D implements B<String> {
public <VT extends String> VT method(final VT p, final List<VT> lst) {
return p;
}}
// error: The type E must implement the inherited abstract method B<String>.method(VT, List<VT>)
class E implements B<String> {
@SuppressWarnings("unchecked")
public String method(final String p, final List<String> lst) {
return p;
}}
Thanks! Cristian
Your code doesn't compile, but here's something similar, which I assume is what you want:
class A<T>{
T value;
B<? super T> b;
void method(){
b.method(value);
}
}
interface B<X>{
<VT extends X> VT method(VT p);
}
// works fine
class C implements B<Number>{
public <VT extends Number> VT method(VT p){return p;}
}
// causes warning
class D implements B<String>{
public <VT extends String> VT method(VT p){return p;}
}
Seeing that you don't have a choice to saying extends String
here, I'd say this is a bug in Eclipse. Furthermore, Eclipse can usually suggest an appropriate SuppressWarnings
, but doesn't here. (Another bug?)
What you can do is change the return and argument type to String
and then suppress the (irrelevant) type safety warning it causes:
// no warnings
class D implements B<String>{
@SuppressWarnings("unchecked")
public String method(String p){return p;}
}
这篇关于@SuppressWarnings的最终类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!