Skip to content

Handle @NullUnmarked methods with explicit @NonNull annotations in JDK javac plugin #1498

Open

Description

@msridhar

E.g.:

https://github.com/jspecify/jdk/blob/fd65be678b854e5d833ac3c8214d0051671c96a0/src/java.base/share/classes/java/nio/file/Files.java#L1821-L1823

We need to add library model / astubx support for null-unmarked methods, and generate the information about all the explicit @NonNull annotations.

Activity

  1. added
    jspecify
    on Mar 25, 2026
  2. vlsi commented on Oct 22, 2026

    @vlsi
    Contributor

    The JSON from the javac plugin already carries method-level @NullUnmarked (NullnessAnnotationSerializer passes hasNullUnmarked into MethodInfo), but AstubxGenerator.getMethodRecords ignores it and writes only Nullable annotations. The astubx record and the stubx reader in LibraryModelsHandler already accept an explicit NonNull on a parameter, but the record has no slot for "unmarked". So with JSpecifyJDKModels on, an @NullUnmarked method in a @NullMarked JDK class is checked as fully null-marked.

    This is visible to users as false positives on the @NullUnmarked methods of jspecify/jdk whose parameters carry no annotation: HttpCookie(String, String), HttpCookie.setValue, MulticastSocket.setInterface, and MulticastSocket.setNetworkInterface (also the protected KeyStore constructor). The same source compiles cleanly with JSpecifyJDKModels=false. Files.getAttribute(p, null) is reported today only because the whole method is treated as marked; after the fix it should still be reported, since its parameters are explicitly @NonNull.

    Reproducer (NullAway 3035507, JDK 21)

    Flags: -XepOpt:NullAway:AnnotatedPackages=foo -XepOpt:NullAway:JSpecifyMode=true -XDaddTypeAnnotationsToSymbol=true -XepOpt:NullAway:JSpecifyExperimental=true (turns on JSpecifyJDKModels)

    package foo;
    import java.net.HttpCookie;
    import java.net.MulticastSocket;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import org.jspecify.annotations.NullMarked;
    @NullMarked
    class Test {
      void cookie(HttpCookie c) {
        c.setValue(null); // reported, should not be: setValue is @NullUnmarked
      }
      HttpCookie ctor() {
        return new HttpCookie("n", null); // reported, should not be
      }
      void socket(MulticastSocket s) throws Exception {
        s.setInterface(null); // reported, should not be
      }
      Object attr(Path p) throws Exception {
        return Files.getAttribute(p, null); // reported, and should stay reported: explicit @NonNull
      }
    }

    Observed: passing @Nullable parameter 'null' where @NonNull is required on the setValue, HttpCookie constructor, setInterface, and getAttribute lines. Expected: only the getAttribute line is reported. With -XepOpt:NullAway:JSpecifyJDKModels=false none of the first three is reported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    false positivejdk-modelsjspecify

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions