Sonar代码规则

blocker级别

序号

分类

规则

解读

1

Bug

Loops should not be infinite

循环必须有一个终止条件

2

Bug

Override both equals and hashcode

equals和hashcode必须同时覆盖或则都不覆盖

3

Bug

Resources should be closed

使用资源必须关闭

4

Bug

Double-checked locking should not be used

dublecheck可能出现线程安全问题

5

Bug

“wait” should not be called when multiple locks are held

持有多个锁时,不应该调用wait方法,因为wait只会释放一个锁

6

Bug

“@Controller” classes that use “@SessionAttributes” must call “setComplete” on their “SessionStatus” objects

使用SessionAttributes时,需要调用setComplete来销毁attributes信息

7

Bug

“@SpringBootApplication” and “@ComponentScan” should not be used in the default package

这2个注解最好不要分开放在不同包不同类中

8

Bug

Printf-style format strings should not lead to unexpected behavior at runtime

格式化打印时,切勿写错格式与入参

9

Bug

“wait(…)” should be used instead of “Thread.sleep(…)” when a lock is held

在锁机制下,应该使用wait,而不是Thread.sleep;因为sleep不释放资源

10

Bug

“PreparedStatement” and “ResultSet” methods should be called with valid indices

statement和resultset下标索引都是1开头

11

Bug

Files opened in append mode should not be used with ObjectOutputStream

追加模式写文档切勿使用ObjectOutputStream,会导致StreamCorruptedException

12

Bug

Methods “wait(…)”, “notify()” and “notifyAll()” should not be called on Thread instances

wait、notify、notifyAll方法不应该被Thread调用,这样会破坏JVM本身改变线程的状态

13

Bug

Methods should not call same-class methods with incompatible “@Transactional” values

在同一个类中一个没有Transactional注解调用另外一个有Transactional注解的方法,这个Transactional会不生效

14

Vulnerability

Struts validation forms should have unique names

Struts的表单验证,不要使用相同名称

15

Vulnerability

Default EJB interceptors should be declared in “ejb-jar.xml”

ejb的拦截器一定要申明到ejb-jar.xml的配置文件中,不能更改配置文件名,否则拦截器会变成非默认

16

Vulnerability

“”@RequestMapping"" methods should be ““public””

@RequestMapping不要使用在private方法中,因为controller的方法通过反射机制调用,private不起到私有的作用。另外加上@sercurity也不起作用,因为AOP代码对非静态方法不起作用

17

Vulnerability

“HostnameVerifier.verify” should not always return true

ssl链接必须验证hostname

18

Vulnerability

XML parsers should not be vulnerable to XXE attacks

XML使用规范,防止被XXE或者SSRF漏洞

19

Vulnerability

LDAP deserialization should be disabled

LDAP协议不应该启用发序列话,有导致远程攻击的风险

20

Vulnerability

“javax.crypto.NullCipher” should not be used for anything other than testing

Cipher加密应该使用比较强大的算法

21

Vulnerability

Cipher algorithms should be robust

加密算法需要健壮,加密说法要指定安全模式和padding规则

22

Vulnerability

Encryption algorithms should be used with secure mode and padding scheme

加密说法要指定安全模式和padding规则

23

Vulnerability

Neither DES (Data Encryption Standard) nor DESede (3DES) should be used

不应该使用不安全的加密算法

24

Vulnerability

Security constraints should be defined

web服务器中web.xml必须设置,增强安全

25

Vulnerability

Databases should be password-protected

不应该使用没有密码的数据库

26

Vulnerability

Octal values should not be used

不建议使用八进制

27

code smell

[p3c]Braces are used with if, else, for, do and while statements, even if the body contains only a single statement.

if、while等建议使用大括号,哪怕主体只有一句代码

28

code smell

Methods and field names should not be the same or differ only by capitalization

方法名和字段名不能相同或仅大小写不同,容易造成困惑

29

code smell

[p3c]Manually create thread pool is better.

手动创建线程池,而非使用Excutor创建,可能考虑排队队列是无限长导致资源耗尽

30

code smell

Methods returns should not be invariant

返回结果不应该只有一种结果

31

code smell

[p3c]Use System.currentTimeMillis() to get the current millisecond. Do not use new Date().getTime().

使用System.currentTimeMillis()代替new Date().getTime(),new Date()其实也是调用currentTimeMillis,所以可以少创建一个对象

32

code smell

[p3c]The wrapper classes should be compared by equals method rather than by symbol of ‘==’ directly.

包装类应该使用equal方法

33

code smell

Switch cases should end with an unconditional “break” statement

每个case应该以一个无条件的break结束

34

code smell

“clone” should not be overridden

不建议重写clone方法,会造成浅拷贝以及跳过构造函数限制问题

35

code smell

“main” should not “throw” anything

main不应该抛异常,因为没有再外层可以catch这个exception

36

code smell

[p3c]When using regex, precompile needs to be done in order to increase the matching performance.

使用正则表达式,最好使用预编译号的pattern以提高性能

37

code smell

Short-circuit logic should be used in boolean contexts

boolean类型使用非与或操作以为的操作,可能导致错误

38

code smell

Future keywords should not be used as names

部分高版本已经被java定为关键字,最好也不要使用

39

code smell

“ThreadGroup” should not be used

ThreadGroup不要再使用了,有些方法不推荐,有些方法不安全。使用ThreadFactory和ThreadPoolExecutor代替

40

code smell

EJB interceptor exclusions should be declared as annotations

不推荐使用EJB的xml配置拦截器,推荐使用注解方式,这样更能显示看到

41

code smell

JUnit test cases should call super methods

JUnit测试用例,在初始化和结束方法中,如果是继承类最好调用super方法

42

code smell

TestCases should contain tests

TestCase中最好包含测试用例的方法,以避免命名TestCase但不是做测试用例而忽略某个类的测试

43

code smell

Silly bit operations should not be performed

某些确定的位移操作是可确定的,比如&-1、^0、

44

code smell

““switch”” statements should not contain non-case labels

switch语句的case分支不能省略case关键字;case分支的逻辑处理代码要封装为一个方法,不要在case分支里写复杂的逻辑处理

45

code smell

Assertions should be complete

断言方法要写全

46

code smell

JUnit framework methods should be declared properly

测试用例的方法命名必须按照JUnit规范

47

code smell

Child class fields should not shadow parent class fields

子类不推荐修改父类的属性的更封闭的修饰词,容易造成混乱

48

code smell

Threads should not be started in constructors

不应该在构造方法调用线程启动方法,如果该类被继承,那么线程可以在子类初始化完成之前启动,可能造成混乱

49

code smell

Tests should include assertions

测试用例中应该使用断言来测试,而非抛异常

50

code smell

Exit methods should not be called

不应该在代码中调用System.exit方法,避免被恶意攻击关闭整个JVM

51

code smell

[p3c]Do not remove or add elements to a collection in a foreach loop.

不要在for循环中添加或删除集合的元素

52

code smell

[p3c]Avoid using Apache Beanutils to copy attributes.

不建议使用apache的beanutils拷贝对象,因为性能上比较低

53

code smell

[p3c]Use ScheduledExecutorService instead.

推荐使用ScheduledExecutorService来做定时任务,相对于Timer来说对时间上更严格

54

security hotspot

Hard-coded credentials are security-sensitive

硬编码秘钥文件有风险,最好把秘钥文件存储到数据库或者其他专门的服务中和业务代码分开。放在代码中很容易反编译看到秘钥