最终类型参数的 @SuppressWarnings

发布于 2024-09-27 13:25:50 字数 1000 浏览 0 评论 0原文

在某个地方,我有一个带有通用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞觅青森が 2024-10-04 13:25:50

您的代码无法编译,但这里有类似的内容,我认为这就是您想要的:

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;}
}

看到您没有选择在这里说 extends String ,我想说这是一个错误在日食中。此外,Eclipse 通常可以建议适当的 SuppressWarnings,但这里没有。 (另一个错误?)

您可以做的是将返回和参数类型更改为 String,然后抑制它导致的(不相关的)类型安全警告:

// no warnings
class D implements B<String>{
    @SuppressWarnings("unchecked")
    public String method(String p){return p;}
}

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;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文