eclipse-jdt / eclipse-jdt/eclipse.jdt.core
Weird interaction with null analysis, jspecify, and external annotations
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 47
Description
External annotations sometimes seem to break/behave strangely. This is pretty vague, but I've managed to pinpoint one specific case.
See attached project, it has the following code:
```
@org.eclipse.jdt.annotation.NonNullByDefault
public class JdtAnnotations {
public static class Reproduce {
public static void main(String[] args) {
var opt = Optional.fromNullable(nullString());
var nullStr = opt.orNull();
var nonNullStr = nonNullString();
nonNullStr = opt.or(nonNullString());
nonNullStr = opt.or("");
System.out.println(nullStr.length());
System.out.println(nonNullStr.length());
}
@org.eclipse.jdt.annotation.Nullable
public static String nullString() {
return null;
}
@org.eclipse.jdt.annotation.NonNull
public static String nonNullString() {
return "";
}
}
}
```
It has jdt external null annotations and null analysis set up and also guavas `Optional.or` method marked as returning `@NonNull` in bundled eea file.
1. Import the project, look at this like:
```
nonNullStr = opt.or("");
```
it compiles without any warnings, jdt correctly inferring that return value is `@NonNull`
2. Go to null analysis preferences (it already has most jspecify annotations set up and add `@org.jspecify.annotations.NullMarked`) as an alternative to `@NonNullByDefault`.
3. Now, the line in question generates warnings. Jdt no longer sees that `Optional.or` returns `@NonNull` value.
Tested on
```
Eclipse IDE for Enterprise Java and Web Developers (includes Incubating components)
Version: 2026-03 (4.39.0)
Build id: 20260305-0817
```
[eclipse_guava_optional_reproducer.zip](https://github.com/user-attachments/files/26055098/eclipse_guava_optional_reproducer.zip)
Contributor guide
Assessment
This issue has not been assessed yet.