eclipse插件FindBugs各种bug描述及解决方法

1 )原代码如下:

protected String[] a = null;

public void test(String[] str){

    this.a = str;

}

findbugs描述为:

This code stores a reference to an externally mutable object into the internal representation of the object. If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Storing a copy of the object is better approach in many situations.

网上翻译如下:

可能因使引用可指向多个对象而暴露内部存储结构。 
这代码使一个指向外部多个对象的引用指向了一个内部对象存储地址。 
如果实例被未被信任代码访问或多个对象发生了未经检查的改变就会危及安全性或其它重要属性, 
你需要去做一些不同的事情。存储一个对象的拷贝在许多情况下会是一个更好的方法。

修改如下:

public void test(String[] str){

    if(str!=null)

    this.a = str.clone();

}

--------------------------------------------------------------------------------

2 )在bean中定义数组类型的bug

[参考]http://topic.csdn.net/u/20080115/20/c8893ce0-5546-4762-97bb-9b00d10885cc.html

原代码:

private String[] name;

public String[] getName() { 
return name; 
}

public void setName(String[] name) { 
this.name = name; 
}

bug描述:

[EI] May expose internal representation by returning reference to mutable object [EI_EXPOSE_REP]

解决:

private String[] name;

public String[] getName() { 
String[] temp = name; 
return temp; 
}

public void setName(String[] name) { 
String[] temp = name; 
this.name = temp; 
}

说明:

所有容器类型如ArrayList和数组类型,如果你都自动生成get set,都会有这个警告。 
    这个警告的主要目的是:一般的get set直接把此对象中某一容器的引用放到外部,可以随便更改,违反了封装的原则,至于那个temp的方法,由于不是直接对内部容器进行操作,故没有警告,但没有实际意义,自己知道即可。

Returning a reference to a mutable object value stored in one of the object's fields exposes the internal representation of the object. If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Returning a new copy of the object is better approach in many situations.

--------------------------------------------------------------------------------

3) 序列化问题

源码:

private Obj[] obj;

public void getObj(){

Obj[] tep = obj;

return tep;

}

public Obj[] setObj(Obj[] o){

Obj[] tep = o;

this.obj = tep;

}

bug描述:

This Serializable class defines a non-primitive instance field which is neither transient, Serializable, or java.lang.Object, and does not appear to implement the Externalizable interface or the readObject() and writeObject() methods. Objects of this class will not be deserialized correctly if a non-Serializable object is stored in this field.

修改:

public class Obj implements Serializable {

...

}

4 ) new Integer(int) 和 Integer.valueOf(int)

bug描述:

[Bx] Method invokes inefficient Number constructor; use static valueOf instead [DM_NUMBER_CTOR]

Using new Integer(int) is guaranteed to always result in a new object whereas Integer.valueOf(int) allows caching of values to be done by the compiler, class library, or JVM. Using of cached values avoids object allocation and the code will be faster.

说明:

[参考]http://www.cnblogs.com/hyddd/articles/1391318.html

FindBugs推荐使用Integer.ValueOf(int)代替new Integer(int),因为这样可以提高性能。如果当你的int值介于-128~127时,Integer.ValueOf(int)的效率比Integer(int)快大约3.5倍。

下面看看JDK的源码,看看到Integer.ValueOf(int)里面做了什么优化:

public static Integer valueOf(int i) {
  final int offset = 128;
  if (i >= -128 && i <= 127) { // must cache
    return IntegerCache.cache[i + offset];
   }
  return new Integer(i);
}

private static class IntegerCache {
  private IntegerCache(){}
    
  static final Integer cache[] = new Integer[-(-128) + 127 + 1];
  static {
  for(int i = 0; i < cache.length; i++)
      cache = new Integer(i - 128);
   }
}

从源代码可以知道,ValueOf对-128~127这256个值做了缓存(IntegerCache),如果int值的范围是:-128~127,在ValueOf(int)时,他会直接返回IntegerCache的缓存给你。

所以你会看到这样的一个现象:

public static void main(String []args) {
      Integer a = 100;
      Integer b = 100;
      System.out.println(a==b);

Integer c = new Integer(100);
      Integer d = new Integer(100);
      System.out.println(c==d);
}

结果是:

true
false

因为:java在编译的时候 Integer a = 100; 被翻译成-> Integer a = Integer.valueOf(100);,所以a和b得到都是一个Cache对象,并且是同一个!而c和d是新创建的两个不同的对象,所以c自然不等于d。

再看看这段代码:

public static void main(String args[]) throws Exception{
         Integer a = 100;
         Integer b = a;
         a = a + 1;  //或者a++;
         System.out.println(a==b);
}

结果是:false

因为在对a操作时(a=a+1或者a++),a重新创建了一个对象,而b对应的还是缓存里的100,所以输出的结果为false。

--------------------------------------------------------------------------------

5) toString() 和 String

源码:

return a.toString();

bug描述

[Dm] Method invokes toString() method on a String [DM_STRING_TOSTRING]
Calling String.toString() is just a redundant operation. Just use the String.

修改为:

return (String) a;

原文地址:http://www.blogjava.net/pure/archive/2009/09/30/296989.html

FindBugs 恶意代码(may expose internal representation),序列化(defines non-transient non-serializable )错误解决方法相关推荐

  1. findbugs:may expose internal representation by ret

    2019独角兽企业重金招聘Python工程师标准>>> findbugs:1. *** getXXX() may expose internal representation by ...

  2. Xcode真机调试中“There was an internal API error“错误解决方法

    Xcode真机调试中"There was an internal API error"错误解决方法 参考文章: (1)Xcode真机调试中"There was an in ...

  3. 提交代码出现 Push to origin/master was rejected 错误解决方法

    提交代码出现 Push to origin/master was rejected 错误解决方法 参考文章: (1)提交代码出现 Push to origin/master was rejected ...

  4. win10安装PS和AI后报代码为16的错误解决方法

    win10安装PS和AI后报代码为16的错误解决方法 一.总结 一句话总结:修改兼容性和以管理员方式运行就可以了 修改兼容性 以管理员身份运行 二.PS和AI安装后报代码为16的错误解决方法介绍(转) ...

  5. PS和AI安装后报代码为16的错误解决方法

    PS和AI安装后报代码为16的错误解决方法 参考文章: (1)PS和AI安装后报代码为16的错误解决方法 (2)https://www.cnblogs.com/lotuses/p/10144397.h ...

  6. fatal error C1001: INTERNAL COMPILER ERROR(compiler file 'msc1.cpp', line 1786)解决方法

    有时会碰到奇怪的编译错误 fatal error C1001: INTERNAL COMPILER ERROR(compiler file 'msc1.cpp', line 1786) 致命错误C10 ...

  7. 本地wamp的Internal Server Error错误解决方法

    一.本地wamp下调试url重写,加入htaccess文件后提示:500 Internal Server Error ...,而删除这个文件网站又可以正常访问,其实就是没有开启url重写的功能.开启一 ...

  8. VS2005 中关于“LC.EXE已退出,代码为 -1”的错误解决方法。

    最近在用VS2005做东西时遇到一个比较莫名的问题,当在自己的解决方案中引用了一系列的第三方软件或者控件后,当我获取了整个程序的最新版本,对整个工程再在本地进行编译会出现"LC.EXE已退出 ...

  9. php代码运行后空白什么原因,PHP空白页面常见原因及解决方法

    编写PHP,难免会出现错误.其实出现错误也不难解决,最难解决的是出现空白页面.大家想想看,若编写PHP出现错误,可以根据错误的提示来改正,倘若PHP什么也不给你显示,那岂不是让编写者困挠不以?下面,我 ...

最新文章

  1. os.path.dirname(path) 返回文件的绝对路径
  2. sql 给数据库表 字段 添加注释
  3. Qt工作笔记-使用toVariant().toMap()分割Json文件(666解析法)
  4. 信息学奥赛一本通(1263:【例9.7】友好城市)
  5. SpannableStringUtil实现丰富文字效果
  6. spark加载数据的方式
  7. python怎么设置回文数_Python中的回文数
  8. 青龙脚本合集(不定期更新版)
  9. 微信扫一扫下载apk解决方案
  10. 苹果6s为什么连接不上服务器未响应,iPhone6s/6s plus连接iTunes没反应怎么办?苹果手机无法连接iTunes的解决方法...
  11. java 串口 中文乱码_Java 实现 POS 打印机无驱串口打印(解决中文乱码)
  12. xp怎么删除计算机管理员用户名和密码,Windows XP 的 Administrator 超级管理员密码忘记了,如何清除?...
  13. b区计算机复试国家线,今年调剂太恐怖 B区考研分数线竟比A区高?
  14. stm32启用内部晶振(stm32设置外部晶振)
  15. 八款电脑自动校时工具推荐
  16. 笔记本使用计算机的快捷键是什么,笔记本电脑截屏的快捷键是什么
  17. katana(武士刀)setuid提权
  18. pureMVC的争议,说说缺点
  19. 基于USB2.0的视频图像处理芯片实现方案
  20. 华南主板超频设置图解_AMD用户不会超频不要紧,开启这个功能免费的性能提升...

热门文章

  1. 最全面的Socket使用解析
  2. 摩根森MSort光选分选机介绍
  3. python控制苹果手机流量_配置ubunto 流量使用限制 python 实现简单 http server
  4. 花旗:为什么有限外包
  5. java多态性练习题---主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5 主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5
  6. GitLab 查看服务器端口地址忘记管理员密码
  7. 是什么微信社群营销模式仅用3个微信半年裂变2000000付费会员的?
  8. Photoshop-颜色通道-原理(转)
  9. 【沐风老师答疑系列】3DMAX水滴形形状建模
  10. SpringDataJPA多表联合查询