public class Test { private String[] name; //如果name的类型为String 就不会有bug提示 public String[] getName() {
return this.name;
} public void setName(String[] name) {
this.name = name;
}
}
///下面是findbug的错误提示:英文不是太好,看的不是很明白
May expose internal representation by incorporating reference to mutable object</b><br/><p> This code stores a reference to an externally mutable object into the
  internal representation of the object.&nbsp;
   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为什么把name的类型改为String后,就没有bug提示了?

解决方案 »

  1.   

    May expose internal representation by incorporating reference to mutable   
    object.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.可能因使引用可指向多个对象而暴露内部存储结构。
    这代码使一个指向外部多个对象的引用指向了一个内部对象存储地址。 
    如果实例被未被信任代码访问或多个对象发生了未经检查的改变就会危及安全性或其它重要属性,
    你需要去做一些不同的事情。存储一个对象的拷贝在许多情况下会是一个更好的方法。
    (翻译若有不对之处请多包涵^^)
      

  2.   

    网上findbug使用的介绍文章中写到,按下面修改findbug就没bug提示了,
    为什么要放到一个临时变量中就可以了?
    public class Test {
    private String[] name; public String[] getName() {
    String[] temp = name;
    return temp;
    } public void setName(String[] name) {
    String[] temp = name;
    this.name = temp;
    }
    }
      

  3.   

    setName 直接修改了本地的数据,而没有进行任何校验,可能会造成某些有用数据的丢失。呵呵,一个警告而已,也许我们的程序就是希望直接修改呢! 呵呵!
    String[]   temp   =   name; 他会检测你做了一些事情,但实际上啥也没做!
      

  4.   

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