编译警告:未选中调用XXX作为原始类型

时间:2015-06-13 07:23:29

标签: java generics compiler-warnings unchecked

我收到编译器警告:

  

警告:[未选中]未选中调用setView(V)作为原始类型AbstractPresenter的成员

   this.presenter.setView(this);
     

其中V是一个类型变量:

     

V扩展了AbstractPresenter类

中声明的AbstractView

AbstractPresenter类的代码如下:

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;
    }
}

setView类在下面的AbstractView类中调用:

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;
    }
}

我已经搜索了其他成员关于同一警告的问题,并试图使解决方案适应我的问题,但它没有用。

我不明白为什么会引发警告,因为在V类的声明中强制AbstractPresenter类型:

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

这只是一个警告,我可以忽略它,但我想了解它为什么会发生,我想让我的代码尽可能干净。

3 个答案:

答案 0 :(得分:9)

你的类型是原始的 - 也就是说,你的泛型类型绑定到一个本身有类型的类型,但你还没有提供类型,所以它是原始的。

更改要键入的类型范围。试试这个:

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

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

答案 1 :(得分:7)

你的问题在于这一行:

created_at

您的public abstract class AbstractView<P extends AbstractPresenter> extends 被声明为扩展原始 P的类型。基本上,我们不知道该类型的AbstractPresenterV是什么。

因此M属于此原始类型,我们不知道其this.presenterV。因此,当您使用M调用其setView时,编译器无法判断类型是否正确。

也是如此
this

public abstract class AbstractPresenter<V extends AbstractView, M> 是一种扩展 raw V的类型,我们不知道它的基本类型是什么。因此,编译器无法完成泛型的工作。

每当您进行此类型声明时,请记住在声明中指定所有泛型类型的类型,并使用正确表示它们之间关系的类型变量。

答案 2 :(得分:2)

我本来想添加一条评论但不能,因为我没有足够的声誉。

你的类型是原始的。 AbstractView中的演示者是原始类型,因为传递给Abstract View的泛型参数是原始的