2

我正在将我的 java 代码库从 java7 迁移到 java8。在此过程中,我还将从 javax.annotation @Nullable、@NonNull 和 @ParametersAreNotNullByDefault 注释切换到 org.eclipse.jdt 注释,以便在 Eclipse 中进行空值分析(Mars 版本 4.5.0:Build id:20150621-1200)。在这样做时,我偶然发现了一种我无法编译的情况(因为关于基于注释的空检查的严格 Eclipse 设置),因为我无法解释。我不是在寻找一种方法来编译我的代码,而是更多地了解错误发生的原因。

我在一个包中有以下类,指定@NonNullByDefaultpackage-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找到

4

1 回答 1

1

显然,此错误已通过针对 JDT-core修复错误436091所引入的更改得到解决。此修复程序应存在于 eclipse Mars.2 (4.5.2) 和 eclipse neon-M4 (4.6.0-M4) 中。

于 2016-01-18T19:29:31.077 回答