【问题标题】:Compilation warning: Unchecked call to XXX as member of the raw type编译警告:未经检查地调用 XXX 作为原始类型的成员
【发布时间】:2015-08-29 05:39:18
【问题描述】:

我收到编译器警告:

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

   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 类的声明中强制使用 V 类型时引发警告:

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

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

【问题讨论】:

  • 代码中的循环引用让我很头疼。为什么view需要有presenter,为什么presenter又需要有view?您不能将其转换为单向关系吗?
  • @Chetan Kniger 和 Kervin 谢谢两位,现在我知道什么是原始类型,并且确实我有一个循环引用,如果我在 AbstractPresenter 类中参数化 V 的声明,它将转换为循环继承。 ..所以我需要回到设计部分。我认为 View 不需要在这里引用 Presenter。

标签: java generics compiler-warnings unchecked


【解决方案1】:

您的类型是原始的 - 也就是说,您的泛型类型绑定到本身具有类型但您没有提供类型的类型,因此它是原始的。

更改要键入的类型界限。试试这个:

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

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

【讨论】:

  • 我无法检查几个答案,所以我检查了一个,但所有贡献都帮助我解决了我的问题,所以感谢这个线程中的所有人。
【解决方案2】:

你的问题在于这一行:

public abstract class AbstractView<P extends AbstractPresenter> extends

您的P 被声明为扩展原始 AbstractPresenter 的类型。基本上,我们不知道那个类型的VM是什么。

因此this.presenter 属于这种原始类型,我们不知道它的VM。因此,当你用this 调用它的setView 时,编译器无法判断类型是否正确。

public abstract class AbstractPresenter<V extends AbstractView, M> 

V 是一个扩展 raw AbstractView 的类型,我们不知道它的基本类型是什么。因此,编译器无法完成泛型所指的工作。

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

【讨论】:

    【解决方案3】:

    我本来想添加评论,但因为我没有足够的声誉而无法添加。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 2016-10-22
      • 2021-04-17
      • 1970-01-01
      相关资源
      最近更新 更多