简体   繁体   English

使用 Lombok 的 @Getter 和 @Setter 时,SonarQube 找不到未使用的导入

[英]SonarQube can't find unused imports when using Lombok's @Getter and @Setter

When using lombok's generated getters and setters methods the unused imports are not detected.当使用 lombok 生成的 getter 和 setter 方法时,未检测到未使用的导入。

Example:例子:

@Entity
@Getter
@Setter
public class HomeMessage {

    ...
    private String message;
}
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完整的工作代码在这里: 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.因为我正在调用由 lombok 生成的.getMessage()方法,所以 sonarqube 未检测到未使用的导入。

If I remove the @Getter and @Setter annotations, and create the methods manually then the unused imports will be detected.如果我删除@Getter@Setter注释,并手动创建方法,那么将检测到未使用的导入。

I tried to set sonar.java.libraries without luck:我试图设置sonar.java.libraries没有运气:

<sonar.java.libraries>${user.home}/.m2/repository/org/projectlombok/lombok/**/*.jar</sonar.java.libraries>

Spring Boot 2.7.3 Spring 启动 2.7.3
Lombok 1.18.24龙目岛 1.18.24
Java 17 Java 17
SonarQube 9.6-community SonarQube 9.6-社区

Anyone has any solution for this?有人对此有任何解决方案吗?

try config like this in your sonar.properties: sonar.java.binaries=target/classes在你的 sonar.properties 中尝试这样的配置: sonar.java.binaries=target/classes

This is a bug in sonarjava and is tracked here: https://sonarsource.atlassian.net/browse/SONARJAVA-4313这是 sonarjava 中的一个错误,在此处进行跟踪: https://sonarsource.atlassian.net/browse/SONARJAVA-4313

The workaround is to add in maven:解决方法是在 maven 中添加:

<sonar.java.fileByFile>true</sonar.java.fileByFile>

Note that this solution might influence the performance of the analysis.请注意,此解决方案可能会影响分析的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM