java:S5411 triggered on generic type parameters

I don’t know if this is fixed yet because i cannot see the ticket linked here, but a similar thing happens when implementing a generic functional interface, example using JavaFx observables:

booleanProperty().addListener((observable, oldValue, newValue) -> {
   if (newValue) {
       // ... do something ...
   }
});

On SonarQube Developer Edition Version 9.9.1, this results in “Use a primitive boolean expression here.” code smell on line if (newValue) {.

Developer can do nothing here of course because addListener is a JavaFx function that expects a lamda with a parameter based on the generic type of the property, which in this case is Boolean.

Hi Jan,

Thank you for reporting the problem!

I was able to reproduce it with an ArrayList and forEach:

List<Boolean> xs = new ArrayList<>();
xs.add(true);
xs.add(false);
///xs.add(null);
xs.forEach(value -> {
  if(value) {
    System.out.println("yes");
  } else {
    System.out.println("no");
  }
});

The issue is a true positive - uncommenting the line that adds null yields a NullPointerException.

The ticket that you linked was fixed, but it is not applicable here, as it helps only when boxed Boolean comes from a method call.

I created a ticket to track this issue.

1 Like