Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/object/TestClone.java
41153 views
/*1* Copyright (c) 2014, 2020, 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 8033626 8246453 824846726* @summary assert(ex_map->jvms()->same_calls_as(_exceptions->jvms())) failed: all collected exceptions must come from the same place27* @modules java.base/jdk.internal.misc28* @library /test/lib29*30* @run main/othervm -XX:-TieredCompilation -Xbatch31* -XX:CompileCommand=compileonly,compiler.intrinsics.object.TestClone::test*32* compiler.intrinsics.object.TestClone33* @run main/othervm -XX:-TieredCompilation -Xbatch34* -XX:+IgnoreUnrecognizedVMOptions -XX:+StressReflectiveCode35* -XX:CompileCommand=compileonly,compiler.intrinsics.object.TestClone::test*36* compiler.intrinsics.object.TestClone37* @run main/othervm -XX:-TieredCompilation -Xbatch38* -XX:+IgnoreUnrecognizedVMOptions -XX:+StressReflectiveCode -XX:+VerifyGraphEdges39* -XX:CompileCommand=compileonly,compiler.intrinsics.object.TestClone::test*40* compiler.intrinsics.object.TestClone41*/4243package compiler.intrinsics.object;4445import jdk.test.lib.Asserts;4647abstract class MyAbstract {4849public Object myClone1() throws CloneNotSupportedException {50return this.clone();51}5253public Object myClone2() throws CloneNotSupportedException {54return this.clone();55}5657public Object myClone3() throws CloneNotSupportedException {58return this.clone();59}60}6162class MyClass1 extends MyAbstract {63@Override64public Object clone() throws CloneNotSupportedException {65return super.clone();66}67}6869class MyClass2 extends MyAbstract {7071}7273class MyClass3 extends MyAbstract implements Cloneable {74@Override75public Object clone() throws CloneNotSupportedException {76return super.clone();77}78}7980class MyClass4 extends MyAbstract implements Cloneable {8182}8384public class TestClone implements Cloneable {85static class A extends TestClone {}86static class B extends TestClone {87public B clone() {88return (B)TestClone.b;89}90}91static class C extends TestClone {92public C clone() {93return (C)TestClone.c;94}95}96static class D extends TestClone {97public D clone() {98return (D)TestClone.d;99}100}101static TestClone a = new A(), b = new B(), c = new C(), d = new D();102103public static Object test1(TestClone o) throws CloneNotSupportedException {104// Polymorphic call site: >90% Object::clone / <10% other methods105return o.clone();106}107108public static void test2(MyAbstract obj, boolean shouldThrow) throws Exception {109try {110obj.myClone1();111} catch (Exception e) {112return; // Expected113}114Asserts.assertFalse(shouldThrow, "No exception thrown");115}116117public static void test3(MyAbstract obj, boolean shouldThrow) throws Exception {118try {119obj.myClone2();120} catch (Exception e) {121return; // Expected122}123Asserts.assertFalse(shouldThrow, "No exception thrown");124}125126public static void test4(MyAbstract obj, boolean shouldThrow) throws Exception {127try {128obj.myClone3();129} catch (Exception e) {130return; // Expected131}132Asserts.assertFalse(shouldThrow, "No exception thrown");133}134135public static void main(String[] args) throws Exception {136TestClone[] params1 = {a, a, a, a, a, a, a, a, a, a, a,137a, a, a, a, a, a, a, a, a, a, a,138a, a, a, a, a, a, a, a, a, a, a,139b, c, d};140141MyClass1 obj1 = new MyClass1();142MyClass2 obj2 = new MyClass2();143MyClass3 obj3 = new MyClass3();144MyClass4 obj4 = new MyClass4();145146for (int i = 0; i < 15000; i++) {147test1(params1[i % params1.length]);148149test2(obj1, true);150test2(obj2, true);151152test3(obj3, false);153test3(obj2, true);154155test4(obj3, false);156test4(obj4, false);157}158159Asserts.assertTrue(test1(a) != a);160Asserts.assertTrue(test1(b) == b);161Asserts.assertTrue(test1(c) == c);162Asserts.assertTrue(test1(d) == d);163164try {165test1(null);166throw new AssertionError("No exception thrown");167} catch (NullPointerException e) { /* expected */ }168169System.out.println("TEST PASSED");170}171}172173174