I know there are tons of similar questions in SO, but I'm afraid I can't find an analog case to mine. Please forgive me if there is one (and I would love a link :).
I have these two base classes, with self-bounded generics:
public interface View<V extends View<V, P>, P extends Presenter<V, P>> {
P getPresenter();
}
public abstract class Presenter<V extends View<V, P>, P extends Presenter<V, P>> {
protected V view;
public void takeView(V view) { this.view = view; }
}
The reason behind this is that every presenter know the exact class of its view and viceversa, so they can communicate invoking methods in each other without any fuss. (Each presenter will define its own interface for their views to implement, so the architecture is cleaner, but you know what I mean...)
If I implement them, there is no problem:
public class FooView implements View<FooView, FooPresenter> {
@Override
public FooPresenter getPresenter() {
FooPresenter p = new FooPresenter();
p.takeView(this); // Nice and clean!
return p;
}
}
public class FooPresenter extends Presenter<FooView, FooPresenter> {}
Then, I want to create an abstract base class for some kind of views:
public abstract class BaseView<V extends BaseView<V,P>, P extends Presenter<V,P>> extends SomeOtherBaseClass implements View<V, P>
But I need to do an unchecked cast when linking presenter and view!!!
@Override
public P getPresenter() {
P p = createPresenter(); // Another abstract, so every view can have its own presenter
p.takeView((V) this); // Doesn't compile without casting, the cast is marked as unchecked
return p;
}
As a funny aside, if I don't call takeView in BaseView, and do it in every concrete implementation, it works again...
public class BarView implements BaseView<BarView, FooPresenter> {
@Override
public FooPresenter getPresenter() {
FooPresenter p = createPresenter();
p.takeView(this); // javac likes this :/
return p;
}
}
Is there any way of avoiding the unchecked cast? I reckon is a limit in Java generics, but I may be the limited one. :D
Thanks a lot.