Compilation warning: Unchecked call to XXX as member of the raw type

jcollin.be picture jcollin.be · Jun 13, 2015 · Viewed 28.3k times · Source

I am getting the compiler warning:

warning: [unchecked] unchecked call to setView(V) as a member of the raw type AbstractPresenter

   this.presenter.setView(this);

where V is a type-variable:

V extends AbstractView declared in class AbstractPresenter

The code of the AbstractPresenter class is the following:

public abstract class AbstractPresenter<V extends AbstractView, M> 
implements Presenter<V, M> {

    private M model;
    private V view;

    @Override
    public final V getView() {
        return this.view;
    }

    public final void setView(V view) {
        if (view == null) {
            throw new NullPointerException("view cannot be null.");
        }

        if (this.view != null) {
            throw new IllegalStateException("View has already been set.");
        }
        this.view = view;
    }

    @Override
    public final M getModel() {
        return this.model;
    }

    protected final void setModel(M model) {
        if (model == null) {
            throw new NullPointerException("model cannot be null.");
        }        
        this.model = model;
    }
}

The setView method is called in the AbstractView class below:

public abstract class AbstractView<P extends AbstractPresenter> extends 
UserControl {
    private final P presenter;

    public AbstractView(P presenter) {
        this.presenter = presenter;
        this.initialisePresenter();
    }

    private void initialisePresenter() {
        if (this.presenter == null){
            throw new IllegalStateException();
        }

        this.presenter.setView(this); //This is the call that raises the warning
    }

    protected P getPresenter() {
        return this.presenter;
    }
}

I have searched the questions from other members regarding the same warning and tried to adapt the solutions to my issue but it did not work.

I don't understand why the warning is raised as the V type is forced in the declaration of the AbstractPresenter class:

public abstract class AbstractPresenter<V extends AbstractView, M> 
implements Presenter<V, M> 

It is just a warning and I could ignore it but I would like to understand why it happens and I want to get my code as clean as possible.

Answer

Bohemian picture Bohemian · Jun 13, 2015

Your types are raw - that is, your generic types are bonded to a type that itself has a type, but you haven't provided one, so it's raw.

Change your type bounds to be typed. Try this:

public abstract class AbstractPresenter<V extends AbstractView<V>, M> implements Presenter<V, M>

and

public abstract class AbstractView<P extends AbstractPresenter<P> extends UserControl