typetools / typetools/checker-framework
Generic type variable doesn't respect @NonNull annotation on the generic tpye.
@smillst is already working on this.
Since Jun 14, 2025.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
The generic type Bar<T> is defined with a non-null constraint (@NonNull T). Therefore, the type variable T inherits this @NonNull annotation. As a result, the value variable in the example should be treated as non-null.
public interface Foo<@NonNull T> {
T foo(T input);
}
public static class Bar<@NonNull T> {
T value;
public Bar(Foo<T> foo, T input) {
this.value = foo.foo(input);
}
public String bar() {
return value.toString();
}
}
However, the compilation failed with "error: [dereference.of.nullable] dereference of possibly-null reference value" at the line of value.toString() . Could you please clarify why this occurs?
Interestingly, if the generic constraint is changed from @NonNull T to T extends @NonNull Object for Bar, the compilation succeeds. While the manual suggest that @NonNull T and T extends @NonNull Object are the same, this result show they behave differently. Could you please explain this as well?
The version 3.49.4 was used for the test.
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.