Skip to content

@MonotonicNonNull should work for static fields #1160

@agrieve

Description

@agrieve

#1148 added support for @MonotonicNonNull for non-static fields. It would be great to have this work for static fields as well. Right now a static field with the annotation is treated as if it were annotated with @Nullable.

Example in chrome where this would be useful: ServiceLoaderUtil.sOverridesForTesting

import org.chromium.build.annotations.NullMarked;
import org.chromium.build.annotations.MonotonicNonNull;
import java.util.Map;

@NullMarked
public final class Foo {
    private static @MonotonicNonNull Map<Class<?>, Object> sOverridesForTesting;

    public static void doLater(Runnable r) {
    }

    public static void test() {
        if (sOverridesForTesting == null) {
            sOverridesForTesting = Map.of();
        }
        doLater(() -> sOverridesForTesting.clear());
    }
}
Foo.java:16: warning: [NullAway] dereferenced expression sOverridesForTesting is @Nullable
        doLater(() -> sOverridesForTesting.clear());
                                          ^
    (see http://t.uber.com/nullaway )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions