原!findbugs:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE 和 OBL_UNSATISFIED_OBLIGATION
改findbogs碰到的两个问题,一个是关于IO流,一个是关于空指针检查异常。
1.NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
前面代码略。。。 File crFile = new File(crFilelocalPath);
if (crFile.exists()&& crFile.isDirectory() && crFile.listFiles() != null
&& crFile.listFiles().length > 0) { //略。。。 } 后面略。。。。
原因分析:
问题出在 crFile.listFiles() ,google到,说是第一个 crFile.listFiles()判断不为null,但是第二个crFile.listFiles()还是可能会为null。(原文:If the first call of listFiles() is != null, that doesn't mean the second call is also != null.)
google原文见:https://sourceforge.net/p/findbugs/bugs/1468/
所以改为
前面略。。 File crFile = new File(crFilelocalPath);
if (crFile.exists() && crFile.isDirectory()) {
File[] files = crFile.listFiles();
if (files != null && files.length > 0) {
//略。。。
}
}
后面略。。。
findbugs就不报错了。。。
2.OBL_UNSATISFIED_OBLIGATION
是关于IO流的关闭
findbugs报错的代码
public static boolean storeFile2LocalPath(String fileName, String localPath, File fileInput) {
boolean success = false;
FileInputStream inputStream = null;
OutputStream os = null;
try {
inputStream = new FileInputStream(fileInput);
//保存到临时文件
byte[] bs = new byte[1024];// 1K的数据缓冲
int len;// 读取到的数据长度
// 输出的文件流保存到本地文件
File tempFile = new File(localPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
String filePath = tempFile.getPath() + File.separator + fileName;
os = new FileOutputStream(filePath);
log.info("storeFile2LocalPath() and filePath = " + filePath);
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
success = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 完毕,关闭所有链接
try {
if(os != null) {
os.close();
os = null;
}
if(inputStream != null){
inputStream.close();
inputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return success;
}
报错如下:
This method may fail to clean up (close, dispose of) a stream,
database object, or other resource requiring an explicit cleanup
operation.
In general, if a method opens a stream or other resource, the method
should use a try/finally block to ensure that the stream or resource
is cleaned up before the method returns.
This bug pattern is essentially the same as the **OS_OPEN_STREAM and
ODR_OPEN_DATABASE_RESOURCE** bug patterns, but is based on a different
(and hopefully better) static analysis technique. We are interested is
getting feedback about the usefulness of this bug pattern. To send
feedback, either: •send email to findbugs@cs.umd.edu •file a bug
report: http://findbugs.sourceforge.net/reportingBugs.html
In particular, the false-positive suppression heuristics for this bug
pattern have not been extensively tuned, so reports about false
positives are helpful to us.
See Weimer and Necula, Finding and Preventing Run-Time Error Handling
Mistakes, for a description of the analysis technique.
原因分析:
连续关闭两个流,在同一个finally快里,若第一个流close失败,出现异常时,会导致第二个流没有关闭。
所以改为如下方式,findbugs不报错了。
//将后面的finally块改为: 再嵌套一个finally块 finally {
// 完毕,关闭所有链接
try {
if (os != null) {
os.close();
os = null;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
原!findbugs:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE 和 OBL_UNSATISFIED_OBLIGATION的更多相关文章
- FindBugs插件的使用手册
安装FindBugs直接查找eclipse的商店,查找spot Bugs 插件,安装即可 完成安装之后重启eclipse,右击项目文件或目录,会发现多了Findbugs的菜单: 使用Findbugs ...
- findBugs英文代号的对照表
findBugs错误英文翻译rule.findbugs.IMSE_DONT_CATCH_IMSE.name=不良实践 - 捕获可疑IllegalMonitorStateException rule.f ...
- 使用SpotBugs/FindBugs进行代码检查
原po:https://blog.csdn.net/zhangb00/article/details/8407070 SpotBugs 介绍 SpotBugs是Findbugs的继任者(Findbug ...
- Findbugs异常总汇
FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...
- findbugs规则
FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...
- FindBugs报错
FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...
- 详解FindBugs的各项检测器 .
FindBugs是一个静态分析工具,在程序不需运行的情况下,分析class文件,将字节码与一组缺陷模式进行对比,试图寻找真正的缺陷或者潜在的性能问题.本文档主要详细说明FindBugs 2.0.3版本 ...
- [Done]FindBugs: boxing/unboxing to parse a primitive
在开发过程中遇到了以下问题: FindBugs: boxing/unboxing to parse a primitive 查看代码(左边是老代码,右边是新的): 问题出在 自动装箱和拆箱的检查. 参 ...
- MyEclipse使用总结——将原有的MyEclipse中的项目转成maven项目[转]
前面一篇文章中我们了解了 在myeclipse中新建Maven框架的web项目 那么如果我们原来有一些项目现在想转成maven项目应该怎么做呢 我收集到了三种思路: 一.新建一个maven项目,把原项 ...
随机推荐
- Java - NIO基础
1. 概述 现在使用NIO的场景越来越多,很多技术框架都使用NIO技术,比如Tomcat,Jetty,Netty等. 传统IO基于字节流和字符流进行操作,而NIO基于Channel和Buffer进行操 ...
- Missing iOS Distribution signing identity for …, 在打包的时候发现证书过期了。
今天早上 上班发现钥匙串中的全部证书 都 提示此证书签发者无效 Thanks for bringing this to the attention of the community and apolo ...
- iOS直播-基于RTMP的视频推送
iOS直播-基于RTMP的视频推送 所谓的视频推送就是把摄像头和麦克风捕获到视频和音频推送到直播服务器上.我们这里使用推送协议是RTMP协议. 扩展:腾讯直播平台,阿里直播平台,百度直播平台提供均为R ...
- Jquery js框架使用
jquery 众所周知 ,强大的 js框架 自己使用的一些笔记 //1.json格式定义方法 var product_obj={ check_init:function(){ ...
- 解决微信小程序中Date.parse()获取时间戳IOS不兼容的问题(IOS为NaN的问题)
前端同事在做微信小程序时发现IOS获取的时间戳为空的问题,后来通过跟踪发现,原来是因为IOS系统不支持2017-01-01格式的时间导致的, var mydata = '2017-01-01 11:0 ...
- js 数组取出最大值最小值和平均值的方法
1.直接遍历数组 ,,,,,,,]; ]; ;i<arr.length;i++){ if(max<arr[i]) max=arr[i]; } 2.借用Math的方法 ,,,,,,,]; v ...
- C# mvc中动态压缩文件发送给前端
前言 帮朋友解决一个C#中发送压缩文件的的问题,因为感觉解释起来更麻烦,就直接用几分钟时间写了个小Demo.本着"走过路过"不错过的原则,也给记录一下. 1.前端代码 非常简单的一 ...
- python post get请求
安装 Requests pip install requests import requests requests.get('https://github.com/timeline.json') 使用 ...
- json中的日期格式转换(扩展new date()显示格式)
在java spring mvc 开发过程中,通过json 格式,向前端传递数据,日期格式发生了转变, 在前台数据展示时,要进行一定格式的转换才能正常显示: 我在开发中使用了easy ui 和my ...
- RelativeSource.TemplatedParent 属性wpf
今天看到这一句代码时候,自己只是知道绑定了,可是不知道绑定了什么啊 就去查了一下,后来说的好像是绑定的TemplateParent返回的 一个值.可是这是为什么呢, 有的说是绑定的是一个资源. 下面有 ...