Sonar相关规则解读2

今天继续来看Sonar规则


3、Throwable.printStackTrace(...) should not be called

处理异常时不要用e.printStackTrace(),最好记录log

Throwable.printStackTrace(...) prints a Throwable and its stack trace to some stream. By default that stream System.Err, which could inadvertently expose sensitive information.

Loggers should be used instead to printThrowables, as they have many advantages:

1、Users are able to easily retrieve the logs.

2、The format of log messages is uniform and allow users to browse the logs easily.

This rule raises an issue when printStackTrace is used without arguments, i.e. when the stack trace is printed to the default stream.



4、"public static" fields should be constant

There is no good reason to declare a field "public" and "static" without also declaring it "final". Most of the time this is a kludge to share a state among several objects. But with this approach, any object can do whatever it wants with the shared state, such as setting it to null.

从代码安全性上考虑,public static 一定要加 final。


Sonar相关规则解读1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容