Home > Back-end >  Sonarqube Major issue "Refactor code so that stream pipeline is used"
Sonarqube Major issue "Refactor code so that stream pipeline is used"

Time:06-16

im working in java 17 project and i have the following method :

  public List<String> getUserroles(List<UserRoleDTO> userRoles) {

    return userRoles.stream().filter(UserRoleDTO::getRight).map(UserRoleDTO::getActionId)
      .toList();
  }

and my build is failled because sonarqube prompt Major issue "Refactor code so that stream pipeline is used".

any suggestion please on how i can adapt my code to be sonar compliant.

Best regards

CodePudding user response:

toList() is introduced in Java 16 as far as I can see.

Can you please see if your sonarqube version is also inline with what you are using in your code?

toList() is terminal operation, so you are correctly using streams. This leads issue only with Sonarqube version which may be referring java older than 16.

Try using sonarqube 9.x or higher which may be supporting java 16 onwards.

CodePudding user response:

Method Stream.toList() has been already mentioned in the rules of Java analyzer. And the latest SonarQube's Version 9.5 according to its description should support Java 16.

So firstly, try to update the SonarQube.

If it would not resolve the issue, you can substitute toList() with collect(Collectors.toList()).

  • Related