大家帮我看下findbug提示的这个bug是什么意思?

TheLost 2008-01-15 08:37:49
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. 
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提示了?
...全文
594 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyberpeng 2008-01-17
  • 打赏
  • 举报
回复
楼上的解释赞。
学习了。
yuanqingfei 2008-01-16
  • 打赏
  • 举报
回复
1、变为String不会有问题,是因为String是unmutable型的,也就是一生成后就永远不会变的。
2、所有容器类型如ArrayList和数组类型,如果你都自动生成get set,都会有这个警告。
3、这个警告的主要目的是:一般的get set直接把此对象中某一容器的引用放到外部,可以随便更改,违反了封装的原则,至于那个temp的方法,由于不是直接对内部容器进行操作,故没有警告,但没有实际意义,自己知道即可。
老紫竹 2008-01-16
  • 打赏
  • 举报
回复
setName 直接修改了本地的数据,而没有进行任何校验,可能会造成某些有用数据的丢失。

呵呵,一个警告而已,也许我们的程序就是希望直接修改呢! 呵呵!
String[]   temp   =   name; 


他会检测你做了一些事情,但实际上啥也没做!
TheLost 2008-01-15
  • 打赏
  • 举报
回复
网上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;
}
}
dracularking 2008-01-15
  • 打赏
  • 举报
回复
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.

可能因使引用可指向多个对象而暴露内部存储结构。
这代码使一个指向外部多个对象的引用指向了一个内部对象存储地址。
如果实例被未被信任代码访问或多个对象发生了未经检查的改变就会危及安全性或其它重要属性,
你需要去做一些不同的事情。存储一个对象的拷贝在许多情况下会是一个更好的方法。
(翻译若有不对之处请多包涵^^)
IamHades 2008-01-15
  • 打赏
  • 举报
回复
数组未初始化吧.

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧