最终类型参数的 @SuppressWarnings
在某个地方,我有一个带有通用“VT extends String”的方法。显然这会产生一个警告:类型参数 VT 不应受最终类型 String 的限制。最终类型无法进一步扩展。
你知道是否有办法抑制这个警告(Eclipse)?如果你想知道我是如何得到这个的:
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;
}}
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;
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码无法编译,但这里有类似的内容,我认为这就是您想要的:
看到您没有选择在这里说
extends String
,我想说这是一个错误在日食中。此外,Eclipse 通常可以建议适当的SuppressWarnings
,但这里没有。 (另一个错误?)您可以做的是将返回和参数类型更改为 String,然后抑制它导致的(不相关的)类型安全警告:
Your code doesn't compile, but here's something similar, which I assume is what you want:
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 appropriateSuppressWarnings
, 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: