This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 262707

Summary: Add configuration of Nullable/NonNull annotations. Use FQNs or allow to exclude FQNs.
Product: java Reporter: jinahya
Component: HintsAssignee: Svata Dedic <sdedic>
Status: REOPENED ---    
Severity: normal    
Priority: P3    
Version: 8.1   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:

Description jinahya 2016-07-07 04:20:19 UTC
When the field is annotated with @NotNull there comes a wrong hint

Unnecessary test for null - the expression is never null


public void setUsername(final String username) {
    if (this.username != null) { // HERE!!!
        throw new IllegalStateException("already has a username");
    }
    this.username = username;
}

@NotNull
private String username;

When I comment out the @NotNull, the hint gone.
Comment 1 Svata Dedic 2016-07-18 07:01:18 UTC
@NotNull annotation on a field declares that the field never sees a null value (except possibly if an exception is thrown during construction). NetBeans then assumes that the field can be safely dereferenced and generates warning: this.username cannot be null, as its annotation asserts, so checks for null (or non null) are always false (true).

IMHO, @NotNull is misused in your example, if the field is permitted to be null (username not initialized yet).


Maybe I didn't understand the use-case/example right, please elaborate.
Comment 2 jinahya 2016-07-18 10:57:04 UTC
I've never heard that @NotNull annotation on a field applies that the field will never null after construction.

@javax.validation.constraints.NotNull is for validating the bean on some specific cases such as @PrePersist or @PostLoad.

NetBeans should not presume or oversee what @NotNull intended to be.

final User user = new User(); // username is null

// do something else
user.username("whatever"); // username set

// do something else
user.username("else"); // username set

// get some coffee.
entityManager.persist(user); // @NotNull validates the inserting bean!

// NBA time!
entityManager.find(User.class, 1); // @NotNull validates the selected bean!
Comment 3 Svata Dedic 2016-07-18 12:08:42 UTC
Ah ;) OK, that @NotNull - it's a different story. Thanks for the explanation.

This Netbeans inspection iwas tailored for annotations like:
javax.annotation.Nonnull, org.jetbrains.annotations.NotNull, ...

for simplicity, NB uses just simple names and treats all the @NotNull the same to cover a wide range of annotations invented in various libraries. This behaviour seems to interfere with constraints.NotNull spec.

In your case, using @javax.validation.constraints.NotNull in static analysis is not appropriate, but there's no UI to configure the accepted annotations.

In the meantime, use @SuppressWarnings for the setter.
Comment 4 rkraneis 2018-01-09 11:39:09 UTC
A configuration option would be very appreciated as NetBeans (current dev version) seems to ignore org.checkerframework.checker.nullness.qual.NonNull