sonar扫描bug及对应修复

##1.Use isEmpty() to check whether the collection is empty or not.
解释:
建议使用list.isEmpty()方法 替代list.size()==0 或者 !list.isEmpty() 替代 list.size() >0
修改前:

if(attachedColumns.size() > 0) 

修改后:

if(attachedColumns.isEmpty()) 

2.Remove this expression which always evaluates to “true”

解释:
建议移除变量值一直是true,没有变化的值
修改前:

boolean columnExisted = false;
boolean groupExisted = false;
for(String id : columnIds) {String[] idTemp = id.split("\\.");String groupCode = idTemp[idTemp.length == 3 ? 1 : 0];if(targetGroupCode.equals(groupCode)) {groupExisted = true;}
}
if(!columnExisted && groupExisted)

修改后:

boolean columnExisted = false;
boolean groupExisted = false;
for(String id : columnIds) {String[] idTemp = id.split("\\.");String groupCode = idTemp[idTemp.length == 3 ? 1 : 0];if(targetGroupCode.equals(groupCode)) {groupExisted = true;}
}
if(groupExisted)

3.Replace this “Map.get()” and condition with a call to “Map.computeIfAbsent()”.

解释:
建议使用Map.computeIfAbsent() 方法替代Map.get() 方法,computeIfAbsent()方法对 hashMap 中指定 key 的值进行重新计算,如果不存在这个 key,则添加到 hashMap 中
修改前:

List<Node> relatedReports = reportIdMappingTable.get(reportId);
if(relatedReports == null) {relatedReports = new LinkedList<>();reportIdMappingTable.put(reportId,relatedReports);
}
relatedReports.add(new Node(targetNode,report));

修改后:

List<Node> relatedReports =reportIdMappingTable.computeIfAbsent(reportId, 
(Function<? super String, ? extends List<Node>>) new LinkedList<Node>());
relatedReports.add(new Node(targetNode,report));

4.Provide the parametrized type for this generic.

解释:
建议创建对象时添加默认参数类型
修改前:

 Map<String,List<Node>> reportIdMappingTable = new LinkedHashMap();

修改后:

Map<String,List<Node>> reportIdMappingTable = new LinkedHashMap<String,List<Node>>();

5.Iterate over the “entrySet” instead of the “keySet”

解释:
建议使用entrySet 替代 keySet,通过查看源代码发现,调用方法keySetMap.keySet()会生成KeyIterator迭代器,其next方法只返回其key值,而调用entrySetMap.entrySet()方法会生成EntryIterator 迭代器,其next方法返回一个Entry对象的一个实例,其中包含key和value。所以当我们用到key和value时,用entrySet迭代性能更高
修改前:

public void doSomethingWithMap(Map<String,Object> map) {for (String key : map.keySet()) {  Object value = map.get(key); }
}

修改后:

public void doSomethingWithMap(Map<String,Object> map) {for (Map.Entry<String,Object> entry : map.entrySet()) {String key = entry.getKey();Object value = entry.getValue();}
}

6.2 duplicated blocks of code must be removed.

解释::
有重复代码块建议删除
修改前:

修改后:

7.Replace this use of System.out or System.err by a logger.

解释:
建议使用logger日志输出替代控制台打印system.out.println();
修改前:

System.out.println("My Message"); 

修改后:

logger.log("My Message");

8.Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.

解释:
添加一个嵌套注释,解释为什么这个方法是空的,抛出UnsupportedOperationException,或者完成这个实现
修改前:

public void doSomething() {
}

修改后:

public void doSomething() {// Do nothing because of X and Y.
}

9.Remove the parentheses around the “x” parameter

解释:
移除最外成括号
修改前:

(x) -> x * 2

修改后:

x -> x * 2

10.Define and throw a dedicated exception instead of using a generic one.

解释:
定义并引发专用异常,而不是使用泛型异常
修改前:

public void foo(String bar) throws Throwable {  throw new RuntimeException("My Message");     
}

修改后:

public void foo(String bar) {throw new MyOwnRuntimeException("My Message");
}

11.Rename this field “LOG” to match the regular expression “[a-z][a-zA-Z0-9]*$”

解释:
重命名成符合命名规则(’[a-z][a-zA-Z0-9]*$’)的属性名称
修改前:

class MyClass {private int my_field;
}

修改后:

class MyClass {private int myField;
}

12.Replace “@RequestMapping(method = RequestMethod.GET)” with “@GetMapping”.

解释:
建议使用@GetMapping替换@RequestMapping(method = RequestMethod.GET)
修改前:

@RequestMapping(path = "/greeting", method = RequestMethod.GET) 
public Greeting greeting(@RequestParam(value = "name") String name) {
...
}

修改后:

@GetMapping(path = "/greeting") 
public Greeting greeting(@RequestParam(value = "name") String name) {
...
}

13.Define a constant instead of duplicating this literal “action1” 3 times.

解释:
建议定义个字符串常量值替代出现引用3次的字符串
修改前:

public void run() {prepare("action1");                           execute("action1");release("action1");
}

修改后:

private static final String ACTION_1 = "action1";  
public void run() {prepare(ACTION_1);                             execute(ACTION_1);release(ACTION_1);
}

14.Extract this nested ternary operation into an independent statement.

解释:
建议将此嵌套的三元操作提取到独立语句中
修改前:

public String getReadableStatus(Job j) {return j.isRunning() ? "Running" : j.hasErrors() ? "Failed" : "Succeeded"; 
}

修改后:

public String getReadableStatus(Job j) {if (j.isRunning()) {return "Running";}return j.hasErrors() ? "Failed" : "Succeeded";
}

15.Add a private constructor to hide the implicit public one.

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

class StringUtils { public static String concatenate(String s1, String s2) {return s1 + s2;}
}

修改后:

class StringUtils {private StringUtils() {throw new IllegalStateException("Utility class");}public static String concatenate(String s1, String s2) {return s1 + s2;}
}

16、SonarLint: This accessibility update should be removed.

field.setAccessible(true);

修复:可以使用反射工具类ReflectionUtils.makeAccessible替换

ReflectionUtils.makeAccessible(field);

17、This accessibility bypass should be removed.

field.set(obj, value);

修复:使用 ReflectionUtils.setField替换

ReflectionUtils.setField(field,obj,value);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/775708.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

菜鸟笔记-15arange函数学习

arange函数是Python中Numpy库的一个函数&#xff0c;主要用于生成一个等差数列。这个函数非常适用于创建指定范围内的数字序列&#xff0c;用于数值计算、数据分析等多种场景。 arange函数的语法如下&#xff1a; numpy.arange([start,] stop[, step,], dtypeNone) 参数说明…

机器学习 | 期望最大化(EM)算法介绍和实现

在现实世界的机器学习应用中&#xff0c;通常有许多相关的特征&#xff0c;但只有其中的一个子集是可观察的。当处理有时可观察而有时不可观察的变量时&#xff0c;确实可以利用该变量可见或可观察的实例&#xff0c;以便学习和预测不可观察的实例。这种方法通常被称为处理缺失…

vue2高德地图选点

<template><el-dialog :title"!dataForm.id ? 新建 : isDetail ? 详情 : 编辑" :close-on-click-modal"false" :visible.sync"show" class"rv-dialog rv-dialog_center" lock-scroll width"74%" :before-close&q…

Vue.js概述

一、概述 数据驱动的响应式框架&#xff0c;我们只关注Vue对象里面设置的数据即可&#xff0c;数据发生改变时&#xff0c;页面自动重新渲染 最典型的MVVM框架 二、挂载点 什么是“挂载点”&#xff1f;一个标签 作用&#xff1a;被Vue实例接收后&#xff0c;实例中设置的各…

boot整合xfire

最近换了项目组&#xff0c;框架使用的boot整合的xfire&#xff0c;之前没使用过xfire&#xff0c;所以写个例子记录下&#xff0c;看 前辈的帖子 整理下 pom文件 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot…

Java | 日期天数计算

大家可以关注一下专栏&#xff0c;方便大家需要的时候直接查找&#xff0c;专栏将持续更新~ 题目描述 编写一个Java程序&#xff0c;用于输入一个日期&#xff08;包括年、月、日&#xff09;&#xff0c;然后判断这一天是这一年的第几天。 程序需要接收一个表示日期的字符…

Python爬虫入门:从网站爬取文章内容并保存到本地文件

目录 前言 准备工作 简单爬虫实现 注意事项 爬虫伦理与合法性 总结 前言 在互联网时代&#xff0c;数据是宝贵的资源。然而&#xff0c;当需要从海量网站中抓取数据时&#xff0c;手动操作显然不切实际。这时&#xff0c;爬虫技术应运而生&#xff0c;成为我们获取数据的…

OSG编程指南<二十一>:OSG视图与相机视点更新设置及OSG宽屏变形

1、概述 什么是视图?在《OpenGL 编程指南》中有下面的比喻,从笔者开始学习图形学就影响深刻,相信对读者学习场景管理也会非常有帮助。 产生目标场景视图的变换过程类似于用相机进行拍照,主要有如下的步骤: (1)把照相机固定在三脚架上,让它对准场景(视图变换)。 (2)…

详细分析java.io.EOFException: readObject: unexpected end of file的解决方法

目录 前言1. 问题所示2. 原理分析3. 解决方法4. 彩蛋前言 以下问题涉及知识点推荐阅读 详细分析Java中的分布式任务调度框架 XXL-Job出现 Caused by: java.lang.NumberFormatException: For input string: “Error“ 解决方法(全)java框架 零基础从入门到精通的学习路线 附开…

【爬虫基础】第4讲 GET与POST请求

GET请求 GET请求是一种HTTP方法&#xff0c;用于向服务器获取&#xff08;或读取&#xff09;数据。它是Web开发中最常用的请求方式之一。对于GET请求&#xff0c;客户端向服务器发送一个HTTP请求&#xff0c;服务器返回请求的资源。GET请求通常用于获取静态资源&#xff0c;比…

c#基础-引用类型和值类型的区别

在C#中,数据类型分为两类:值类型和引用类型。 值类型:直接存储数据,分配在栈(Stack)上。常见的值类型包括基本数据类型(int, float, double等),结构体(struct),枚举(enum)等。 引用类型:存储数据的引用和对象,分配在托管堆(Heap)上。常见的引用类型包括类(cla…

记录关于智能家居的路程的一个bug___Segmentation fault(段错误)

前言 其实发生段错误的情况有很多&#xff1a; 其实在项目的开发中最有可能的错误就是①和②&#xff0c;考虑到本项目数组用的比较少&#xff0c;所以主要是考虑错误①指针的误用。 有时候错误就是那么离谱&#xff0c;声音也算是一种设备&#xff1f;&#xff1f;&#xff…

35.HarmonyOS App(ArkUI)使用父组件@Builder装饰的方法初始化子组件@BuilderParam报错

HarmonyOS App(ArkUI)使用父组件Builder装饰的方法初始化子组件BuilderParam报错 Type void is not assignable to type () > void. <tsCheck> 去掉括号()就可以了 装饰器&#xff1a; 用于装饰类、结构、方法以及变量&#xff0c;并赋予其特殊的含义。如上述示例中En…

Linux 内核工具 iptables 配置TCP/UDP端口转发(命令参考)

1、配置TCP端口转发 把本机20000/TCP端口转发到7.7.7.7:20000 iptables -t nat -A PREROUTING -p tcp --dport 20000 -j DNAT --to-destination 7.7.7.7:20000 iptables -t nat -A POSTROUTING -j MASQUERADE 2、配置UDP端口转发 把本机20000/UDP端口转发到7.7.7.7:20000 i…

SpringBoot实现RabbitMQ的简单队列(SpringAMQP 实现简单队列)

文章目录 1. 前言2. Basic Queue 简单队列模型2.1 父工程导入依赖2.2 消息发送2.2.1 消息发送方必要的配置2.2.2 发消息 3. 消息接收3.1 消息接收方必要的配置3.2 接收消息 1. 前言 SpringAMQP 是基于 RabbitMQ 封装的一套模板&#xff0c;并且还利用 SpringBoot 对其实现了自…

2024.3.26学习总结

一&#xff0c;正则匹配 正则匹配是用来搜索&#xff0c;匹配&#xff0c;替换的一种字符串模式&#xff0c;使用正则匹配可以让搜索匹配的语句更加简洁&#xff0c;在php中会使用一些函数来处理正则匹配 常用的语法&#xff1a; 字符类 [abc]: 匹配单个字符a、b或c[^abc]: 匹…

DevSecOps平台架构系列-互联网企业私有化DevSecOps平台典型架构

目录 一、概述 二、私有化DevSecOps平台建设思路 2.1 采用GitOps公有云建设 2.2 采用GitOps私有云建设 2.3 总结 三、GitOps及其生态组件 3.1 采用GitOps的好处 3.1.1 周边生态系统齐全 3.1.2 便于自动化的实现 3.1.3 开发人员属性GitOps 3.2 GitOps部分生态组件介绍…

搜维尔科技【应急推演】虚拟仿真技术的发展为煤炭矿井的安全生产找到新的出口

煤炭矿井的安全生产一直是我国关注的重大事项&#xff0c;保证煤炭矿井的安全生产&#xff0c;减少人员伤亡等不可逆的损失成为重中之重。虚拟仿真技术的发展为煤炭矿井的安全生产找到了新的出口。依托虚拟仿真技术&#xff0c;对煤炭矿井进行实时的生产监测&#xff0c;对矿井…

PTA 道路管制

乌拉乌拉国有n个城市和m条道路&#xff0c;城市编号为1∼n。由于乌拉乌拉国每一个城市都在创城&#xff08;创建文明城市&#xff09;&#xff0c;因此&#xff0c;城市之间的道路通行施行道路交通管制&#xff1a; 已知从城市ui​到城市vi​的道路&#xff0c;需要时间ti​。…

华为昇腾asend

昇腾Ascend C编程语言 Ascend C原生支持C/C编程规范&#xff0c;通过多层接口抽象、并行编程范式、孪生调试等技术&#xff0c;极大提高了算子的开发效率&#xff0c;帮助AI 参考文章 手把手教你在昇腾平台上搭建PyTorch训练环境 - 哔哩哔哩 (bilibili.com)https://www.bilibi…