I'm using generics to abstract out Comparable data types, as illustrated in the code supplied below. This case arose from a Java Swing component, specifically from attempting to adapt table model to use generics.
I have a working solution to that problem (Case 3A below). However, during the process of arriving at that solution I became confused by the behavior of the compiler when I changed the class signature from T extends Comparable<T> to T extends Comparable<? super T>.
Why does the compiler want Comparable to be a raw type (Case 3A below)? I expected the class signature to work (Case 6A, Class<T extends Comparable<? super T>>), but it requires a cast and causes a compiler error when using if (type == Boolean.class).
Why do cases 2B, 3A/B, and 4A/B allow if (type == Boolean.class) but cases 1A/B, 2A, 5A/B and 6A/B cause a compiler error? Specifically, I'd like an explanation of this error in the case of 2A vs. 2B, which share the same class signature.
The runnable test cases are below. The external Score and DetailedScore classes are supplied to demonstrate a case involving inheritance, which highlights the difference between Case A and Case B.
ComparableTest.java
public class ComparableTest<L, T extends Comparable<? super T>> // Case A
//public class ComparableTest<L, T extends Comparable<T>> // Case B: Works when used without inheritance.
{
public static void main(String[] args)
{
new ComparableTest<String, Boolean>(new String("B"), Boolean.TRUE);
new ComparableTest<String, Float>(new String("F"), new Float(1f));
new ComparableTest<String, Score>(new String("S"), new Score(5f));
new ComparableTest<String, Score>(new String("D"), new DetailedScore<String>("DS.S", 5f));
new ComparableTest<String, DetailedScore<?>>(new String("DS"), new DetailedScore<String>("DS.DS", 5f));
}
public ComparableTest(L label, T value)
{
// Case 1A: Compiler Error: Type mismatch: cannot convert from Class<capture#2-of ? extends Comparable> to Class<T>
// Class<T> type = value.getClass(); // Q: Why can't I use Class<T>?
// Case 2A: Compiler Warning: Type safety: Unchecked cast from Class<capture#2-of ? extends Comparable> to Class<T>
// Class<T> type = (Class<T>) value.getClass(); // Case 2B: This works if 'T extends Comparable<T>' (see note in class declaration above).
// Case 3A: Compiler Warning: Comparable is a raw type. References to generic type Comparable<T> should be parameterized
Class<? extends Comparable> type = value.getClass(); // Q: Why must Comparable be a raw type here?
// Case 4A: Compiler Warning: Type safety: Unchecked cast from Class<capture#2-of ? extends Comparable> to Class<? extends Comparable<?>>
// Class<? extends Comparable<?>> type = (Class<? extends Comparable<?>>) value.getClass();
// Case 5A: Compiler Warning: Type safety: Unchecked cast from Class<capture#2-of ? extends Comparable> to Class<? extends Comparable<T>>
// Class<? extends Comparable<T>> type = (Class<? extends Comparable<T>>) value.getClass();
// Case 6A: Compiler Warning: Type safety: Unchecked cast from Class<capture#2-of ? extends Comparable> to Class<? extends Comparable<? super T>>
// Class<? extends Comparable<? super T>> type = (Class<? extends Comparable<? super T>>) value.getClass();
// Case 1A, 2A: Compiler Error: Incompatible operand types Class<T> and Class<Boolean>
// Case 2B, 3A/B, 4A/B: OK.
// Case 5A/B, 6A/B: Compiler Error: Incompatible operand types Class<capture#4-of ? extends Comparable<T>> and Class<Boolean>
if (type == Boolean.class)
{
System.out.println("Treating " + label + " as boolean (" + type.getCanonicalName() + ")");
} else if (type == Float.class) {
System.out.println("Treating " + label + " as float (" + type.getCanonicalName() + ")");
} else {
System.out.println("Treating " + label + " as (" + type.getCanonicalName() + ")");
}
return;
}
}
Score.java
public class Score implements Comparable<Score>
{
private Float value;
public Score(Float value)
{
this.value = value;
return;
}
@Override
public int compareTo(Score o)
{
return this.value.compareTo(o.value); // for brevity
}
}
DetailedScore.java
public class DetailedScore<D> extends Score
{
private D detail;
public DetailedScore(D someDetail, Float value)
{
super(value);
this.detail = someDetail;
return;
}
public D getDetail()
{
return this.detail;
}
}
getClass()returns aClass<?>not aClass<T>... But you can safely cast it. – assylias Jul 8 '15 at 15:10Class<? extends Comparable<? super T>>; however, this still causes a compiler error when using the==operator. – vallismortis Jul 8 '15 at 15:33Java(TM) SE Runtime Environment (build 1.7.0_55-b13),Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)on Windows Server 2003 R2 Standard x64. – vallismortis Jul 8 '15 at 17:57