Java:删除“Comparable 是原始类型”警告

发布于 2024-11-28 23:31:26 字数 713 浏览 1 评论 0原文

假设我有一个名为 foo 的方法,以 2 个对象作为参数。两个对象具有相同的类型并且都实现类似的接口。

void foo(Object first, Object second){

    if (!first.getClass().isInstance(second))   //first and second of the same type
        return;

    Comparable firstComparable = (Comparable)first;  //WARNING
    Comparable secondComparable = (Comparable)second;  //WARNING

    int diff = firstComparable.compareTo(secondComparable);  //WARNING
}

前 2 个警告是:

Comparable 是原始类型。对泛型类型 Comparable 的引用 应该参数化

最后一个警告:

类型安全:compareTo(Object)方法属于原始类型 可比。对泛型类型 Comparable 的引用应该是 参数化

如何重构我的代码以删除这些警告?

编辑: 我可以在不更改 foo 方法的签名的情况下做到这一点吗?

Suppose I have a method called foo taking 2 Object as parameter. Both objects are of the same type and both implements comparable interface.

void foo(Object first, Object second){

    if (!first.getClass().isInstance(second))   //first and second of the same type
        return;

    Comparable firstComparable = (Comparable)first;  //WARNING
    Comparable secondComparable = (Comparable)second;  //WARNING

    int diff = firstComparable.compareTo(secondComparable);  //WARNING
}

The first 2 warning are:

Comparable is a raw type. References to generic type Comparable
should be parameterized

The last warning:

Type safety: The method compareTo(Object) belongs to the raw type
Comparable. References to generic type Comparable should be
parameterized

How could I refactor my code in order to remove these warnings?

EDIT:
Can I do that without changing foo method's signature?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

夏末染殇 2024-12-05 23:31:26

您必须告诉编译器它们是相同类型并且具有可比性。如果您无法更改签名,您可以添加向后兼容的方法。

@SuppressWarnings("unchecked")
static void foo(Object first, Object second) {
    foo((Comparable) first, (Comparable) second);
}

static <T extends Comparable<T>> void foo(T first, T second){
    int diff = first.compareTo(second); // no warning.
}

You have to tell the compiler that they are the same type and comparable. If you can't change the signature you can add a method for backward compatibility.

@SuppressWarnings("unchecked")
static void foo(Object first, Object second) {
    foo((Comparable) first, (Comparable) second);
}

static <T extends Comparable<T>> void foo(T first, T second){
    int diff = first.compareTo(second); // no warning.
}
莫相离 2024-12-05 23:31:26

无需更改签名,您可以

    void foo(Object first, Object second){

        if (!first.getClass().isInstance(second)) 
            return;

        Comparable<Object> firstComparable = (Comparable<Object>)first;  
        Comparable<Object> secondComparable = (Comparable<Object>)second; 

        int diff = firstComparable.compareTo(secondComparable);  
    }

但是您仍然得到:
类型安全:未检查从 Object 到 Comparable的转换,

Comparable 不是原始类型。对泛型类型 Comparable的引用应该参数化
并且没有类型安全性:compareTo(Object) 方法属于原始类型 Comparable。对泛型类型 Comparable的引用应该参数化

Without changeing Signature you can do

    void foo(Object first, Object second){

        if (!first.getClass().isInstance(second)) 
            return;

        Comparable<Object> firstComparable = (Comparable<Object>)first;  
        Comparable<Object> secondComparable = (Comparable<Object>)second; 

        int diff = firstComparable.compareTo(secondComparable);  
    }

But you still got :
Type safety: Unchecked cast from Object to Comparable<Object>

but no Comparable is a raw type. References to generic type Comparable<T> should be parameterized
and no Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized

满地尘埃落定 2024-12-05 23:31:26

你必须使用
Comparable,其中 Type 是实现 Comparable 的对象。

首先,为什么你的方法参数是Objects的实例?如果确定参数类型相同,则应使用特定的类作为参数。如果可以有类的层次结构,则将类放在层次结构中最高的类。使用Object来实现一般功能从来都不是一个好主意。

You have to do use
Comparable<Type> where Type is the object that is implementing Comparable.

First, why are your method parameters instance of Objects? If you are sure the types of parameters are same, you should use the specific class as the parameter. If you can have an hierarchy of classes, have the class highest in the hierarchy. Having Object to acheive general functionality is never a good idea.

泛滥成性 2024-12-05 23:31:26

编辑:既然你说你不能改变方法的签名,那么如果没有不安全的(对编译器)强制转换和@SuppressWarnings,你真的无法逃脱:

@SuppressWarnings("unchecked")
public void foo(final Object first, final Object second) {
    if (!first.getClass().isInstance(second)) // first and second of the
        return;

    Comparable<Object> firstComparable = (Comparable<Object>) first;
    Comparable<Object> secondComparable = (Comparable<Object>) second;
    int diff = firstComparable.compareTo(secondComparable);
}

EDIT: Since you said you can't change the method's signature, then you really can't get away without an unsafe (to the compiler) cast, and a @SuppressWarnings:

@SuppressWarnings("unchecked")
public void foo(final Object first, final Object second) {
    if (!first.getClass().isInstance(second)) // first and second of the
        return;

    Comparable<Object> firstComparable = (Comparable<Object>) first;
    Comparable<Object> secondComparable = (Comparable<Object>) second;
    int diff = firstComparable.compareTo(secondComparable);
}
梅窗月明清似水 2024-12-05 23:31:26

添加@SuppressWarnings注释。

@SuppressWarnings("unchecked")
void foo(Object first, Object second){

    if (!first.getClass().isInstance(second))   //first and second of the same type
        return;

    Comparable firstComparable = (Comparable)first;  //WARNING
    Comparable secondComparable = (Comparable)second;  //WARNING

    @SuppressWarnings("unused")
    int diff = firstComparable.compareTo(secondComparable);  //WARNING
}

Add @SuppressWarnings annotation.

@SuppressWarnings("unchecked")
void foo(Object first, Object second){

    if (!first.getClass().isInstance(second))   //first and second of the same type
        return;

    Comparable firstComparable = (Comparable)first;  //WARNING
    Comparable secondComparable = (Comparable)second;  //WARNING

    @SuppressWarnings("unused")
    int diff = firstComparable.compareTo(secondComparable);  //WARNING
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文