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.
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.
@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.
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!
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.
A configuration option would be very appreciated as NetBeans (current dev version) seems to ignore org.checkerframework.checker.nullness.qual.NonNull