I have a bit of a problem understanding Java's type erasure when it comes to bounded types. Consider this:
class Event {} // From the API
class FooEvent extends Event {}
abstract class Foo<EventType extends Event> {
public abstract <E extends EventType> void onEventCaught(E event);
}
class Bar extends Foo<FooEvent> {
@Override
public void onEventCaught(FooEvent event) {
}
}
Apparently this compiles without problems. The question that I ask myself is, for which parameter-types is Bar#onEventCaught()
declared, here (as in, what does reflection think)?
Is it onEventCaught(FooEvent event)
or maybe onEventCaught(Event event)
?
From the Java Language Specification
You have
and
The leftmost bound of
E
isEventType
, which is another type variable, whose leftmost bound isEvent
. So the erasure ofE
inis
Event
.Type variables do appear in
.class
files, and you can use them in reflection.prints