I am getting a compiler error calling a generic method with explicit type parameters, as if the explicit type parameter had not been taken into account. Minimal example:
class CastExample {
static class ThingProducer<S> {
public <T> T getThing() { return null; }
}
static class ThingA {}
public static void main(String... args) {
ThingProducer thingProducer = new ThingProducer();
ThingA thingA = thingProducer.<ThingA>getThing(); // compile error here
}
}
ThingProducer
is a raw type since the class has a type parameter, but in calling getThing
we are not referencing the class type parameter, but instead providing the method type parameter. Per my understanding of the JLS, this should be legal, but it gives me this error:
incompatible types: Object cannot be converted to ThingA
The error disappears if I
- remove the
<S>
fromThingProducer
- or make
getThing
static - declare
thingProducer ThingProducer<?>
instead of the raw typeThingProducer
Is this a compiler bug? If not, what rule in the JLS defines this behavior?