Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove Sonar issue on Java stream "Refactor the code so this stream pipeline is used"

I’m working on a project (Java 17) where I have a list of object with two properties, actionId (String) and right (boolean). I’m trying to get the actionId for object with right = true and store the result as List of String.

This is my code:

 List<String> usserValidActionsArray = userActionGatewayDTO.stream().filter(UserActionGatewayDTO::getRight)
      .map(UserActionGatewayDTO::getActionId).toList();

My code works fine, but Sonar is blocking me by saying:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Refactor the code so this stream pipeline is used

Can anyone suggest to me how I can improve my code to be compliant?

>Solution :

I guess that Sonar doesn’t know about Stream.toList() (introduced in Java 16), and thinks it is a non-terminal operation. This seems to be bug SONAR-3746 (see also Stream#toList (JDK 16) is not recognized as terminal operator). This problem should be solved in SonarLint 7.2.

I guess, that means you need SonarQube 9 or higher, as SonarQube 9.0 is the first version that announced Java 16 support, and SonarQube 9.1 mentions adding new rules for Java 16. However, I’m not to familiar with how SonarLint and SonarQube updates interact in this way.

As a workaround, you can replace toList() with collect(Collectors.toList()).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading