一、说明
1、sonar已经安装OK
2、springboot项目
3、maven项目
二、生成token
1、登录到sonar
2、生成token
说明1: token仅生成一次,需保留
说明2: token忘记后,需要回收重新生成
3、执行命令
mvn sonar:sonar -Dsonar.projectKey=hg-demo-Dsonar.host.url=http://192.168.1.100:9000 -Dsonar.login=97de6a2ca95a7d75dff14ab718d7e4d6291af6c6 -Dsonar.java.binaries=target/classes
说明1: 修改对应的内容即可
说明2: 执行前,使用idea插件,先clean 再 compile,再执行上面的命令
三、BUG处理
1、Correctness - equals method overrides equals in superclass and may not be symmetric
由于@Data因其的BUG,@Data处理继承上需要独立声明 callSuper
解决:
子类上增加注解:
@EqualsAndHashCode(callSuper = true)
~~
2、Performance - Boxed value is unboxed and then immediately reboxed
解释:已装箱的值被解除装箱,然后立即重新装箱。
解决:将-1L改为静态常量即可
3、A "NullPointerException" could be thrown; "commandStats" is nullable here.
解释:空指针风险
解决:处理前判空
4、Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.
解释: 线程休眠异常未打标识
解决:catch捕获给线程打标识
Thread.currentThread().interrupt();
5、Change this code to use a stronger protocol.
解释: 当使用不安全的协议(即:不同于“TLSv1.2”或“DTLSv1.2”的协议)创建SSLContext时,该规则会引发问题。
解决:
context = SSLContext.getInstance("TLSv1.2");
TLSv1.2
~~
四、异味处理
0、处理建议
1)按类处理问题,异味分类大概几类,全局搜索替换会快一些
2)解决一类,sonar扫描代码提交,较少sonar相同类别的异味提醒
1、Remove this unused import 'cn.hutool.core.collection.CollUtil'.
解释: 引入类但未使用
解决:
1)鼠标聚焦的项目名称(点击选中项目),IDEA快捷键: Ctrl + Alt + O,全局扫描并处理引用未使用问题;
2)若项目包含vue前端模块,建议不要选择项目,而需要对java模块逐个选中然后快捷键;Ctrl + Alt + O会对vue的代码进行格式化
2、Use static access with "cn.hutool.core.collection.CollUtil" for "newArrayList".
解释: 建议方法改为"cn.hutool.core.collection.CollUtil" for "newArrayList",自定义类会被定义为异味,因为有现成的jar依赖,无需重复造轮子
解决:
1)全局替换类;全局替换可能会造成未import的问题,需要逐个类处理
2)建议处理顺序:全局替换 -> IDEA的maven插件compile -> 逐个类处理无引用编译报错 -> 处理引用未使用
3、Make "params" transient or serializable.
解释: 类中实现了Serializable接口(即序列化),而类中有Map私有变量,Map不支持序列化(需遵循CC BY-SA 4.0 许可协议)
解决: 用transient修饰即可
transient
~~