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

May expose internal representation by incorporating reference to mutable object

    博客分类:
  • java
阅读更多

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中,当向方法传递一个简单类型时,传递的是一个值(赋值传递) 。当传递的是一个对象时,则按引用传递(把内存中的对象地址赋值给这个对象,我的理解同样是直接把值传递给另一个对象模型相同的对象)

 

 

分享到:
评论

相关推荐

    findbugs常见Bug以及处理办法

    May expose internal representation by incorporating reference to mutable object 描述:调用set方法,修改对象属性,被修改的对象属性是一个可变的对象; May expose internal representation by returning ...

    findbugs:may expose internal representation by returning

    NULL 博文链接:https://futeng.iteye.com/blog/1833449

    findbug 常见异常处理

    May expose internal representation by incorporating reference to mutable object 描述:调用set方法,修改对象属性,被修改的对象属性是一个可变的对象; May expose internal representation by returning ...

    ADO seems to be the ideal way to expose tabular data from yo

    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导出数据

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    cryptography is a package designed to expose cryptographic.zip

    cryptography is a package designed to expose cryptographic.zip

    Learning ECMAScript 6(PACKT,2015)

    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 ...

    expose模板wordpress

    expose模板wordpress 外国收费模板,

    Python_Real-World+Data+Science_A+Course+in+Four+Modules-2016.pdf

    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...

    英文原版-Learning ECMAScript 6 1st Edition

    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 ...

    spring-boot-reference.pdf

    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. ...

    The CUDA Handbook: A Comprehensive Guide to GPU Programming

    Designed to be both a comprehensive reference and a practical cookbook, the text is divided into the following three parts: Part I, Overview, gives high-level descriptions of the hardware and ...

    eAsula.pdf

    IMPORTANT – READ CAREFULLY ... ANY FURTHER USE OF THE SOFTWARE OR OTHER LICENSED MATERIALS BY YOU SUBSEQUENT TO SUCH TERMINATION WILL EXPOSE YOU TO LIABILITY FOR COPYRIGHT INFRINGEMENT AND OTHER CLAIMS.

    Gawk Effective awk Programming

    o prototype an algorithm or design in AWK to get it running quickly and expose problems early. Often, the interpreted performance is adequate and the AWK prototype becomes the product.

    注册表说明

    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 ...

    Troubleshooting.OpenStack.17839

    Next, we will expose you to common errors and problems you may encounter when using the OpenStack Block Storage service (Cinder). We will then examine Heat, the OpenStack Orchestration Service, where...

    Functional Programming in JavaScript(PACKT,2015)

    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 ...

    Developer’s Guide to Web Application Security(syngress安全图书)

    Through a review of recent Web application breaches, the book will expose the prolific methods hackers use to execute Web attacks using common vulnerabilities such as SQL Injection, Cross-Site ...

    PostGIS Cookbook.pdf

    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...

Global site tag (gtag.js) - Google Analytics