Repository navigation
Description
Version
NullAway 0.14.0, and current master (21f02dd). Error Prone 2.50.0, JDK 21.
Flags: -XepOpt:NullAway:JSpecifyMode=true -XDaddTypeAnnotationsToSymbol=true.
Summary
An anonymous class that extends Base<@Nullable String> has its overrides checked against Base<String>: the @Nullable in the supertype's type argument is dropped. Every override whose signature wraps the type variable in another type is then reported as a nullability mismatch.
A named class extending the very same type with the very same overrides is accepted, and so is a local class declared inside a method. Only the anonymous form is affected.
Reproducer
package foo;
import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
public class Test {
abstract static class Base<T extends @Nullable Object> {
abstract List<T> make();
}
static Object anonymous() {
return new Base<@Nullable String>() {
// BUG: Diagnostic contains: Method returns List<@Nullable String>, but overridden method returns List<String>
@Override List<@Nullable String> make() { throw new RuntimeException(); }
};
}
// same supertype, same override: clean
static class Named extends Base<@Nullable String> {
@Override List<@Nullable String> make() { throw new RuntimeException(); }
}
}Test.java:16: warning: [NullAway] Method returns List<@Nullable String>, but overridden method returns List<String>, which has mismatched type parameter nullability
@Override List<@Nullable String> make() { throw new RuntimeException(); }
^
What does and does not report
| Declaration form | Reported |
|---|---|
anonymous class extending Base<@Nullable String> |
yes |
anonymous class implementing Iface<@Nullable String> |
yes |
anonymous class in diamond form, new Base<>() { … } |
yes |
| named nested class extending the same type | no |
| local class declared inside a method | no |
anonymous class with a non-null type argument, new Base<String>() { … } |
no |
| Override signature, inside the anonymous class | Reported |
|---|---|
@Nullable String get() — bare type variable |
no |
List<@Nullable String> get() |
yes |
List<List<@Nullable String>> get() |
yes |
@Nullable String[] get() — array of the type variable |
yes |
void take(List<@Nullable String> arg) — parameter position |
yes |
| an override that does not mention the type variable | no |
So the substitution is right at the top level of the override's type and wrong as soon as the type variable sits inside a type constructor. Both messages NullAway has for override mismatches appear:
Method returns List<@Nullable String>, but overridden method returns List<String>, which has mismatched type parameter nullability
Parameter has type List<@Nullable String>, but overridden method has parameter type List<String>, which has mismatched type parameter nullability
The local-class row is what separates this from a scoping problem: a class declared in the same statement position, with the same supertype, is checked correctly as soon as it has a name.
Neither the shape of the type argument nor the bound on the type variable changes the outcome; @Nullable Object[] as the type argument, and T declared with an implicit non-null bound, both behave the same as the case above. That is what separates it from #1732, where where the signature came from is what mattered.
Why it is a false positive
new Base<@Nullable String>() { … } instantiates Base at @Nullable String, exactly as class Named extends Base<@Nullable String> does. The override's List<@Nullable String> is the substituted type of List<T>, so it matches. The diagnostic asks the author to write a return type that does not match the supertype.
Workaround
Give the anonymous class a name. That keeps the checking in place, which @SuppressWarnings("NullAway") on the enclosing method would not.
Where we hit it
RedisTable.scan returns an Enumerable<@Nullable Object[]> built as an anonymous AbstractEnumerable<@Nullable Object[]>. The same shape written as a named inner class, which is how KafkaStreamTable happens to spell it, is accepted. Found while replacing the Checker Framework with NullAway in Apache Calcite (apache/calcite#5213).
Activity
[CALCITE-7736] Put :mongodb under nullness verification
[CALCITE-7736] Put :cassandra under nullness verification
[CALCITE-7736] Finish the :innodb enumerable the module's own commit …
[CALCITE-7736] Put :plus under nullness verification
[CALCITE-7736] Put :mongodb under nullness verification
[CALCITE-7736] Put :cassandra under nullness verification
[CALCITE-7736] Finish the :innodb enumerable the module's own commit …
[CALCITE-7736] Put :plus under nullness verification
[CALCITE-7736] Put :testkit under nullness verification
[CALCITE-7736] Put :mongodb under nullness verification
[CALCITE-7736] Put :cassandra under nullness verification
[CALCITE-7736] Finish the :innodb enumerable the module's own commit …
[CALCITE-7736] Put :plus under nullness verification
[CALCITE-7736] Put :testkit under nullness verification
[CALCITE-7736] Put :mongodb under nullness verification
84 remaining items
[CALCITE-7736] Put :cassandra under nullness verification
[CALCITE-7736] Put :plus under nullness verification
[CALCITE-7736] Put :testkit under nullness verification
[CALCITE-7736] Put :mongodb under nullness verification
[CALCITE-7736] Put :cassandra under nullness verification
[CALCITE-7736] Put :plus under nullness verification
[CALCITE-7736] Put :testkit under nullness verification
[CALCITE-7736] Put :mongodb under nullness verification
[CALCITE-7736] Put :cassandra under nullness verification
[CALCITE-7736] Put :plus under nullness verification
[CALCITE-7736] Put :testkit under nullness verification
[CALCITE-7736] Put :mongodb under nullness verification
[CALCITE-7736] Put :cassandra under nullness verification
[CALCITE-7736] Put :plus under nullness verification
[CALCITE-7736] Put :testkit under nullness verification