Path: blob/master/test/jdk/java/util/Hashtable/ReadObject.java
41149 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 465291126* @summary test Hashtable readObject for invocation of overridable put method27*/2829import java.io.ByteArrayInputStream;30import java.io.ByteArrayOutputStream;31import java.io.ObjectInputStream;32import java.io.ObjectOutputStream;33import java.io.Serializable;34import java.util.Hashtable;3536/**37* Class that extends Hashtable to demonstrate bug when38* subclass wraps the values put into the Hashtable.39*/40public class ReadObject extends Hashtable {41/**42* Wraps the values put into MyHashtable objects43*/44class ValueWrapper implements Serializable {45private Object mValue;4647ValueWrapper(Object value) {48mValue = value;49}5051Object getValue() {52return mValue;53}54}5556public Object get(Object key) {57ValueWrapper valueWrapper = (ValueWrapper)super.get(key);58Object value = valueWrapper.getValue();59if (value instanceof ValueWrapper)60throw new RuntimeException("Hashtable.get bug");61return value;62}6364public Object put(Object key, Object value) {65if (value instanceof ValueWrapper)66throw new RuntimeException(67"Hashtable.put bug: value is already wrapped");68ValueWrapper valueWrapper = new ValueWrapper(value);69super.put(key, valueWrapper);70return value;71}7273private static Object copyObject(Object oldObj) {74Object newObj = null;75try {76//Create a stream in which to serialize the object.77ByteArrayOutputStream ostream = new ByteArrayOutputStream();78ObjectOutputStream p = new ObjectOutputStream(ostream);79//Serialize the object into the stream80p.writeObject(oldObj);8182//Create an input stream from which to deserialize the object83byte[] byteArray = ostream.toByteArray();84ByteArrayInputStream istream = new ByteArrayInputStream(byteArray);85ObjectInputStream q = new ObjectInputStream(istream);86//Deserialize the object87newObj = q.readObject();88} catch (Exception ex) {89ex.printStackTrace();90}91return newObj;92}9394public static void main(String[] args) {95ReadObject myHashtable = new ReadObject();96myHashtable.put("key", "value");97ReadObject myHashtableCopy = (ReadObject)copyObject(myHashtable);98String value = (String)myHashtableCopy.get("key");99}100}101102103