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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7008623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:14:47  来源:igfitidea点击:

Java: removing "Comparable is a raw type" warning

javawarningscomparableraw-types

提问by Heisenbug

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

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

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

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

The last warning:

最后警告:

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

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

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

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

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

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

回答by Peter Lawrey

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

回答by oliholz

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

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

你仍然得到:
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

但没有Comparable is a raw type. References to generic type Comparable<T> should be parameterized
也没有Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized

回答by Alistair A. Israel

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

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

回答by Nivas

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

您必须使用 Comparable<Type>where Type 是正在实现的对象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 Objectto acheive general functionality is never a good idea.

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

回答by Ondrej Sotolar

Add @SuppressWarnings annotation.

添加@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
}