typetools / typetools/checker-framework
EnsuresNonNull failing on generic method parameters
Open
@smillst is already working on this.
Since Mar 8, 2024.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
Hello,
I have come across an issue while using @EnsuresNonNull on method parameters with certain generic types.
If I have the following method
public class CollectionContracts {
@EnsuresNonNull("#1")
public static <T extends Collection<E>, E> @NonNull T requireNotEmpty(T collection) {
if(collection == null) {
throw new IllegalArgumentException("Collection was null");
}
if(collection.isEmpty()) {
throw new IllegalArgumentException("Collection was empty");
}
return collection;
}
}
the compilation fails with the following error message:
[...]/CollectionContracts.java:4:59
java: [contracts.postcondition] postcondition of requireNotEmpty is not satisfied.
found : no information about collection
required: collection is @NonNull
If I change the signature to
@EnsuresNonNull("#1")
public static <E> @NonNull Collection<E> requireNotEmpty(Collection<E> collection) {
everything work as I would expect.
I am using checker-framework version 3.42.0 with maven-compiler-plugin 3.12.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.