SonarQube 检测出的 bug、漏洞以及异味的修复整理,mysql 基础知识
[](
)12.Remove the unboxing from “Integer”

提示是这个 Integer 封装是多余的,所以直接用强类型转换就可以吧。

[](
)13.Use another way to initialize this instance

这个提示用其他的方式初始化,不要在定义的时候就初始化,修改如下:

遇到的基本上就是这些 bug 了,还有 css 中的一些 bug,按照提示修改的
[](
)14.Unexpected missing generic font family

将这两个 body 的样式合二为一,可以消除 bug 和漏洞
[](
)15.Unexpected duplicate “display”

Display 重复了,删除掉一个。
[](
)16.Unexpected unknown type selector “element”

空样式,直接删除掉。
[](
)17. Change this condition so that it does not always evaluate to “false”
(更改此条件,以便它不总是评估为“false")
这是很多 webservice 文件中出现的一个 bug。 如果后期会对 webservice 文件过滤,这个就可以不管了。不过也可以把这行删除掉,obj 不可能为 null if (obj == null) return false; 这样这个 bug 也没有了。

[](
)二 漏洞
[](
)1.Use a logger to log this exception

这种提示就是异常应该用日志打印出来。

[](
)2.‘password’ detected in this expression, review this potentially hard-coded credential

提示密码不能直接这样传递,不安全。但是也没有提供参考的案例。所以我是这样的改的,也能消除漏洞。如下图:

[](
)3.Make areaList a static final constant or non-public and provide accessors if needed

类变量字段不应具有公共可访问性。所以把 public 访问修饰符,改成其他的修饰符,最好是 private.

[](
)4.Secure this “Transformer” by either disabling external DTDs or enabling secure processing.

提示应该保护 XML 变换器。创建 javax.xml.transform.Transformer 但未启用“安全处理”或创建一个而不禁用外部 DTD 时,可能会发生 XML 外部实体或 XSLT 外部实体(XXE)漏洞。 如果该外部实体被攻击者劫持,则可能导致机密数据泄露,拒绝服务,服务器端请求伪造,从解析器所在机器的角度进行端口扫描,以及其他系统影响。
进行修改如下可以消除漏洞:

[](
)5.Do something with the “boolean” value returned by “delete”.

提示当包含操作状态代码时,不应忽略返回值。也就是说不应该忽略文件删除操作的结果。
所以进行如下修改,但是如下修改虽然修复了漏洞,但是新增了异味。

异味提示"java.nio.Files#delete" should be preferred (squid:S4042)。应该使用 Files.delete()方法,而不能之间文件 delete.所以最后修改成:

[](
)6.Make this “public static userInfoUrl” field final

这种“public static”字段应该成员变量应该是不变的,所以需要加上 final 修饰,如下:

还有几种漏洞不好修复,暂时没有思路
[](
)7.Change this method so it throws exceptions

这种提示,TrustManagers 不应盲目接受任何证书。通常会创建 X509TrustManager 接口的空实现,以允许连接到未由根证书颁发机构签名的主机。 这样的实现将接受任何证书,这使得应用程序容易受到中间人攻击。 正确的解决方案是提供适当的信任存储。
[](
)8.Use the recommended AES (Advanced Encryption Standard) instead.

这种原来用 DES 加密的提示不应使用 DES(数据加密标准)和 DESede(3DES)。它推荐的使用 AES.但是将 DES 加密改成 AES 加密虽然程序异味消除了,但是程序肯定不对吧,加密方式换了肯定会出问题的吧。
[](
)三 异味
异味太多了,我也没有消除太多了异味,一个项目异味一般都是好几 k,所以消除起来一两个异味对基数没有什么影响,并且异味太多,消除一部分后,并没有感受到那种异味数量巨减的感觉,导致消除异味的积极性不是很高。并且有的异味是真的不好消除,有时候打开一个文件,几千行代码一片都是标异味。直接就放弃了。
[](
)1.Replace the type specification in this constructor call with the diamond operator ("<>").

Java 7 引入了菱形运算符(<>)来减少泛型代码的冗长。 例如,您现在可以使用<>简化构造函数声明,而不必在其声明及其构造函数中声明 List 的类型,编译器将推断该类型。如下:

[](
)2.Add a default case to this switch

swatch 中没有 default,也没有 break;虽然上面的代码不要 break 也不会有什么问题。但是万一哪天变了,不是 return 就很容易出错了吧。
[](
)3.Remove this empty statement.

两个分号,代码中有很多地方有这种情况感觉,删掉多余的。
[](
)4.Remove this useless assignment to local variable “XXX”.

上图,定义的变量但是没有使用,就会抛出这种异味,解决这种异味,是需要看看这个变量有什么作用,没有作用的可以删除掉,如果不改随意改动,可以在他们下面增加一条日志打印他们,这样也能消除这个异味。
[](
)5.Directly append the argument of String.valueOf().

String.valueOf()不应附加到 String。这里我的理解是,result 是 string 类型,arerType 是 int 类型,在拼接的时候会自动的将 int 类型转换成 string,不用多此一举。

[](
)6.Define a constant instead of duplicating this literal “XXXX” 4 times.

类似这种,当一个不变的字符串在一个文件中多次出现,就应该给这些字符串提取成常量,这样方便以后修改和维护。但是说实话提取常量这个异味真的很枯燥,并且代码中有大量的这种情况,感觉每个项目或者每个模块都应该提取一个常量类,这样这个模块用到这些不变字符串,就直接从这个类中获取,但是这个工作量有点大哈哈,我就简单的尝试了一下,把自定义报表那部分提取常量,但是进行了一部分就没有进行下去了,枯燥且工作量大


但是我觉得这个工作还是做比较好,这样便于以后的维护,常量值改变只用改一个地方就好了,不用所有地方都修改。
[](
)7.Use a StringBuilder instead.

当字符串需要频繁变化时,用 stringBuilder 代替 string .这里还有一种异味是代码中有的地方用的是 StringBuffer,也需要转成 StringBuilder.因为 Stringbuffer 是确保线程安全的,stringBuilder
不用,所以 stringBuilder 效率更高。那会不会引发线程安全问题呢,不会,因为这个是在方法内部定义的变量,所以对这个方法而言是线程封闭的,不会引发线程安全问题。
[](
)8.Reorder the modifiers to comply with the Java Language Specification.

提示修饰符的顺序应该符合 java 语言规范,它给出的参考如下:

所以把 static 修饰符 放到 final 就好了。
[](
)9.Declare “XXX” on a separate line.

定义变量的时候,一个变量一行,便于查看

[](
)10.Return an empty collection instead of null.

最好不要直接返回 null,应返回空数组和集合.如下:

[](
)11.Use isEmpty() to check whether the collection is empty or not.

判断集合时候为空是,不要使用 size(),建议使用 isEmpty()方法。如下:

[](
)12.This block of commented-out lines of code should be removed

这种可以直接删除掉,或者不想删除的可以用/***/注释,对于单行的可以把后面的分号去掉就不会报错异味了。

[](
)13.Remove the literal “false” boolean value

布尔文字不应该是多余的。用 true 或者 false 在 if 中判断是不好的写好,直接可以通过本身进行判断,如下:

[](
)14.Add a private constructor to hide the implicit public one.

如果一个类的里面的方法都是 static 修饰的静态方法,那么需要给这个类定义一个非公共构造函数(添加私有构造函数以隐藏隐式公共构造函数)如下:

[](
)15.Refactor this method to reduce its Cognitive Complexity from 55 to the 15 allowed.

关于代码圈复杂度大于 15 的异味,以及代码过长的异味,说实话也没有什么好的方法,只能读代码,然后抽离函数出来,当然抽离函数不可能一次就能做到代码简洁之道要求的那样,一个函数只做一件事,单一层次原则。但是我们也不能因为做不到这样就就什么都不做,还是要迈出这一步,先抽离函数,虽然没有做到单一层次原则,但是消除了异味。下面代码是对上面的进行简单的函数抽离,消除异味

[](
)16.关于代码中很多的 stitch 分支问题。
评论