Search code examples
javagenericsgwtp

Generics - Inheriting class modifies a Generic Type and causes a compiler error


I have the following abstract class:

public abstract class PresenterWithBreadCrumb<V extends View> extends PresenterWidget<V> {
...

What I want to do is extend this class and modify the type V. There is a method that I need in addition to what is provided by the View interface. The new instance is below.

public abstract class ApplicationPanelPresenter<V extends ApplicationPanelPresenter.ApplicationPanelView>
        extends PresenterWithBreadCrumb<ApplicationPanelPresenter.ApplicationPanelView> {

    public interface ApplicationPanelView extends View {
        void clearPanel();
    }

When I try to refactor my code, and change the classes that were originally extending PresenterWithBreadCrumb to ApplicationPanelPresenter I'm introducing a compile error.

Sample before:

public class RequirementsPanelPresenter extends PresenterWithBreadCrumb<RequirementsPanelPresenter.MyView>
        implements RequirementsPanelUiHandlers {

    interface MyView extends View, HasUiHandlers<RequirementsPanelUiHandlers> {
    }

    @Inject
    RequirementsPanelPresenter(EventBus eventBus, MyView view) {
        super(eventBus, view);
        getView().setUiHandlers(this);
    }

Sample After:

public class RequirementsPanelPresenter extends ApplicationPanelPresenter<RequirementsPanelPresenter.MyView>
        implements RequirementsPanelUiHandlers {

    interface MyView extends ApplicationPanelPresenter.ApplicationPanelView, HasUiHandlers<RequirementsPanelUiHandlers> {
    }

    @Inject
    RequirementsPanelPresenter(EventBus eventBus, MyView view) {
        super(eventBus, view);
        getView().setUiHandlers(this);
    }

The compile error is on the statement getView().setUiHandlers(this);

The compile error is:

The method setUiHandlers(RequirementsPanelPresenter) is undefined for the type ApplicationPanelPresenter.ApplicationPanelView

Why is the compiler interpreting "this" as ApplicationPanelPresenter.ApplicationPanelView? How did my change introduce this error and how can I fix it?

Additional Context

  • The getView() method is defined in a parent class and returns a type V extends View.

  • The setUiHandlers method comes from extended interface HasUiHandlers. The method parameter is type C extends UiHandlers. The interface that RequirementsPanelPresenter is implementing, RequirementsPanelUiHandler, extends UiHandlers.


Solution

  • At a glance, I'd expect it to be

    public abstract class ApplicationPanelPresenter<
            V extends ApplicationPanelPresenter.ApplicationPanelView> 
        extends PresenterWithBreadCrumb<V> {
    

    Your code is too complex for me to tell at a glance if that'll fix it, though.