Java: Removing the "Comparable is Raw Type" Warning

Suppose I have a method called foo that takes 2 objects as a parameter. Both objects are of the same type and both implement a 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 two warnings:

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

Last warning:

Security Type: The compareTo (Object) method refers to the raw type Comparable. References to the generic parameterized type

How can I reorganize my code to remove these warnings?

EDIT: Can I do this without changing the signature of the foo method?

+7
source share
5 answers

You must tell the compiler that they are of the same type and comparable. If you cannot 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. } 
+13
source

Without changing the 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 have:
Type safety: Unchecked cast from Object to Comparable<Object>

but not Comparable is a raw type. References to generic type Comparable<T> should be parameterized 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 Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized

+3
source

You need to use Comparable<Type> where Type is an object that implements Comparable .

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

+1
source

EDIT: Since you said you cannot change the method signature, then you really cannot leave without unsafe (for the compiler) and @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); } 
+1
source

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 } 
0
source

All Articles