Sonar的bug修改

1.bug修改

1.Introduce a new variable instead of reusing the parameter “vin”.

在这里插入图片描述
使用一个新的变量;

2. A “NullPointerException” could be thrown; “command” is nullable here.

在这里插入图片描述
进行空值判断;

Objects.requireNonNull(command, "command must not be null");
//字符串可以使用进行空值判断
StringUtils.isEmpty()

例子:
//获取登录手机号和密码
String mobile = member.getMobile();
String password = member.getPassword();

//手机号和密码非空判断
if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
    throw new wywException(20001,"登录失败");
}
3.Change this condition so that it does not always evaluate to “false”

在这里插入图片描述
进行条件判断,我在这里没看出来有什么问题!

 if (!t1())if (t1 == false) 是一个意思。
 可以使用hutool工具包判断:
 1.引入依赖
 <dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.1</version>
</dependency>
2.写判断
 if (BooleanUtil.isTrue(t1())) {
     System.out.println("======"+t1());
 }
4.Use another way to initialize this instance.

在这里插入图片描述
不要在初始化的时候赋值!
改为:

public static final Set<String> WORK_STATUS_LEAVE = new HashSet<>();
static {
    WORK_STATUS_LEAVE.add("离职");
    WORK_STATUS_LEAVE.add("离退职人员");
    WORK_STATUS_LEAVE.add("调离");
    WORK_STATUS_LEAVE.add("死亡人员");
    WORK_STATUS_LEAVE.add("离退");
    WORK_STATUS_LEAVE.add("死亡");
    WORK_STATUS_LEAVE.add("正式不在岗");
    WORK_STATUS_LEAVE.add("不在岗合同工");
    WORK_STATUS_LEAVE.add("非公司人员");
    WORK_STATUS_LEAVE.add("内退");
    WORK_STATUS_LEAVE.add("正退");
    WORK_STATUS_LEAVE.add("退休");
}
5.Call “Optional#isPresent()” before accessing the value.

在这里插入图片描述

 case "SPEED":
     Optional<Map<String, Object>> optional = mapList.stream().filter(m -> "4".equals(m.get("paramId").toString())).findAny();
     if (optional.isPresent()) {
         String paramValue = optional.get().get("paramValue").toString();
         allMap.put(header, paramValue.contains("-") ? "无效" : paramValue);
     }
     break;
 case "ENGINE_SPEED":
     mapList.stream()
             .filter(m -> "5".equals(m.get("paramId").toString()))
             .findAny()
             .ifPresent(m -> allMap.put(header, m.get("paramValue")));
     break;
6.This class overrides “hashCode()” and should therefore also override “equals()”.

在这里插入图片描述

@Override
public int hashCode(){
    return  Objects.hash(variableName,address);
}

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    VariableDictionaryItemDto that = (VariableDictionaryItemDto) o;
    return Objects.equals(address,that.address) &&
            Objects.equals(variableName,that.variableName);
}

在进行equals与hashcode重写的时候,建议使用idea的自动生成;

7.The return value of “map” must be used.

在这里插入图片描述

  @Transactional(rollbackFor = Exception.class)
  public void delete(Long id) {
      get(id).ifPresent(entity -> {
          delDiagnosBookFile(entity.getDiagnosUrl());
          moniFaultDictDao.deleteById(id);
      });
  }

使用ifPresent(),如果有就做,没有就什么都不做。

2.漏洞修改

1.Do something with the “boolean” value returned by “delete”.

在这里插入图片描述
修改代码:

Boolean boo =file.delete();
if (!boo) {
      log.error("文件删除失败");
}if (!file.delete()) {
	 log.error("文件删除失败");
}
2.Make this “public static VEHICLE_VIN_IDX” field final

在这里插入图片描述
改后:

public static final int IDX_VIN = 0;

使用final进行修饰

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值