I am not sure how to word the title of this question in a concise way. There are a few related questions I have found, for instance this one, but none of them seem to answer the question I have explicitly.
But essentially what I am asking is this:
Consider the following code
static <A, B> Class<? extends A> getLeftClass(Pair<A, B> tuple) {
A left = tuple.getLeft();
return left.getClass();
}
As is, this code does not compile. The compilation fails with the error
Type mismatch: cannot convert from Class<capture#20-of ? extends Object> to Class<? extends A>
I think this is essentially because getClass
returns a type Class<? extends Object>
but the compiler is expecting Class<? extends A>
.
A solution is to cast the class as follows:
static <A, B> Class<? extends A> getLeftClass(Pair<A, B> tuple) {
A left = tuple.getLeft();
return (Class<? extends A>) left.getClass();
}
This compiles. However, there is a warning because of an unchecked cast.
My question is, is the warning justified? Is there a legitimate reason not to do this? Or is this just a case where the compiler can't verify that it is correct, but it will always work as long as tuple.getLeft()
is indeed an instance of A
?
By the way, the Pair
class is this one, from the Apache commons library