我正在将我的 java 代码库从 java7 迁移到 java8。在此过程中,我还将从 javax.annotation @Nullable、@NonNull 和 @ParametersAreNotNullByDefault 注释切换到 org.eclipse.jdt 注释,以便在 Eclipse 中进行空值分析(Mars 版本 4.5.0:Build id:20150621-1200)。在这样做时,我偶然发现了一种我无法编译的情况(因为关于基于注释的空检查的严格 Eclipse 设置),因为我无法解释。我不是在寻找一种方法来编译我的代码,而是更多地了解错误发生的原因。
我在一个包中有以下类,指定@NonNullByDefault
在package-info.java
.
我有一个由抽象类实现的接口,该抽象类又由一个具体类扩展,如下所示:
public interface SimulationComponent {
<T extends SimulationComponent> List<T> getCorrectSimulationSubComponents();
List<? extends SimulationComponent> getErroneousSimulationSubComponents();
}
public abstract class AbstractSimulationComponent
implements SimulationComponent {
@Override
public List<SimulationComponent> getCorrectSimulationSubComponents() {
return Collections.emptyList();
}
@Override
public List<SimulationComponent> getErroneousSimulationSubComponents() {
return Collections.emptyList();
}
}
public class ConcreteSubSimComponent extends AbstractSimulationComponent {
public void doSomething() {
}
}
Eclipse 在 ConcreteSubSimComponent 中通知我以下问题:
The method @NonNull List<@NonNull SimulationComponent> getErroneousSimulationSubComponents() from
AbstractSimulationComponent cannot implement the corresponding method from SimulationComponent due
to incompatible nullness constraints
这个问题似乎是由getErroneousSimulationSubComponents()
. 这就是我如何指定导致我在迁移到 java8 时注意到问题的方法。我发现只需将此方法签名替换为getCorrectSimulationSubComponents()
. 我不明白为什么最后一个版本有效而以前的版本无效。
此外,这似乎只是具体子类中的问题。直接实现接口的具体类不会显示任何问题。
我正在使用 JavaSE-1.8 和一个带有无法编译代码的示例项目,可以在https://github.com/KrisC369/NullProblemIllustration找到