77范文网 - 专业文章范例文档资料分享平台

findbugs检测提示详解

来源:网络收集 时间:2019-01-07 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

findbugs检测提示详解

1、Comparison of String objects using == or !=

例,override equals方法时容易犯错 if(this.topic != key.getTopic()) return false;

2、Dead store to newStatusRecord

定义局部变量后没有引用

3、Invocation of toString on values

直接调用数组的toString方法

public Query createQuery(String hql, Object values[],Session session){

logger.debug(values); logger.debug((new

StringBuilder()).append(\\}

正确的例子,调用Arrays.toString()和Arrays.deepToString()方法。 import java.util.Arrays; class A{ }

class B{ @Override

public String toString() { return \ } }

public class Test {

public static void main(String[] args) {

Object [] a = {new Integer(0),new Boolean(true),true,new A(),new B()};

Object[][]b ={{new A(),new B()},{new A(),new B()},{new A(),new B()}}; System.out.println(Arrays.deepToString(b)); } }

4、ignores exceptional return value of

java.io.File.mkdirs()

忽略了返回值,应当含有返回值

public void initFolder() { if (!exitDir.isDirectory()) { exitDir.mkdirs();

logger.info(\ } }

This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the File.delete() method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value.

5、不使用new String()定义空的字符串

String alarmCodeCond = new String(); 应当

String alarmCodeCond = \

6、invokes inefficient new Short(short) constructor; use Short.valueOf(short) instead JVM缓存数字常量

Short aShort = new Short(12); 应当

Short aShort = Short.valueOf(12);

7、方法命名习惯,首字母小写

The method name LaneHandShakeService(Short) doesn't start with a lower case letter

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

8、一个primtive的类型的值经过box后马上unbox

Primitive value is boxed then unboxed to perform primitive coercion

exitRecord.setEnOperatorId(new

Long(transactRecord.getEnoperatorID()).intValue()); 应当直接强制类型转换

exitRecord.setEnOperatorId((int)transactRecord.getEnoperatorID());

9、Call to equals() comparing different types 使用equals()方法比较不同的类,

反例

StringBuilder builder = new StringBuilder(\ String string = \ builder.equals(string);

10、Check for oddness that won't work for negative numbers

检查奇数的方法:

反例

if (i % 2 == 1) { //... }

The code uses x % 2 == 1 to check to see if a value is odd, but this won't work for negative numbers (e.g., (-5) % 2 == -1). If this code is intending to check for oddness, consider using x & 1 == 1, or x % 2 != 0.

11、Load of known null value,null值的不当使用

反例:

if (devIds == null && devIds.size() == 0) { //... }

if (null != tempList || tempList.size() != 0) { //... }

if (batchNo == null) {

throw new Exception(\ + \ }

12、Method call passes null for nonnull parameter

对参数为null的情况没做处理

public void method1() { String ip = null; try {

ip = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); }

long ipCount = countIpAddress(ip); // 可能会传入空引用 }

long countIpAddress(String ip) { long ipNum = 0;

String[] ipArray = ip.split(\} 修改后:

public void method1() { String ip = null; try {

ip = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); }

long ipCount = countIpAddress(ip); // 可能会传入空引用 }

long countIpAddress(String ip) { long ipNum = 0; if (ip == null) {

return 0; //或者抛出异常 }

String[] ipArray = ip.split(\

}

注意:函数入口需要交验入参的合法性。

//...

//... //...

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库findbugs检测提示详解在线全文阅读。

findbugs检测提示详解.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/409688.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: