Path: blob/master/test/jdk/java/util/AbstractMap/AbstractMapClone.java
41149 views
/*1* Copyright (c) 2000, 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 432874826* @summary AbstractMap's clone() method is implemented to27* reset AbstractMap's private fields after super.clone()28*29* @author Konstantin Kladko30*/3132import java.util.AbstractMap;33import java.util.HashMap;34import java.util.Map;35import java.util.Set;3637public class AbstractMapClone extends AbstractMap implements Cloneable {3839private Map map = new HashMap();4041public Set entrySet() {42return map.entrySet();43}4445public Object put(Object key, Object value) {46return map.put(key, value);47}4849public Object clone() {50final AbstractMapClone clone;51try {52clone = (AbstractMapClone)super.clone();53} catch (CloneNotSupportedException e) {54throw new AssertionError(e);55}56clone.map = (Map)((HashMap)map).clone();57return clone;58}5960public static void main(String[] args) {61AbstractMapClone m1 = new AbstractMapClone();62m1.put("1", "1");63Set k1 = m1.keySet();64AbstractMapClone m2 = (AbstractMapClone)m1.clone();65Set k2 = m2.keySet();66m2.put("2","2");67if (k1.equals(k2)) {68throw new RuntimeException("AbstractMap.clone() failed.");69}70}71}727374