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

2024-01-10 16:32

本文主要是介绍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;

***************************************************************************

未解决bug

1、

[DMI] Code contains a hard coded reference to an absolute pathname [DMI_HARDCODED_ABSOLUTE_FILENAME]

This code constructs a File object using a hard coded to an absolute pathname (e.g., new File("/home/dannyc/workspace/j2ee/src/share/com/sun/enterprise/deployment");

这篇关于eclipse插件FindBugs各种bug描述及解决方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/591406

相关文章

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

电脑死机无反应怎么强制重启? 一文读懂方法及注意事项

《电脑死机无反应怎么强制重启?一文读懂方法及注意事项》在日常使用电脑的过程中,我们难免会遇到电脑无法正常启动的情况,本文将详细介绍几种常见的电脑强制开机方法,并探讨在强制开机后应注意的事项,以及如何... 在日常生活和工作中,我们经常会遇到电脑突然无反应的情况,这时候强制重启就成了解决问题的“救命稻草”。那

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

Python中的魔术方法__new__详解

《Python中的魔术方法__new__详解》:本文主要介绍Python中的魔术方法__new__的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、核心意义与机制1.1 构造过程原理1.2 与 __init__ 对比二、核心功能解析2.1 核心能力2.2