1.bug修改
文章目录
- 1.bug修改
- 1.Introduce a new variable instead of reusing the parameter "vin".
- 2. A "NullPointerException" could be thrown; "command" is nullable here.
- 3.Change this condition so that it does not always evaluate to "false"
- 4.Use another way to initialize this instance.
- 5.Call "Optional#isPresent()" before accessing the value.
- 6.This class overrides "hashCode()" and should therefore also override "equals()".
- 7.The return value of "map" must be used.
- 2.漏洞修改
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进行修饰