`
小网客
  • 浏览: 1250986 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • Java
阅读更多

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

    Eclipse 插件 FindBugs 是一款用于静态代码分析的工具,它可以帮助开发者在程序运行前发现潜在的错误和不良编程习惯。FindBugs 能够检查 Java 代码,并识别出可能的问题,如空指针异常、未初始化的变量、线程安全...

    eclipse插件findBugs

    "Eclipse插件FindBugs"是一个用于静态代码分析的强大工具,旨在帮助开发者在运行程序之前发现潜在的bug和代码质量问题。Eclipse是一个广泛使用的Java集成开发环境(IDE),而FindBugs插件则为其增添了一个重要的功能...

    eclipse插件FindBugs错误分析说明

    Eclipse插件FindBugs是一个静态代码分析工具,它通过扫描Java代码,检测程序中的潜在bug、代码异味以及不符合编码规范的地方。以下是一些常见FindBugs错误的解释和修改建议。 1. EC_UNRELATED_TYPESBug 错误描述:...

    eclipse插件 findBugs 最新版

    **Eclipse插件FindBugs详解** FindBugs是一款强大的静态代码分析工具,专为Java开发者设计,用于在代码执行前检测潜在的错误和不良编程习惯。它通过深入解析字节码来查找可能的问题,而无需实际运行程序。Eclipse...

    详解eclipse插件findbugs新规则的开发过程

    《详解Eclipse插件FindBugs新规则的开发过程》 在软件开发中,代码质量是衡量项目健康度的重要指标之一。FindBugs是一款强大的静态代码分析工具,它能够检测Java代码中的潜在错误,帮助开发者在运行时之前发现并...

    findbugs eclipse插件

    **findbugs eclipse插件** 是一个非常重要的工具,主要用于帮助开发者在Eclipse集成开发环境中发现潜在的代码问题和错误。FindBugs是一款静态代码分析工具,它可以分析Java代码,找出可能存在的bug、不良编程习惯...

    android eclipse 插件 findbugs 3.0 linux

    **Android与Eclipse插件FindBugs 3.0在Linux环境中的应用** FindBugs是一款强大的静态代码分析工具,专用于检测Java程序中的潜在错误和不良编程实践。在Android开发环境中,它作为Eclipse插件,能为开发者提供一个...

    eclipse中findbugs插件

    FindBugs插件是将FindBugs工具集成到Eclipse中的一个扩展,使得用户在开发过程中可以方便地检查代码问题。 安装FindBugs插件的过程相对简单。首先,你需要下载`edu.umd.cs.findbugs.plugin.eclipse_3.0.1.20150306-...

    findbugs 最新eclipse插件

    **findbugs最新Eclipse插件**是用于静态代码分析的工具,它可以帮助开发者在编码阶段发现潜在的错误和不良编程习惯。FindBugs是开源项目,由University of Maryland开发并维护,广泛应用于Java应用程序的质量保证...

    eclipse 插件 findBugs

    **Eclipse插件FindBugs详解** 在软件开发过程中,代码质量是衡量项目稳定性和可维护性的重要指标。为了确保代码的健壮性,开发者通常需要进行严谨的代码审查和测试。Eclipse插件FindBugs就是这样一个强大的静态代码...

    Eclipse 插件 findbugs

    FindBugs不仅可以作为Eclipse插件使用,还可以与Maven、Gradle等构建工具集成,实现自动化代码质量检查。 ### 6. **FindBugs的替代品** 随着Java社区的发展,FindBugs已不再更新,其开发团队转向了新的项目叫做...

    checkstyle和findBugs插件 eclipse插件

    本文将详细介绍两个重要的Eclipse插件:Checkstyle和FindBugs。 1. Checkstyle Checkstyle是一款静态代码分析工具,用于检测Java源代码中的潜在问题和不符合编码规范的地方。通过定义一套检查规则,Checkstyle可以...

    ecplise插件findbugs2.0

    在 2.0 版本中,FindBugs 支持 JDK 1.6,并且能够与 Eclipse 3.2 及以上版本无缝集成,为开发者提供了更为便捷的代码质量检查环境。 **1. FindBugs 的功能** FindBugs 通过分析字节码而非源代码,能够发现多种类型...

    Eclipse集成findBugs步骤.doc

    尽管文档中提到Eclipse 3.3及旧版本的操作,但FindBugs通常与Eclipse的各个版本保持兼容,包括Eclipse 3.4及以上。不过,不同版本的Eclipse可能会有一些界面或功能上的差异,但基本的安装和使用流程是一致的。 总之...

    findbugs插件eclipse

    Eclipse是一个广泛使用的开源IDE,拥有丰富的插件生态系统,FindBugs就是其中之一,它的目标是帮助开发者在代码执行之前就找出可能存在的bug,防止缺陷进入生产环境。 FindBugs插件的工作原理是通过分析字节码来...

    Eclipse集成findBugs步骤

    2. **在Eclipse中安装FindBugs插件** - 方法一:下载Eclipse plugin版本,解压并放入Eclipse的Plugin目录下,重启Eclipse。 - 方法二:通过Eclipse Marketplace搜索并安装,按照提示步骤完成安装。 3. **在...

    findbugs在eclipse中安装及使用

    FindBugs 在 Eclipse 中的安装及使用 FindBugs 是一个静态分析工具,检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题。FindBugs 可以在不实际运行程序的情况对软件进行分析,检测出一些隐藏...

    Eclipse集成findBugs步骤.pdf

    对于Eclipse 3.4及以上版本,尽管界面可能略有不同,但集成和使用FindBugs的基本步骤是一致的。 总的来说,Eclipse集成FindBugs能有效提升开发效率,提前发现潜在的代码问题,避免在后期调试中花费更多时间和精力。...

Global site tag (gtag.js) - Google Analytics