Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: removing "Comparable is a raw type" warning

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?

like image 656
Heisenbug Avatar asked Aug 10 '11 09:08

Heisenbug


4 Answers

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.
}
like image 53
Peter Lawrey Avatar answered Sep 21 '22 03:09

Peter Lawrey


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

like image 43
oliholz Avatar answered Sep 19 '22 03:09

oliholz


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.

like image 1
Nivas Avatar answered Sep 19 '22 03:09

Nivas


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);
}
like image 1
Alistair A. Israel Avatar answered Sep 19 '22 03:09

Alistair A. Israel