没有合适的资源?快使用搜索试试~ 我知道了~
首页sonar-JAVA检查规则指南.docx
sonar-JAVA检查规则指南.docx

SonarQube分析Java项目,遵循的基本内置规则;规则为常用激活规则,含bug、漏洞、坏味道三方面不同程度(严重、阻断、主要、次要、提示等级别)。
资源详情
资源评论
资源推荐

1
1
bug
bug
类型
类型
1.1
1.1
".equals()" should not be used to test the values of
".equals()" should not be used to test the values of
"Atomic" classes.
"Atomic" classes.
bug 主要
AtomicInteger 和 AtomicLong 扩展 Number,但它们与 Integer 和 Long 不同,因此应以
不同的方式处理。 AtomicInteger 和 AtomicLong 旨在支持对单个变量进行无锁的线程安全
编程。 这样,AtomicInteger 将永远只与自身“相等”。 相反,您应该.get()值并对其进行
比较。
1.2
1.2
"=+" should not be used instead of "+="
"=+" should not be used instead of "+="
bug 主要
使用运算符对(= +,=-或=!)会编译并运行,但不会产生预期的结果,而这意味着
使用反向的单个运算符(+ =,-=或!=)。
当= +,=-或=!时,此规则会引起问题。 在两个运算符之间没有空格且之后至少有一个空
格字符时使用。
1.3
1.3
"@Controller" classes that use "@SessionAttributes"
"@Controller" classes that use "@SessionAttributes"
must call "setComplete" on their "SessionStatus" objects
must call "setComplete" on their "SessionStatus" objects
bug 阻断
使用@SessionAttributes 的 Spring @Controller 旨在处理有状态/多帖子形式。 这样的
@Controller 使用指定的@SessionAttributes 在两次请求之间将数据存储在服务器上。 会
话结束时,应该清除该数据,但是除非从@RequestMapping 方法在 SessionStatus 对象
上调用 setComplete(),否则 Spring 和 JVM 都不知道是时候这样做了。 请注意,必须
将 SessionStatus 对象作为参数传递给该方法。
1.4
1.4
"@NonNull" values should not be set to null
"@NonNull" values should not be set to null
bug 次要
标记为@ NotNull,@ NonNull 或@Nonnull 的字段,参数和返回值假定为具有非 null
值,并且在使用前通常不进行 null 检查。 因此,将这些值之一设置为 null 或未能在构造函
数中设置此类类字段,可能会在运行时导致 NullPointerExceptions。
1.5
1.5
"@SpringBootApplication" and "@ComponentScan"
"@SpringBootApplication" and "@ComponentScan"
should not be used in the default package
should not be used in the default package
bug 阻断
@ComponentScan 用于确定哪些 Spring Bean 在应用程序上下文中可用。 可以使用
basePackageClasses 或 basePackages(或其别名值)参数来配置要扫描的软件包。 如果未配
置任何参数,则@ComponentScan 将仅考虑带有注释的类的程序包。 在属于默认包的类上
使用@ComponentScan 时,将扫描整个类路径。
这 将 减 慢 应 用 程 序 的 启 动 速 度 , 并 且 该 应 用 程 序 可 能 无 法 启 动
BeanDefinitionStoreException,因为您最终扫描了 Spring Framework 软件包本身。

在以下情况下,此规则会引起问题:
-@ ComponentScan,@ SpringBootApplication 和@ServletComponentScan 用于默认包的
类
-@ComponentScan 已使用默认程序包显式配置
1.6
1.6
"BigDecimal(double)" should not be used
"BigDecimal(double)" should not be used
bug 主要
由于浮点数的不精确性,您不太可能从 BigDecimal(double)构造函数中获得期望的
值。
从 JavaDocs:
此构造函数的结果可能有些不可预测。 可以假设用 Java 编写新的 BigDecimal(0.1)
会创建一个 BigDecimal,它精确地等于 0.1(未缩放的值 1,小数位为 1),但实际上等于
0.1000000000000000055511151231257827021181583404541015625。 这是因为 0.1 无法精确
表示为双精度(或就此而言,表示为任何有限长度的二进制分数)。 因此,尽管出现,但
是传递给构造函数的值并不完全等于 0.1。
相反,您应该使用 BigDecimal.valueOf,该函数在幕后使用一个字符串消除浮点舍入错
误,或者使用一个带有 String 参数的构造函数。
1.7
1.7
"compareTo" results should not be checked for specific
"compareTo" results should not be checked for specific
values
values
bug 次要
尽管大多数 compareTo 方法返回-1、0 或 1,有些则不返回,并且将 compareTo 的结果
与 0 以外的特定值进行测试可能会导致假阴性。
1.8
1.8
"compareTo" should not be overloaded
"compareTo" should not be overloaded
bug 主要
在实现 Comparable <T> .compareTo 方法时,参数的类型必须与 Comparable 声明中使
用的类型匹配。 当使用其他类型时,这将创建一个重载而不是一个替代,这不太可能成为
意图。
当实现 Comparable <T>的类的 compareTo 方法的参数与 Comparable 声明中使用的参数
不同时,此规则会引起问题。
1.9
1.9
"compareTo" should not return "Integer.MIN_VALUE"
"compareTo" should not return "Integer.MIN_VALUE"
bug 次要
重要的是符号,而不是 compareTo 返回的值的大小。 返回 Integer.MIN_VALUE 不会
传达更高程度的不平等,并且这样做可能会导致错误,因为 compareTo 的返回值有时会被
反 转 , 并 期 望 负 值 变 为 正 值 。 但 是 , 反 转 Integer.MIN_VALUE 会 产 生
Integer.MIN_VALUE 而不是 Integer.MAX_VALUE。
1.10
1.10
"DefaultMessageListenerContainer" instances should not
"DefaultMessageListenerContainer" instances should not
drop messages during restarts
drop messages during restarts
bug 主要
DefaultMessageListenerContainer 被实现为 JMS 轮询器。 当 Spring 容器自行关闭时,

随着每个正在进行的 JMS Consumer.receive()调用完成,任何非空返回值将是一条
JMS 消息,由于正在进行的关闭,DMLC 将丢弃该消息。 这将导致接收到的消息永远不会
被处理。
为了防止在重新启动操作期间丢失消息,请将 acceptMessagesWhileStopping 设置为
true,以便在关闭之前处理此类消息。
1.11
1.11
"Double.longBitsToDouble" should not be used for "int"
"Double.longBitsToDouble" should not be used for "int"
bug 主要
Double.longBitsToDouble 需要 64 位长参数。 将其传递给较小的值(例如 int),然后
将数学转换为双精度值根本无法按预期进行,因为将错误地解释位的布局,就好像孩子在
尝试使用成人的手套一样。
1.12
1.12
"equals" method overrides should accept "Object"
"equals" method overrides should accept "Object"
parameters
parameters
bug 主要
“equals”作为方法名称应专门用于覆盖 Object.equals(Object),以防止造成混淆。
试图重载该方法以使用特定的类而不是 Object 作为参数,以保存类比较检查。 但是,当这
是唯一的替代时,它将无法按预期工作。
1.13
1.13
"equals(Object obj)" and "hashCode()" should be
"equals(Object obj)" and "hashCode()" should be
overridden in pairs
overridden in pairs
bug 次要
根据 Java 语言规范,equals(Object)和 hashCode()之间有一个约定:
如果根据 equals(Object)方法两个对象相等,则在两个对象中的每个对象上调用
hashCode 方法必须产生相同的整数结果。
如果根据 equals(java.lang.Object)方法,如果两个对象不相等,则不需要在两个对象
中的每个对象上调用 hashCode 方法必须产生不同的整数结果。
但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的
性能。
为了遵守此合同,这些方法应该都被继承,或者都被覆盖。
1.14
1.14
"equals(Object obj)" should test argument type
"equals(Object obj)" should test argument type
bug 次要
由于 equals 方法将通用对象作为参数,因此可以将任何类型的对象传递给它。 该方法
不应假定它将仅用于测试其类类型的对象。 它必须改为检查参数的类型。
1.15
1.15
"Externalizable" classes should have no-arguments
"Externalizable" classes should have no-arguments
constructors
constructors
bug 主要
Externalizable 类是处理其自己的序列化和反序列化的类。 在反序列化期间,过程的第
一 步 是 使 用 类 的 无 参 数 构 造 函 数 的 默 认 实 例 化 。 因 此 , 没 有 no-arg 构 造 函 数 的
Externalizable 类不能反序列化。

1.16
1.16
"getClass" should not be used for synchronization
"getClass" should not be used for synchronization
bug 主要
getClass 不应在非最终类中用于同步,因为子类将在与父类或彼此之间不同的对象上
进行同步,尽管使用了 synced 关键字,但允许一次将多个线程同时进入代码块。
而是,硬编码要在其上进行同步或使其最终化的类的名称。
1.17
1.17
"hashCode" and "toString" should not be called on array
"hashCode" and "toString" should not be called on array
instances
instances
bug 主要
虽然 hashCode 和 toString 在数组上可用,但它们在很大程度上没有用。 hashCode 返回
数组的“身份哈希码”,而 toString 返回几乎相同的值。两种方法的输出均未实际反映数组的
内容。相反,您应该将数组传递给相关的静态 Arrays 方法。
1.18
1.18
"InterruptedException" should not be ignored
"InterruptedException" should not be ignored
bug 主要
绝不应该在代码中忽略 InterruptedExceptions,在这种情况下,只需将异常计数记录为
“忽略”即可。抛出 InterruptedException 会清除 Thread 的中断状态,因此,如果未正确处理
该异常,则该线程被中断的事实将丢失。相反,应该立即或在清除方法状态后重新抛出
InterruptedExceptions,或者应该通过调用 Thread.interrupt()重新中断线程,即使这应该
是单线程应用程序也是如此。任何其他措施可能会导致线程关闭延迟,并丢失该线程被中
断的信息-可能未完成其任务。
同样,也应传播 ThreadDeath 异常。根据其 JavaDoc:
如果 ThreadDeath 被某个方法捕获,则将其重新抛出以使线程实际上死亡是很重要的。
1.19
1.19
"iterator" should not return "this"
"iterator" should not return "this"
bug 主要
Java 标准库中有两个处理迭代的类:Iterable <T>和 Iterator <T>。 Iterable <T>表示可以
作为“ for-each 循环”语句的目标的数据结构,而 Iterator <T>表示正在进行的遍历的状态。
通常期望 Iterable <T>支持多个遍历。
还通过返回本身的 iterator()实现 Iterable <t>的 Iterator <T>将不支持多次遍历,因为
它将保留其状态。
当同时实现 Iterable <T>和 Iterator <t>的类的 iterator()方法返回此规则时,此规则会
引起问题。
1.20
1.20
"Iterator.hasNext()" should not call "Iterator.next()"
"Iterator.hasNext()" should not call "Iterator.next()"
bug 主要
调 用 Iterator.hasNext ( ) 不 应 有 任 何 副 作 用 , 因 此 不 应 更 改 迭 代 器 的 状 态
Iterator.next ( ) 使 迭 代 器 前 进 一 项 。 因 此 , 在 Iterator.hasNext ( ) 中 调 用 它 会 破 坏
hasNext()契约,并导致生产中出现意外行为。
1.21
1.21
"Iterator.next()"
"Iterator.next()"
methods should throw
methods should throw
"NoSuchElementException"
"NoSuchElementException"
bug 主要
根据合同,当迭代中没有更多元素时,java.util.Iterator.next()方法的任何实现都应引

发 NoSuchElementException 异常。 迭代完成后,任何其他行为都可能导致此 Iterator 用户
的意外行为。
1.22
1.22
"notifyAll" should be used
"notifyAll" should be used
bug 主要
notify 和 notifyAll 都唤醒睡眠线程,但仅通知一个唤醒线程,而 notifyAll 唤醒所有线程。
由于 notify 可能不会唤醒正确的线程,因此应改用 notifyAll。
1.23
1.23
"null" should not be used with "Optional"
"null" should not be used with "Optional"
bug 主要
可选的概念是当 null 可能导致错误时将使用它。 在某种程度上,它替代了 null,并且
在使用 Optional 时,永远不会存在从调用返回或接收 null 的问题。
1.24
1.24
"PreparedStatement" and "ResultSet" methods should
"PreparedStatement" and "ResultSet" methods should
be called with valid indices
be called with valid indices
bug 阻断
PreparedStatement 中的 参 数从 1 开始 编号 , 而不 是 0 , 因此 ,使用 数 字小 于 1 的
PreparedStatement 的任何“设置”方法都是一个错误,因为使用大于参数数量的索引也是一个
错误。同样,ResultSet 索引也从 1 开始,而不是 0。
1.25
1.25
"read" and "readLine" return values should be used
"read" and "readLine" return values should be used
bug 主要
当调用返回从某些数据源读取的数据的方法时,应存储该数据而不是将其丢弃。当然,
任何其他措施都是错误。
当 以 下 任 何 一 项 的 返 回 值 被 忽 略 或 仅 进 行 空 检 查 时 , 此 规 则 会 引 起 问 题 :
BufferedReader.readLine(),Reader.read()以及任何子类中的这些方法。
1.26
1.26
"runFinalizersOnExit" should not be called
"runFinalizersOnExit" should not be called
bug 严重
默 认 情 况 下 , 在 JVM 出 口 上 运 行 终 结 器 是 禁 用 的 。 可 以 使 用
System.runFinalizersOnExit 和 Runtime.runFinalizersOnExit 启用它,但是不赞成使用这两种
方法,因为它们本质上是不安全的。
根据 Oracle Javadoc:
这可能会导致在活动对象上调用终结器,而其他线程正在同时操纵这些对象,从而导
致行为不稳定或死锁。
如果您真的想在虚拟机开始其关闭序列时执行某些操作,则应附加一个关闭钩子。
1.27
1.27
"ScheduledThreadPoolExecutor" should not have 0 core
"ScheduledThreadPoolExecutor" should not have 0 core
threads
threads
bug 严重
java.util.concurrent.ScheduledThreadPoolExecutor 的池的大小与 corePoolSize 相同,因此
将 corePoolSize 设置为零意味着执行程序将没有线程且不运行。
此规则通过其设置程序或对象构造函数检测 corePoolSize 设置为零的实例。
剩余60页未读,继续阅读















安全验证
文档复制为VIP权益,开通VIP直接复制

评论2