1/* 2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * @test 26 * @bug 4652911 27 * @summary test Hashtable readObject for invocation of overridable put method 28 */ 29import java.util.Hashtable; 30 31import java.io.ByteArrayOutputStream; 32import java.io.ObjectOutputStream; 33import java.io.ByteArrayInputStream; 34import java.io.ObjectInputStream; 35import java.io.Serializable; 36 37/** 38 * Class that extends Hashtable to demonstrate bug when 39 * subclass wraps the values put into the Hashtable. 40 */ 41public class ReadObject extends Hashtable { 42 /** 43 * Wraps the values put into MyHashtable objects 44 */ 45 class ValueWrapper implements Serializable { 46 private Object mValue; 47 48 ValueWrapper(Object value) { 49 mValue = value; 50 } 51 52 Object getValue() { 53 return mValue; 54 } 55 }; 56 57 public Object get(Object key) { 58 ValueWrapper valueWrapper = (ValueWrapper)super.get(key); 59 Object value = valueWrapper.getValue(); 60 if (value instanceof ValueWrapper) 61 throw new RuntimeException("Hashtable.get bug"); 62 return value; 63 } 64 65 public Object put(Object key, Object value) { 66 if (value instanceof ValueWrapper) 67 throw new RuntimeException( 68 "Hashtable.put bug: value is already wrapped"); 69 ValueWrapper valueWrapper = new ValueWrapper(value); 70 super.put(key, valueWrapper); 71 return value; 72 } 73 74 private static Object copyObject(Object oldObj) { 75 Object newObj = null; 76 try { 77 //Create a stream in which to serialize the object. 78 ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 79 ObjectOutputStream p = new ObjectOutputStream(ostream); 80 //Serialize the object into the stream 81 p.writeObject(oldObj); 82 83 //Create an input stream from which to deserialize the object 84 byte[] byteArray = ostream.toByteArray(); 85 ByteArrayInputStream istream = new ByteArrayInputStream(byteArray); 86 ObjectInputStream q = new ObjectInputStream(istream); 87 //Deserialize the object 88 newObj = q.readObject(); 89 } catch (Exception ex) { 90 ex.printStackTrace(); 91 } 92 return newObj; 93 } 94 95 public static void main(String[] args) { 96 ReadObject myHashtable = new ReadObject(); 97 myHashtable.put("key", "value"); 98 ReadObject myHashtableCopy = (ReadObject)copyObject(myHashtable); 99 String value = (String)myHashtableCopy.get("key"); 100 } 101} 102