我正在将我的Java代码库从java7迁移到java8的项目。在此过程中,我还从javax.annotation @ Nullable,@ NonNull和@ParametersAreNotNullByDefault注释切换到org.eclipse.jdt注释,以进行Eclipse中的空分析(Mars版本4.5.0:内部版本号:20150621-1200)。在执行此操作时,我偶然发现了由于某种我无法解释的原因而无法编译的情况(由于对基于注释的null检查进行了严格的日食设置)。我不是在寻找一种方法来编译我的代码,而是更多地了解为什么会发生错误。
我有一个包下面的类指定使用默认的非NULL的含量@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中找到
显然,此错误已通过为针对JDT-core修复错误436091而引入的更改得以解决。Eclipse Mars.2(4.5.2)和Eclipse Neon-M4(4.6.0-M4)中应该存在此修复程序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句