0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4652911
0N/A * @summary test Hashtable readObject for invocation of overridable put method
0N/A */
0N/Aimport java.util.Hashtable;
0N/A
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.Serializable;
0N/A
0N/A/**
0N/A * Class that extends Hashtable to demonstrate bug when
0N/A * subclass wraps the values put into the Hashtable.
0N/A */
0N/Apublic class ReadObject extends Hashtable {
0N/A /**
0N/A * Wraps the values put into MyHashtable objects
0N/A */
0N/A class ValueWrapper implements Serializable {
0N/A private Object mValue;
0N/A
0N/A ValueWrapper(Object value) {
0N/A mValue = value;
0N/A }
0N/A
0N/A Object getValue() {
0N/A return mValue;
0N/A }
0N/A };
0N/A
0N/A public Object get(Object key) {
0N/A ValueWrapper valueWrapper = (ValueWrapper)super.get(key);
0N/A Object value = valueWrapper.getValue();
0N/A if(value instanceof ValueWrapper)
0N/A throw new RuntimeException("Hashtable.get bug");
0N/A return value;
0N/A }
0N/A
0N/A public Object put(Object key, Object value) {
0N/A if(value instanceof ValueWrapper)
0N/A throw new RuntimeException(
0N/A "Hashtable.put bug: value is already wrapped");
0N/A ValueWrapper valueWrapper = new ValueWrapper(value);
0N/A super.put(key, valueWrapper);
0N/A return value;
0N/A }
0N/A
0N/A private static Object copyObject(Object oldObj) {
0N/A Object newObj = null;
0N/A try {
0N/A //Create a stream in which to serialize the object.
0N/A ByteArrayOutputStream ostream = new ByteArrayOutputStream();
0N/A ObjectOutputStream p = new ObjectOutputStream(ostream);
0N/A //Serialize the object into the stream
0N/A p.writeObject(oldObj);
0N/A
0N/A //Create an input stream from which to deserialize the object
0N/A byte[] byteArray = ostream.toByteArray();
0N/A ByteArrayInputStream istream = new ByteArrayInputStream(byteArray);
0N/A ObjectInputStream q = new ObjectInputStream(istream);
0N/A //Deserialize the object
0N/A newObj = q.readObject();
0N/A } catch (Exception ex) {
0N/A ex.printStackTrace();
0N/A }
0N/A return newObj;
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A ReadObject myHashtable = new ReadObject();
0N/A myHashtable.put("key", "value");
0N/A ReadObject myHashtableCopy = (ReadObject)copyObject(myHashtable);
0N/A String value = (String)myHashtableCopy.get("key");
0N/A }
0N/A};