不应直接存储或返回可变成员 Mutable members should not be stored or returned directly
Mutable objects are those whose state can be changed. For instance, an array is mutable, but a String is not. Mutable class members should never be returned to a caller or accepted and stored directly. Doing so leaves you vulnerable to unexpected changes in your class state.
Instead use an unmodifiable
Collection(viaCollections.unmodifiableCollection,Collections.unmodifiableList, ...) or make a copy of the mutable object, and store or return copy instead.This rule checks that arrays, collections and Dates are not stored or returned directly.
可变对象是那些状态可以改变的对象。
例如,数组是可变的,但String不是。 永远不应将可变类成员返回给调用者或直接接受和存储。 这样做会使您容易受到类状态的意外更改的影响。
而是使用不可修改的Collection(通过Collections.unmodifiableCollection,Collections.unmodifiableList,...)或制作可变对象的副本,然后存储或返回副本。
此规则检查是否未直接存储或返回数组,集合和日期。
不合格的代码如下:
class A {
private String [] strings;
public A () {
strings = new String[]{"first", "second"};
}
public String [] getStrings() {
return strings; // Noncompliant
}
public void setStrings(String [] strings) {
this.strings = strings; // Noncompliant
}
}
public class B {
private A a = new A(); // At this point a.strings = {"first", "second"};
public void wreakHavoc() {
a.getStrings()[0] = "yellow"; // a.strings = {"yellow", "second"};
}
}
合格代码解决方案
class A {
private String [] strings;
public A () {
strings = new String[]{"first", "second"};
}
public String [] getStrings() {
return strings.clone();
}
public void setStrings(String [] strings) {
this.strings = strings.clone();
}
}
public class B {
private A a = new A(); // At this point a.strings = {"first", "second"};
public void wreakHavoc() {
a.getStrings()[0] = "yellow"; // a.strings = {"first", "second"};
}
}
不应直接存储或返回可变成员 Mutable members should not be stored or returned directly的更多相关文章
- Java 的不可变类 (IMMUTABLE CLASS) 和 可变类 (MUTABLE CLASS)
一.简单定义不可变对象(Immutable Objects)即对象一旦被创建,它的状态(对象的数据,也即对象属性值)就不能改变,反之即为可变对象(Mutable Objects).当满足以下条件时,对 ...
- ibatis.net调用oracle存储过返回游标SYS_REFCURSOR结果集
最近在用ibatis.net框架和oracle 11g开发一套程序.其中有一个需求就是通过存储过程,查询指定条件的数据集. 但是在开发的过程中遇到了问题,问题如下: 1.如何通过ibatis.net执 ...
- Java 集合存储都返回什么?
1.抛出一个类 package com.math.spring; import com.google.common.collect.Lists; import com.google.common.co ...
- iOS 关于objectForKey返回类型是不是mutable
以前看NSUserDefault时,记住了那里的objectForKey返回的一定是immutable的对象.现在有点弄混了,其实,NSObject的objectForKey方法没有这个限制,是可以返 ...
- 【转】C++成员函数的存储方式
[转] http://c.biancheng.net/cpp/biancheng/view/187.html 用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据 ...
- C++成员函数在内存中的存储方式
用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间.按理说,如果用同一个类定义了10个对象,那么就需要分别为10个对象的数据和函数代码分 ...
- const成员函数可以将非const指针作为返回值吗?
先给出一段代码 class A { int *x; public: int *f() const { return x; } }; 成员函数f返回指向私有成员 x 的非常量指针,我认为这会修改成员x ...
- c++成员函数的存储方式---11
原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 成员函数属于一个类的成员,出现再类体中.可以被指定为公有,私有或受保护的. 1.在类外面定义成员函数时, ...
- C++成员函数的存储方式
用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间. 按理说,如果用同一个类定义了10个对象,那么就需要分别为10个对象的数据和函数代码 ...
随机推荐
- C# 将一个string数组转换为int数组
int[] channelCIdArr = Array.ConvertAll(channelIdStr.Split(','),s=>int.Parse(s));
- GoldenGate 12c + Oracle 12c Multitenant Container databases
下面为GoldenGate 12c + Oracle 12c Multitenant Container databases例子 1.安装OGG 源 端OGG: C:\Oracle\product\1 ...
- python之模块 os
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块 os import os ''' FUNCTIONS abort(...)#暂不了解 a ...
- Struts1与Struts2的那些事
一.概述 Struts1以ActionServlet作为核心控制器,由ActionServlet负责拦截用户的全部请求.Struts1框架有3个重要组成部分:Action.ActionForm和Act ...
- qperf测量网络带宽和延迟
原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: qperf测量网络带宽和延迟 我们在做网络服务器的时候,通常会很关心网络的带宽和延迟.因为我们的很多协议都是request-repon ...
- linux运维常见英文报错中文翻译(菜鸟必知)
linux常见英文报错中文翻译(菜鸟必知) 1.command not found 命令没有找到 2.No such file or directory 没有这个文件或目录 3.Permissio ...
- Mysql DBA 20天速成教程
Mysql DBA 20天速成教程 基本知识1.mysql的编译安装2.mysql 第3方存储引擎安装配置方法3.mysql 主流存储引擎(MyISAM/innodb/MEMORY)的特点4.字符串编 ...
- Linux中的lo回环接口详细介绍
1.linux的网络接口之扫盲 (1)网络接口的命名 这里并不存在一定的命名规范,但网络接口名字的定义一般都是要有意义的.例如: eth0: ethernet的简写,一般用于以太网接口. wifi0: ...
- 【java】解析java中的数组
目录结构: contents structure [+] 一维数组 1,什么是一维数组 2,声明一维数组的三种方式 二维数组 1,什么是二维数组 2,声明二维数组的3种方式 3,二维数组的遍历示例 数 ...
- java udp 广播
原文链接: http://blog.csdn.net/yudajun/article/details/8477149 udp 是一种网络通信协议,不需要客户端和服务器端建立连接即可进行通讯功能.相对于 ...