博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java静态集合_java中如何创建不可变并且static的静态集合
阅读量:6644 次
发布时间:2019-06-25

本文共 2390 字,大约阅读时间需要 7 分钟。

为什么80%的码农都做不了架构师?>>>

6936d1565ae689371725cdb5fc51c415.png

以map为例

findbug错误提示:

MS_MUTABLE_COLLECTION_PKGPROTECT, Priority: Normal

XX.XX.XXX.XX is a mutable collection which should be package protected

A mutable collection instance is assigned to a final static field, thus can be changed by malicious code or by accident from another package. The field could be made package protected to avoid this vulnerability. Alternatively you may wrap this field into Collections.unmodifiableSet/List/Map/etc. to avoid this vulnerability.

错误案例:

public class TestMap {

private static final Mapmap = new LinkedHashMap();

static {

map = new HashMap();

map.put(1, "one");

map.put(2, "two");

}

}

正确的做法 (通过Collections.unmodifiableMap)

public class TestMap {

private static final Mapmap ;

static {

MaptempMap = new HashMap();

tempMap.put(1, "one");

tempMap.put(2, "two");

map = Collections.unmodifiableMap(tempMap);

}

}

如果现在往map里添加元素,则抛出UnsupportedOperationException异常

一下附上一段源码

/**

* We need this class in addition to UnmodifiableSet as

* Map.Entries themselves permit modification of the backing Map

* via their setValue operation. This class is subtle: there are

* many possible attacks that must be thwarted.

*

* @serial include

*/

static class UnmodifiableEntrySetextends UnmodifiableSet> {

private static final long serialVersionUID = 7854390611657943733L;

@SuppressWarnings({"unchecked", "rawtypes"})

UnmodifiableEntrySet(Set extends Map.Entry extends K, ? extends V>> s) {

// Need to cast to raw in order to work around a limitation in the type system

super((Set)s);

}

static Consumer> entryConsumer(Consumer super Entry> action) {

return e -> action.accept(new UnmodifiableEntry<>(e));

}

public void forEach(Consumer super Entry> action) {

Objects.requireNonNull(action);

c.forEach(entryConsumer(action));

}

static final class UnmodifiableEntrySetSpliteratorimplements Spliterator> {

final Spliterator> s;

UnmodifiableEntrySetSpliterator(Spliterator> s) {

this.s = s;

}

@Override

public boolean tryAdvance(Consumer super Entry> action) {

Objects.requireNonNull(action);

return s.tryAdvance(entryConsumer(action));

}

@Override

public void forEachRemaining(Consumer super Entry> action) {

Objects.requireNonNull(action);

s.forEachRemaining(entryConsumer(action));

}

@Override

public Spliterator> trySplit() {

Spliterator> split = s.trySplit();

return split == null

? null

: new UnmodifiableEntrySetSpliterator<>(split);

}

..................

转载地址:http://ifevo.baihongyu.com/

你可能感兴趣的文章
【CCNP】BGP路由反射器与AS联邦案例实验
查看>>
TCP_Wrappers
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
一个很酷的加载loading效果
查看>>
我的友情链接
查看>>
Java解析json串
查看>>
ubuntu12.04 NFS搭建指南
查看>>
Sublime Text 使用介绍、全套快捷键及插件推荐
查看>>
toolbar
查看>>
spring boot 项目,maven打jar包时,将本地jar一块打入包
查看>>
Windows Server 2012 虚拟化实战:存储(一)
查看>>
linux shell 计算时间差并显示按时分秒显示
查看>>
iptables防火墙
查看>>
最大子序列和问题的解——C++实现;
查看>>
Shell脚本语言
查看>>
.NET快速开发平台,开发效率倍增神器
查看>>
阿里云 Aliplayer高级功能介绍(六):进度条标记
查看>>
【Python学习笔记】数据结构—序列——list列表和tuple元组
查看>>
Oracle 11G r2 Rac修改IP
查看>>