当前位置:网站首页>代码质量管理
代码质量管理
2022-07-06 20:18:00 【qq_37705525】
代码质量管理
- 1 Make pushDataList a static final constant or non-public and provide accessors if needed.
- 2 Use Galois/Counter Mode (GCM/NoPadding) instead
- 3 Remove this call to "equals"; comparisons between unrelated types always return false.
- 4 Cast one of the operands of this integer division to a "double"
- 5 Sonar提示: Use "BigDecimal.valueOf" instead.
- 6 Either re-interrupt this method or rethrow the "InterruptedException".
- 7 Do something with the "boolean" value returned by "delete".
- 8 Use a logger to log this exception.
- 9 异常之IllegalAccessException
- 10 Use an "instanceof" comparison instead.
- 11 Remove this throw statement from this finally block.
- 12 Remove this return statement from this finally block
- 13 A "NullPointerException" could be thrown; "pkList" is nullable here.
- 14 Use try-with-resources or close this "ResultSet" in a "finally" clause.
- 15 Use "Arrays.toString(array)" instead.
- 16 Save and re-use this “Random”.
- 17 Either re-interrupt this method or rethrow the "InterruptedException".
- 18 Synchronize on a new "Object" instead.
- 19 Replace the call to "Thread.sleep(...)" with a call to "wait(...)"
- 20 Use "BigDecimal.valueOf" instead
- 21 Call "Optional#isPresent()" before accessing the value.
- 22 Use try-with-resources or close this "PreparedStatement" in a "finally" clause.
- 23 Make this "public static producer" field final
- 24 Lower the visibility of this setter or remove it altogether.
- 25 Make this "public static redisTemplate" field final
- 26 A “NullPointerException” could be thrown; “command” is nullable here.
- 27 Cast one of the operands of this multiplication operation to a “long”
- 28 Make this member "protected".
- 29 Not enough arguments
- 30 This branch can not be reached because the condition duplicates a previous condition in the same sequence of “if/else if” statements
- 31 Make this "public static st_soTimeout" field final
- 32 Make st_sendPort a static final constant or non-public and provide accessors if needed.
- 33 'PassWord' detected in this expression, review this potentially hard-coded credential.
1 Make pushDataList a static final constant or non-public and provide accessors if needed.
类变量字段不应具有公共可访问性。所以把public访问修饰符,改成其他的修饰符,最好是private.
公共类变量字段不尊重封装原则,有三个主要缺点:
无法添加其他行为,如验证。
内部表示是公开的,以后不能更改。
成员值可能会在代码中的任何地方发生变化,并且可能不符合程序员的假设。
通过使用私有属性和访问器方法(set和get),可以防止未经授权的修改。
没有充分的理由在不声明字段“final”的情况下声明字段“public”和“static”。大多数情况下,这是一个在多个对象之间共享状态的错误。但是使用这种方法,任何对象都可以对共享状态执行它想要的任何操作,例如将其设置为空。
2 Use Galois/Counter Mode (GCM/NoPadding) instead
3 Remove this call to “equals”; comparisons between unrelated types always return false.
删除对"equals"的调用;不相关类型之间的比较总是返回false。
if (transportTask.getTaskType().equals("1") || transportTask.getTaskType().equals("4")) {}
transportTask.getTaskType()为Short类型,所以if永远是false
short 数据类型是 16 位、有符号的以二进制补码表示的整数
最小值是 -32768(-2^15);
最大值是 32767(2^15 - 1);
Short 数据类型也可以像 byte 那样节省空间。一个short变量是int型变量所占空间的二分之一;
默认值是 0;
例子:short s = 1000,short r = -20000。
short在运算过程中会被提升到int类型或者更高类型进行运算。这是由于Java会自动将short数据在运算过程中当作一个int类型的字面值(literal)进行处理。byte类型做运算时同样如此,会被java自动进行提升等级。
short 是 Java 原始类型。short 变量可以存储16位带符号的整数。
Short 类是 short 原始类型的包装对象类。它定义代表此类型的值的范围的 MIN_VALUE 和 MAX_VALUE 常量。
Java 中的所有整数值都是 32 位的 int 值,除非值后面有 l 或 L(如 235L),这表示该值应解释为 long。
4 Cast one of the operands of this integer division to a “double”
Cast one of the operands of this division operation to a "double"
将此除法操作的一个操作数转换为"double"
非常基础,编码过程中,容易被忽略的错误,两个整数相除,结果必定是整数,
如果用float、double等数据类型接收,语法上不构成错误,但是会丢失精度。
/**
@author css
@date 2019/9/30 9:39
*/
public class Test {
public static void test(double d){
System.out.println(d);
}public static void main(String[] args) {
int a = 1;
int b = 2;
test(a/2);
float c = a/b;
System.out.println;
//此时c的值丢失精度
}
}
//Idea警告:浮点上下文中的整数除法
integer division in floating-point context
5 Sonar提示: Use “BigDecimal.valueOf” instead.
解决方法:使用BigDecimal.valueOf()代替。因为这个方法内部会将参数转换为String,保证精度不丢失。
public static BigDecimal valueOf(double val) {
return new BigDecimal(Double.toString(val));
}
6 Either re-interrupt this method or rethrow the “InterruptedException”.
修改为:
7 Do something with the “boolean” value returned by “delete”.
提示当包含操作状态代码时,不应忽略返回值。也就是说不应该忽略文件删除操作的结果。
所以进行如下修改,但是如下修改虽然修复了漏洞,但是新增了异味。
异味提示"java.nio.Files#delete" should be preferred (squid:S4042)。应该使用Files.delete()方法,而不能之间文件delete.所以最后修改成:
8 Use a logger to log this exception.
修改为如下:
9 异常之IllegalAccessException
java.lang.IllegalAccessException: Class com.study.reflect.ref_test.Tool can not access a member of class com.study.reflect.ref_test.Student with modifiers “private”
IllegalAccessException 表示没有访问权限的异常。
Class com.study.reflect.ref_test.Tool can not access a member of class com.study.reflect.ref_test.Student with modifiers “private”
tool类无法访问student中的成员。
这次权限访问异常,是因为自己在用暴力反射获取字节码对象的时候没有去除权限,也就是setAccessible(true)
10 Use an “instanceof” comparison instead.
修改为:
11 Remove this throw statement from this finally block.
说明:在finally块中使用return、break、throw等可以抑制try或catch块中抛出的任何未处理的Throwable的传播,修改为:
12 Remove this return statement from this finally block
说明:因为finally里面写了return语句的时候,就会覆盖掉try代码块里面的return。因为finally是肯定会执行的。例子如下:
上述代码修改为:
13 A “NullPointerException” could be thrown; “pkList” is nullable here.
增加空值判断,如下所示:
14 Use try-with-resources or close this “ResultSet” in a “finally” clause.
修改为:
或者参考如下:
提示资源没有关闭,需要在finally中进行资源关闭,但是把资源关闭放到finally中由提示这样写不规范有异味。所以它推荐的写法是将创建资源流的代码放在try()中,这样系统会自动的关闭资源,不需要我们写.close()方法,如图
15 Use “Arrays.toString(array)” instead.
修改为:
参考如下
16 Save and re-use this “Random”.
说明:这种提示是随机数应该需要重用,然后他给出的参考是这样的
17 Either re-interrupt this method or rethrow the “InterruptedException”.
修改为
18 Synchronize on a new “Object” instead.
修改为
19 Replace the call to “Thread.sleep(…)” with a call to “wait(…)”
说明:如果在当前线程持有锁时调用Thread.sleep(…),则可能导致性能和可伸缩性问题,甚至更糟,因为持有锁的线程的执行被冻结。最好对monitor对象调用wait(…)来暂时释放锁并允许其他线程运行。修改为如下:
20 Use “BigDecimal.valueOf” instead
说明:由于浮点不精确,您不太可能从BigDecimal(double)构造函数中获得预期的值。修改为如下:
21 Call “Optional#isPresent()” before accessing the value.
说明:Optional value可以保存值,也可以不保存。可选方法中的值可以使用get()方法访问,但它会抛出一个
如果不存在值,则NoSuchElementException。为了避免异常,应该总是在调用get()之前调用isPresent()方法。
另外,请注意其他方法,如orElse(…)、orElseGet(…)或orElseThrow(…),可用于指定如何处理空的可选对象。
修改为如下:
22 Use try-with-resources or close this “PreparedStatement” in a “finally” clause.
修改为如下所示:使用try-with-resources语法
漏洞
23 Make this “public static producer” field final
修改为如下:
24 Lower the visibility of this setter or remove it altogether.
解决方法:去掉枚举中的set方法
25 Make this “public static redisTemplate” field final
修改为如下:
26 A “NullPointerException” could be thrown; “command” is nullable here.
空指针,解决方式:先判断或者先实例化,再访问里面的属性或者成员。
27 Cast one of the operands of this multiplication operation to a “long”
说明:int数运算最终再把结果转为long将有可能产生溢出
解决方案:转换为long型预算
举例:
long bigNum = Integer.MAX_VALUE + 2; // Noncompliant. Yields -2147483647
换为
long bigNum = Integer.MAX_VALUE + 2L;
28 Make this member “protected”.
解决办法
29 Not enough arguments
上面的这种bug在项目中很常见,参数都用new Object[]{}封装起来。特别是从class文件反编译回来成的java文件格式也是这样的。但是如果要消除bug的话直接使用参数就好了,不要用new Object[]{}封装。
30 This branch can not be reached because the condition duplicates a previous condition in the same sequence of “if/else if” statements
if分支中不能出现两个分支一样的情况。
把重复的删除掉就可以了。
31 Make this “public static st_soTimeout” field final
没有充分的理由在不声明字段“final”的情况下声明字段“public”和“static”。大多数情况下,这是一个在多个对象之间共享状态的错误。但是使用这种方法,任何对象都可以对共享状态执行它想要的任何操作,例如将其设置为空。
解决
32 Make st_sendPort a static final constant or non-public and provide accessors if needed.
33 ‘PassWord’ detected in this expression, review this potentially hard-coded credential.
边栏推荐
- SQL中删除数据
- 24.(arcgis api for js篇)arcgis api for js点修改点编辑(SketchViewModel)
- Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
- How to replace the backbone of the model
- Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]
- Construction of knowledge map of mall commodities
- Mathematical induction and recursion
- Don't you know the relationship between JSP and servlet?
- Jericho turns on the display icon of the classic Bluetooth hid mobile phone to set the keyboard [chapter]
- 编译常量、ClassLoader类、系统类加载器深度探析
猜你喜欢
树莓派设置静态ip
The latest 2022 review of "small sample deep learning image recognition"
又一百万量子比特!以色列光量子初创公司完成1500万美元融资
硬件之OC、OD、推挽解释
腾讯云原生数据库TDSQL-C入选信通院《云原生产品目录》
leetcode
杰理之在非蓝牙模式下,手机连接蓝牙不要跳回蓝牙模式处理方法【篇】
Decoration design enterprise website management system source code (including mobile source code)
23.(arcgis api for js篇)arcgis api for js椭圆采集(SketchViewModel)
22.(arcgis api for js篇)arcgis api for js圆采集(SketchViewModel)
随机推荐
【达梦数据库】添加自动收集统计信息的任务
Jericho is in non Bluetooth mode. Do not jump back to Bluetooth mode when connecting the mobile phone [chapter]
RestClould ETL 社区版六月精选问答
数学归纳与递归
[C language] question set of IX
[cpk-ra6m4 development board environment construction based on RT thread studio]
Jerry's transmitter crashed after the receiver shut down [chapter]
Nuggets quantification: obtain data through the history method, and use the same proportional compound weight factor as Sina Finance and snowball. Different from flush
存储过程与函数(MySQL)
2022.6.28
C language string sorting
Jerry's broadcast has built-in flash prompt tone to control playback pause [chapter]
Intelligent static presence detection scheme, 5.8G radar sensing technology, human presence inductive radar application
Significance and measures of source code confidentiality
Optimization of application startup speed
leetcode
Jerry's phonebook acquisition [chapter]
How does C language (string) delete a specified character in a string?
VHDL实现任意大小矩阵加法运算
Make (convert) ICO Icon