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?
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.
}
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
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With