Please use the Apache issue tracking system for new NetBeans issues (https://issues.apache.org/jira/projects/NETBEANS0/issues) !!
Bug 262707 - Add configuration of Nullable/NonNull annotations. Use FQNs or allow to exclude FQNs.
Add configuration of Nullable/NonNull annotations. Use FQNs or allow to exclu...
Status: REOPENED
Product: java
Classification: Unclassified
Component: Hints
8.1
All All
: P3 with 1 vote (vote)
: Dev
Assigned To: Svata Dedic
issues@java
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2016-07-07 04:20 UTC by jinahya
Modified: 2018-01-09 11:39 UTC (History)
0 users

See Also:
Issue Type: DEFECT
:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo