Principle

  1. It is essential to make a defensive copy of each mutable parameter to the constructor.
  2. Defensive copies are made before checking the validity of the parameters (Item 38), and the validity check is performed on the copies rather than on the originals.  

    // Repaired constructor - makes defensive copies of parameters

    public Period(Date start, Date end) {

    this.start = new Date(start.getTime());

    this.end = new Date(end.getTime());

    //Make the defensive copies of the parameters before using them.

    if (this.start.compareTo(this.end) > 0)

    throw new IllegalArgumentException(start +" after "+ end);

    }

    TOCTOU = time of check/ time of use.

  3. Do not use the clone method to make a defensive copy of a parameter whose type is subclassable by untrusted parties.
  4. Return defensive copies of mutable internal fields.  

    // Repaired accessors - make defensive copies of internal fields

    public Date start() {

    return new Date(start.getTime());

    }

    public Date end() {

    return new Date(end.getTime());

    }

Summary

If a class has mutable components that it gets from or returns to its clients, the class must defensively copy these components. If the cost of the copy would be prohibitive and the class trusts its clients not to modify the components inappropriately, then the defensive copy may be replaced by documentation outlining the client's responsibility not to modify the affected components.

Effective Java 39 Make defensive copies when needed的更多相关文章

  1. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  2. 《Effective Java》读书笔记 - 7.方法

    Chapter 7 Methods Item 38: Check parameters for validity 直接举例吧: /** * ...其他的被我省略了 * @throws Arithmet ...

  3. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  4. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  5. Effective Java 第三版——39. 注解优于命名模式

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  6. Effective Java 创建和销毁对象

    <Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一 ...

  7. Effective Java 第三版——17. 最小化可变性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. Effective Java 76 Write readObject methods defensively

    Principle readObject method is effectively another public constructor, and it demands all of the sam ...

  9. [Effective Java]第八章 通用程序设计

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

随机推荐

  1. Github教程(1)

    Git基本命令 git diff:查看工作树,暂存区,最新提交之间的差别 举例: 在README.MD中增加一行 ## This is the subtitle 执行git diff 命令,查看当前工 ...

  2. 【转】MSMQ消息队列安装

    一.Windows 7安装.管理消息队列1.安装消息队列   执行用户必须要有本地 Administrators 组中的成员身份,或等效身份.   具体步骤:    开始—>控制面板—>程 ...

  3. css中position与z-index

    position属性 在css中,position属性用来控制元素的位置信息,其取值共有4种,即static.relative.absolute.fixed. 静态定位(static) 若没有指定po ...

  4. C# 之屏幕找图

    引言 最近,由于工作上的某些原因,又要写类似于外挂的程序,又要用到一个屏幕找图功能,很多程序(eg:按键精灵)都提供了类似的功能,其实在这之前,我也查找过很多类似的C#方法,因为之前有一个试过没有用得 ...

  5. java操作小技巧,遇到过的会一直更新,方便查找

    1.<c:forEach>可以循环map array List 2.操纵数组,不知道类型的情况下,不需要判断数组类型,直接用反射,arrays.Class.isArrays() 获取数组长 ...

  6. python flask应用部署

    失败版本:flask+uwsgi ini配置文件 [uwsgi] callable = app ;//程序内启用的application变量名 home = /home/jcuan/code/pyth ...

  7. phpcms v9 升级视频云问题推荐位不能添加

    因为使用的是v9的早期版本,后来升级的时候没敢升级数据库,直接使用了老的数据库结构,造成[推荐位]添加不能使用,报告没有thumb列. 查看数据库果然没有,没办法要么添加相关的列,要么禁用上传缩略图. ...

  8. .NET Core创建一个控制台(Console)程序

    .NET Core版本:1.0.0-rc2 Visual Studio版本:Microsoft Visual Studio Community 2015 Update 2 开发及运行平台:Window ...

  9. 将32位MD5摘要串转换为128位二进制字符串

    将32为MD5摘要串转换为128位二进制字符串: /// <summary> /// 将字符串转成二进制 /// </summary> /// <param name=& ...

  10. [Tool] 透过PowerPoint Online在部落格文章里内嵌简报

    [Tool] 透过PowerPoint Online在部落格文章里内嵌简报 前言 讲课的时候,用PowerPoint做简报,好像已经成了讲课的惯例.而在课后,将课堂简报整理成部落格的文章,如果单纯是在 ...