redhat-developer / redhat-developer/vscode-java

@Nonnull fields are assumed to be non-null in the constructor

Open
#2,713 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug code analysis upstream
Dominant language
TypeScript
Stars
2.3k
Forks
546
Avg merge
20h 1m
Merged PRs (30d)
11

Description

Class fields annotated with @Nonnull are currently assumed to never be null in the class constructor.
@Nonnull only provides guarantees after construction has finished, and checking these fields in the constructor is often useful to avoid boilerplate variables.

A toy example illustrating the issue:

public class AnnotatedClass {
    private @Nonnull String specialValue;
    private @Nonnull Map<String, String> allValues = new HashMap<>();

    /* this constructor incorrectly warns that specialValue might not have been initialized */
    public AnnotatedClass(Iterable<String> values) {
        for (String entry : values)
        {
            String[] split = entry.split("=", 1);
            if (split.length != 2) throw new IllegalArgumentException("Value improperly formatted");

            final String key = split[0];
            final String value = split[1];
            if (value == null) /* needed to avoid warning on the assignment to specialValue later */
                continue;      /* side note: ideally we wouldn't need this, and the IDE           */
                               /*  would recognize that entry.split returns array of non-null     */
            this.allValues.put(key, value);
            if ("specialKey".equals(key))
                this.specialValue = value;
        }
        if (this.specialValue == null) /* the next line incorrectly produces a dead code warning */
            throw new IllegalArgumentException("The provided iterable does not have a special value!");
    }
}
Environment
  • Operating System: Windows 10 Enterprise
  • JDK version: openjdk 11.0.12 2021-07-20
  • Visual Studio Code version: 1.71.2
  • Java extension version: v1.11.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the warning with the supplied AnnotatedClass example in VS Code using the Java extension and the reported JDK environment. Trace the null-analysis behavior for @Nonnull fields during construction; done means the null check and later assignment no longer produce incorrect warnings while genuine issues remain reported.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.