Skip to content

NP_NULL_ON_SOME_PATH false negative with access method for method #3481

@mimers

Description

@mimers

if i have code like below:

public class Container {
    @Nullable
    private String name;

    public Container() throws Exception {
        Callable<String[]> shortTask = new Callable<String[]>() {

            @Override
            public String[] call() throws Exception {
                if (!name.isEmpty()) {
                    System.out.println("name = " + name);
                }
                return getShortName().split(",");
            }
        };
        shortTask.call();
    }

    @Nullable
    private String getShortName() {
        return name.length() < 5 ? null : name.split(",")[0];
    }
}

now spotbugs will report NP for name because field access which decorated by @Nullable annotation :

  <BugInstance type="NP_NULL_ON_SOME_PATH" priority="2" rank="8" abbrev="NP" category="CORRECTNESS">
    <Class classname="com.example.libdemo.Container$1">
      <SourceLine classname="com.example.libdemo.Container$1" start="12" end="20" sourcefile="Container.java" sourcepath="com/example/libdemo/Container.java"/>
    </Class>
    <Method classname="com.example.libdemo.Container$1" name="call" signature="()[Ljava/lang/String;" isStatic="false">
      <SourceLine classname="com.example.libdemo.Container$1" start="16" end="20" startBytecode="0" endBytecode="110" sourcefile="Container.java" sourcepath="com/example/libdemo/Container.java"/>
    </Method>
    <Field classname="com.example.libdemo.Container" name="name" signature="Ljava/lang/String;" isStatic="false" role="FIELD_CONTAINS_VALUE">
      <SourceLine classname="com.example.libdemo.Container" sourcefile="Container.java" sourcepath="com/example/libdemo/Container.java"/>
    </Field>
    <SourceLine classname="com.example.libdemo.Container$1" start="16" end="16" startBytecode="7" endBytecode="7" sourcefile="Container.java" sourcepath="com/example/libdemo/Container.java" role="SOURCE_LINE_DEREF"/>
    <SourceLine classname="com.example.libdemo.Container$1" start="16" end="16" startBytecode="4" endBytecode="4" sourcefile="Container.java" sourcepath="com/example/libdemo/Container.java" role="SOURCE_LINE_KNOWN_NULL"/>
  </BugInstance>

but not the getShortName() method access with same annotation.

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