I have an interface defining the following method:
public <C extends RTSpan<V>, V extends Object> void onEffectSelected(Effect<V, C> effect, V value);
I'm calling this like this:
mListener.onEffectSelected(Effects.BOLD, true);
with Effects.BOLD
being an Effect<Boolean,RTSpan<Boolean>>
object.
I was under the impression that the compiler should make sure that the two Vs in the onEffectSelected
are of the same type?
However if I write:
mListener.onEffectSelected(Effects.BOLD, 1);
It compiles without issues and of course throws a runtime exception.
What's wrong here? How can I make sure that the second parameter has an identical type to the one used in the first parameter?
EDIT
public class GenericsTest {
interface RTSpan<V> {}
static class BoldSpan implements RTSpan<Boolean> {}
static class Effect<V extends Object, C extends RTSpan<V>> {}
static class BoldEffect extends Effect<Boolean, BoldSpan> {}
interface TheListener {
public <C extends RTSpan<V>, V extends Object> void onEffectSelected(Effect<V, C> effect, V value);
}
static class Effects {
public static final Effect BOLD = new BoldEffect();
}
public static void main(String[] args) {
TheListener listener = new TheListener() {
@Override
public <C extends RTSpan<V>, V> void onEffectSelected(Effect<V, C> effect, V value) {}
};
listener.onEffectSelected(Effects.BOLD, Boolean.TRUE);
listener.onEffectSelected(Effects.BOLD, 1); // this compiles
listener.onEffectSelected(new BoldEffect(), 1); // this doesn't compile
}
}
The issue seems to be Effects.BOLD. If I replace it with a new BoldEffect() the compiler complains. Does anyone know why the static instantiation throws off the compiler?