不应直接存储或返回可变成员 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个对象的数据和函数代码 ...
随机推荐
- DDIA
https://vonng.gitbooks.io/ddia-cn/content/ch7.html
- LR接口性能测试提示Code - 60990 Error: Two Way Communication Error: Function two_way_comm_post_message / two_ (转载)
一.在做JAVA接口性能测试时,场景在运行中出现:Code - 60990 Error: Two Way Communication Error: Function two_way_comm_post ...
- redhat7.0安装ifconfig
问题描述: Setup is unable to find the "ifconfig" program on your machine. Please make sure it ...
- 基于SpringSecurity和JWT的用户访问认证和授权
发布时间:2018-12-03 技术:springsecurity+jwt+java+jpa+mysql+mysql workBench 概述 基于SpringSecurity和JWT的用户访 ...
- ios总结目录
:iOS中er二维码的使用 http://www.cnblogs.com/gcb999/p/3183655.html :iOS中根据数据自动生成有规律的(UItextField和UILabel) IO ...
- [转]shell awk sed tr grep 语法汇总
tr 基本语法 -c # 用字符串1中字符集的补集替换此字符集,要求字符集为ASCII -d # 删除字符串1中所有输入字符 -s # 删除所 ...
- WebService出错 Maximum message size quota for incoming messages (65536) has been exceeded.已超过传入消息(65536)的最大消息大小配额
WebService应用中如果收到的信息非常大时出错. 1:Maximum message size quota for incoming messages (65536) has been exce ...
- 【Struts2】Struts2获取session的三种方式
1.Map<String,Object> map = ActionContext.getContext().getSession(); 2.HttpSession session = S ...
- 安卓sdk安装慢解决办法
1.上图SDK Manager 的 Tools ->Options打开SDK Manager的Settings, 选中“Force https://… sources to be fetched ...
- process credentials(一)
一.介绍 当linux系统中的一个进程运行起来的时候,总是要访问系统的资源,访问文件或者向其他的进程发送信号.系统是否允许其进行这些操作?系统是根据什么来判断该进程的权限?这些问题是和进程信任状(pr ...