FindBugs分析bug
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.
大概意思:
这个代码存储引用一个外部的可变对象的内在表现的对象。
如果情况下能够用不可信赖的代码,和未改变的可变对象会削弱安全或其他重要的性质,你需要做一些不同的事情。
存储拷贝一个对象,在许多情况下有更好的方法。
package test; import java.util.Date; public class Demo { /** * @param args */ public static void main(String[] args) { Test test= new Test(); Date now = new Date(); test.setCreateDate(now); System.out.println(test.getCreateDate()); now.setYear(210);//问题所在! System.out.println(test.getCreateDate()); } } package test; import java.util.Date; public class Test { private Date createDate ; public void setCreateDate(Date createDate) { // Findbugs提示下一行 this.createDate = createDate; // this.createDate = createDate==null?null:(Date)createDate.clone(); // 先判断一下是否为null,如果为判断之前就为null,强转(Date)一定会报异常 } public Date getCreateDate() { return createDate; } }
修改一个对象(now),可能会引起其他对象(test)的修改,即:test=getCreateDate();当赋值完毕后,如果需要修改now的值,那么就有问题了!
可以修改成注释部分 ,不直接返回this.test,而是返回一个this.test的拷贝。
直接运行一下,在解开上的注释,再试试,看看结果!
令人深思啊!
延伸:
Java复制并按值传递引用,而非对象本身。
1.基本类型作为参数传递时,是传递值的拷贝,无论你怎么改变这个拷贝,原值是不会改变的
2.在Java中对象作为参数传递时,是把对象在内存中的地址拷贝了一份传给了参数,相当于赋值传递。 当原引用指向原对象时,方法就能改变原对象的值。如果引用仅是一个副本,则交换函数失效。引用的副本进行了交换,而非原引用。方法被调用后,得到的是没有进行交换的原引用。可以在函数体外对原引用进行交换,以实现成功的交换。
总结一下,在JAVA中,当向方法传递一个简单类型时,传递的是一个值(赋值传递) 。当传递的是一个对象时,则按引用传递(把内存中的对象地址赋值给这个对象,我的理解同样是直接把值传递给另一个对象模型相同的对象) 。
相关推荐
- **May expose internal representation by incorporating reference to mutable object**:调用set方法,修改对象属性,被修改的对象属性是一个可变的对象。 - **描述**:这通常意味着通过公共接口暴露了内部状态...
public Object getContent() { return content; // FindBugs警告就在这里 } } ``` 这里的`getContent()`方法直接返回了私有字段`content`的引用。如果外部代码捕获了这个引用并进行修改,例如: ```java ...
May expose internal representation by incorporating reference to mutable object 描述:调用set方法,修改对象属性,被修改的对象属性是一个可变的对象; May expose internal representation by returning ...
JQuery Tools系列中的Expose是一个非常实用的工具,它允许用户通过半透明层突出显示页面上的某个部分。这项技术通常用于突出页面上的某些信息或者在用户操作时提示用户注意特定内容。以下是对JQuery Tools Expose...
ADO seems to be the ideal way to expose tabular data from your own COM objects and the ATL OLE DB Provider templates can help!使用ADO导出数据
The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...
【expose模板WordPress详解】 WordPress是一款全球广泛应用的内容管理系统(CMS),以其开源、免费和高度可定制性著称。"expose"是针对WordPress设计的一款专业主题模板,它源自国外并带有收费性质,这意味着该模板...
Moving on, it will teach you how to create reflection objects, use it to expose hidden object properties, and test the security of these objects. Next, the book provides use case scenarios of meta ...
包含以下java源文件: ...com.google.gson.internal.ObjectConstructor.class com.google.gson.internal.Primitives.class com.google.gson.internal.Streams.class com.google.gson.internal.UnsafeAllocator.class ...
I love to make references to the real world when I teach coding; I believe they help people retain the concepts better. However, now is the time to be a bit more rigorous and see what coding is from...
Next, it will teach you how to write asynchronous code in a synchronous style using ES6.Moving on, it will teach you how to create reflection objects, use it to expose hidden object properties, and ...
Connecting to Elasticsearch by Using Spring Data 30.6.3. Spring Data Elasticsearch Repositories 30.7. Cassandra 30.7.1. Connecting to Cassandra 30.7.2. Spring Data Cassandra Repositories 30.8. ...
this document does not provide you with any legal rights to any intellectual property in any microsoft product you may copy and use this document for your internal reference purposes ó 2013 ...
The current system might put too much pressure on copyright holders, who struggle to monitor vast amounts of user-generated content, or it may expose video sharing websites to potential legal risks ...
It's followed by a comprehensive roundup of functional programming libraries for JavaScript that minimizes the burden of digging deep into JavaScript to expose a set of tools that makes functional ...
"my expose"是一款针对Windows操作系统设计的实用小工具,它借鉴了苹果Mac OS中的Exposé功能,旨在帮助用户更高效地管理和导航桌面任务栏。在Windows系统中,当我们打开多个窗口时,桌面可能会变得杂乱无章,寻找...
Even for experienced users, this book will serve as a great source of reference by providing new ways of working with PostGIS through the book’s easy-to-follow approach. This hands-on guide looks...