eclipse-jdt / eclipse-jdt/eclipse.jdt.core
Null Analysis not triggered correctly in child class when Java Compliance set to 1.7
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 47
Description
When a Java project uses Null Analysis and the its Java Compliance is set to 1.7, a change in a class does not trigger the Null Analysis correctly in a child class, making the Null Analysis incorrect.
Steps to reproduce:
- Open a fresh instance of Eclipse 2023-06
- Create a Java project
- create a package, for example `com.mycompany`
- create a Foo.java file in this package with this content:
```
package com.mycompany;
import org.eclipse.jdt.annotation.Nullable;
public class Foo {
@Nullable
protected String text = "";
public final void toto() {
if (this.text != null) {
System.out.println("foo" + text.length());
}
}
}
```
- create a Bar.java file in this package with this content:
```
package com.mycompany;
public class Bar extends Foo {
public final void titi() {
if (this.text != null) {
System.out.println("bar");
}
}
}
```
- create a package-info.java file in this package with this content:
```
@org.eclipse.jdt.annotation.NonNullByDefault
package com.mycompany;
```
- get the [org.eclipse.jdt.annotation-2.2.700.jar](https://repo1.maven.org/maven2/org/eclipse/jdt/org.eclipse.jdt.annotation/2.2.700/org.eclipse.jdt.annotation-2.2.700.jar), copy it in the project and add it to the build path
- Open the project Properties
- Go to `Java Compiler > Errors/Warnings`
- Click on `Enable project specific settings`
- Expand the `Null Analysis` item
- Check everything and change everything to `Error`
- Still in the project Properties, go to `Java Compiler`
- Change the Compliance to `1.7`
- Open the Foo.bar file and the Bar.java file -> everything is ok, no error
- In the Foo class, remove the `@Nullable` annotation and save -> errors are raised in the Foo and Bar classes (`Redundant null check: The field text is specified as @NonNull`), as expected
- Add the annotation back in the Foo class, the error is removed in the Foo class but not in the Bar class
The error is removed in the Bar class after a rebuild of the project.
This problem does not happen if the Java Compliance version is set to 1.8 at least.
Contributor guide
Assessment
This issue has not been assessed yet.