eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Cannot get JSpecify annotations to work like org.eclipse.jdt.annotation specifically with generics

Open
#3,434 12 comments 1 reaction 2 assignees Claimed by @agentgt View on GitHub
null
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

@stephan-herrmann

I cannot replace Eclipses null annotations with JSpecify because generics appear to be not marked.

Here is the project.

https://github.com/agentgt/jdt-null-issue

The project does not work with headless at the moment because there appears to be another bug with plexus eclipse compiler not finding jspecify however it does emit the same warnings as the UI so I will paste its output here:

```
[WARNING] COMPILATION WARNING :
[INFO] -------------------------------------------------------------
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/package-info.java:[2,9] A default nullness annotation has not been specified for the package bug.ezkv
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[25,24] Null constraint mismatch: The type 'E extends java.lang.Enum' is not a valid substitute for the type parameter 'E extends java.lang.@NonNull Enum'
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[27,18] Null constraint mismatch: The type 'E extends java.lang.Enum' is not a valid substitute for the type parameter 'E extends java.lang.@NonNull Enum'
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[36,62] Null constraint mismatch: The type 'E extends java.lang.Enum' is not a valid substitute for the type parameter 'E extends java.lang.@NonNull Enum'
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[47,37] Null constraint mismatch: The type 'E extends java.lang.Enum' is not a valid substitute for the type parameter 'E extends java.lang.@NonNull Enum'
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[55,22] Missing non-null annotation: inherited method from java.lang.Iterable specifies this parameter as @org.jspecify.annotations.NonNull
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[60,34] Missing nullable annotation: inherited method from java.util.Set specifies this parameter as @org.jspecify.annotations.Nullable
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[65,9] The return type is incompatible with 'java.util.@NonNull Iterator>' returned from java.util.Set.iterator() (mismatching null constraints)
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[90,9] The return type is incompatible with 'java.lang.Object @org.jspecify.annotations.NonNull[]' returned from java.util.Set.toArray() (mismatching null constraints)
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[95,27] Missing non-null annotation: inherited method from java.util.Set specifies this parameter as @org.jspecify.annotations.NonNull
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[116,29] Missing non-null annotation: inherited method from java.util.Set specifies this parameter as @org.jspecify.annotations.NonNull
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[121,24] Missing non-null annotation: inherited method from java.util.Set specifies this parameter as @org.jspecify.annotations.NonNull
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[126,27] Missing non-null annotation: inherited method from java.util.Set specifies this parameter as @org.jspecify.annotations.NonNull
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[153,26] Missing non-null annotation: inherited method from java.util.Collection specifies this parameter as @org.jspecify.annotations.NonNull
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[158,9] The return type is incompatible with 'java.util.@NonNull Spliterator>' returned from java.util.Set.spliterator() (mismatching null constraints)
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[163,9] The return type is incompatible with 'java.util.stream.@NonNull Stream>' returned from java.util.Collection.stream() (mismatching null constraints)
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[168,9] The return type is incompatible with 'java.util.stream.@NonNull Stream>' returned from java.util.Collection.parallelStream() (mismatching null constraints)
[WARNING] /Users/agent/projects/jdt-null-issue/src/main/java/bug/package-info.java:[2,9] A default nullness annotation has not been specified for the package bug
[INFO] 18 warnings
[INFO] -------------------------------------------------------------
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/module-info.java:[3,11] org.jspecify cannot be resolved to a module
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/package-info.java:[1,2] The type org.jspecify.annotations.NullMarked is not accessible
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[16,8] The type org.jspecify.annotations.Nullable is not accessible
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[60,25] Nullable cannot be resolved to a type
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[80,27] Nullable cannot be resolved to a type
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[101,14] Nullable cannot be resolved to a type
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[111,25] Nullable cannot be resolved to a type
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[148,14] Nullable cannot be resolved to a type
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/bug/ezkv/FlagSet.java:[232,18] Nullable cannot be resolved to a type
[ERROR] /Users/agent/projects/jdt-null-issue/src/main/java/bug/package-info.java:[1,2] The type org.jspecify.annotations.NullMarked is not accessible
[INFO] 10 errors
```

EDIT here are the errors in the UI

```
Description Resource Path Location Type
Null constraint mismatch: The type 'E extends Enum' is not a valid substitute for the type parameter 'E extends @NonNull Enum' FlagSet.java /jdt-null-issue/src/main/java/bug/ezkv line 25 Java Problem
Null constraint mismatch: The type 'E extends Enum' is not a valid substitute for the type parameter 'E extends @NonNull Enum' FlagSet.java /jdt-null-issue/src/main/java/bug/ezkv line 27 Java Problem
Null constraint mismatch: The type 'E extends Enum' is not a valid substitute for the type parameter 'E extends @NonNull Enum' FlagSet.java /jdt-null-issue/src/main/java/bug/ezkv line 36 Java Problem
Null constraint mismatch: The type 'E extends Enum' is not a valid substitute for the type parameter 'E extends @NonNull Enum' FlagSet.java /jdt-null-issue/src/main/java/bug/ezkv line 47 Java Problem
Null type safety (type annotations): The expression of type 'String' needs unchecked conversion to conform to '@NonNull String' FlagSet.java /jdt-null-issue/src/main/java/bug/ezkv line 208 Java Problem
```

While I have checked in all the eclipse settings I'm going to show the relevant ones below:

```properties
org.eclipse.jdt.core.builder.annotationPath.allLocations=enabled
org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=warning
org.eclipse.jdt.core.compiler.annotation.nonnull=org.jspecify.annotations.NonNull
org.eclipse.jdt.core.compiler.annotation.nonnull.secondary=org.eclipse.jdt.annotation.NonNull
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.jspecify.annotations.NullMarked
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault.secondary=org.eclipse.jdt.annotation.NonNullByDefault
org.eclipse.jdt.core.compiler.annotation.notowning=org.eclipse.jdt.annotation.NotOwning
org.eclipse.jdt.core.compiler.annotation.nullable=org.jspecify.annotations.Nullable
org.eclipse.jdt.core.compiler.annotation.nullable.secondary=org.eclipse.jdt.annotation.Nullable
org.eclipse.jdt.core.compiler.annotation.nullanalysis=enabled
```

My guess is to why this is happening is that annotations besides the eclipse ones are assumed not to target generics.

That is all other annotations are treated like:

```java
DefaultLocation[] value() default { PARAMETER, RETURN_TYPE, FIELD} // missing TYPE_BOUND, TYPE_ARGUMENT };
```

To import my reproducible project you may need to adjust the `.classpath` to add the EEA. I have not added every EEA of the JDK but just the ones I know it needs.

```xml







```

I'm going to tag @sebthom as they are also one of the few that know Eclipse null analysis.

If I replace all the annotations with the Eclipse ones (and make the Eclipse ones primary) the issues go away.

@stephan-herrmann I'm happy to help anyway including code diving. I know promised help before but I have finally finished a bulk of opensource work and eager on making Eclipse null analysis one of the best jspecify-like options.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.