Issue
When using lombok's generated getters and setters methods the unused imports are not detected.
Example:
@Entity
@Getter
@Setter
public class HomeMessage {
...
private String message;
}
class="lang-java prettyprint-override">import java.io.File; // unused import
import org.springframework.web.bind.annotation.PostMapping; // unused import
@RestController
public class HomeController {
@Autowired
HomeRepository homeRepository;
@GetMapping("/")
public String helloWorld() {
return homeRepository.getReferenceById(1L).getMessage();
}
}
Full working code here: https://github.com/cod-r/spring-boot
Because I'm calling the .getMessage()
method, which is generated by lombok, the unused imports are not detected by sonarqube.
If I remove the @Getter
and @Setter
annotations, and create the methods manually then the unused imports will be detected.
I tried to set sonar.java.libraries
without luck:
<sonar.java.libraries>${user.home}/.m2/repository/org/projectlombok/lombok/**/*.jar</sonar.java.libraries>
Spring Boot 2.7.3
Lombok 1.18.24
Java 17
SonarQube 9.6-community
Anyone has any solution for this?
Solution
This is a bug in sonarjava and is tracked here: https://sonarsource.atlassian.net/browse/SONARJAVA-4313
The workaround is to add in maven:
<sonar.java.fileByFile>true</sonar.java.fileByFile>
Note that this solution might influence the performance of the analysis.
Answered By - Codrut
Answer Checked By - Pedro (JavaFixing Volunteer)