Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/vm/verifier/defaultMethods/DefaultMethodRegressionTests.java
38838 views
/*1* Copyright (c) 2012, 2013, 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 800363926* @summary defaultMethod resolution and verification27* @run main DefaultMethodRegressionTests28*/2930import java.util.ArrayList;31import java.util.Arrays;32import java.util.List;3334/**35* This set of classes/interfaces (K/I/C) is specially designed to expose a36* bug in the JVM where it did not find some overloaded methods in some37* specific situations. (fixed by hotspot changeset ffb9316fd9ed).38*/39interface K {40int bbb(Long l);41}4243interface I extends K {44default void aaa() {}45default void aab() {}46default void aac() {}4748default int bbb(Integer i) { return 22; }49default int bbb(Float f) { return 33; }50default int bbb(Long l) { return 44; }51default int bbb(Double d) { return 55; }52default int bbb(String s) { return 66; }5354default void caa() {}55default void cab() {}56default void cac() {}57}5859class C implements I {}6061public class DefaultMethodRegressionTests {62public static void main(String... args) {63new DefaultMethodRegressionTests().run(args);64}65void run(String... args) {66testLostOverloadedMethod();67System.out.println("testLostOverloadedMethod: OK");68testInferenceVerifier();69System.out.println("testInferenceVerifier: OK");70}71void testLostOverloadedMethod() {72C c = new C();73assertEquals(c.bbb(new Integer(1)), 22);74assertEquals(c.bbb(new Float(1.1)), 33);75assertEquals(c.bbb(new Long(1L)), 44);76assertEquals(c.bbb(new Double(0.01)), 55);77assertEquals(c.bbb(new String("")), 66);78}79// Test to ensure that the inference verifier accepts older classfiles80// with classes that implement interfaces with defaults.81void testInferenceVerifier() {82// interface I { int m() default { return 99; } }83byte I_bytes[] = {84(byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe, 0x00, 0x00, 0x00, 0x34,850x00, 0x08, 0x07, 0x00, 0x06, 0x07, 0x00, 0x07,860x01, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x01, 0x00,870x03, 0x28, 0x29, 0x49, 0x01, 0x00, 0x04, 0x43,880x6f, 0x64, 0x65, 0x01, 0x00, 0x01, 0x49, 0x01,890x00, 0x10, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c,900x61, 0x6e, 0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65,910x63, 0x74, 0x06, 0x00, 0x00, 0x01, 0x00, 0x02,920x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x01,930x00, 0x03, 0x00, 0x04, 0x00, 0x01, 0x00, 0x05,940x00, 0x00, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x01,950x00, 0x00, 0x00, 0x03, 0x10, 0x63, (byte)0xac, 0x00,960x00, 0x00, 0x00, 0x00, 0x0097};98// public class C implements I {} /* -target 1.5 */99byte C_bytes[] = {100(byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe, 0x00, 0x00, 0x00, 0x31,1010x00, 0x0c, 0x0a, 0x00, 0x03, 0x00, 0x08, 0x07,1020x00, 0x09, 0x07, 0x00, 0x0a, 0x07, 0x00, 0x0b,1030x01, 0x00, 0x06, 0x3c, 0x69, 0x6e, 0x69, 0x74,1040x3e, 0x01, 0x00, 0x03, 0x28, 0x29, 0x56, 0x01,1050x00, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x0c, 0x00,1060x05, 0x00, 0x06, 0x01, 0x00, 0x01, 0x43, 0x01,1070x00, 0x10, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c,1080x61, 0x6e, 0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65,1090x63, 0x74, 0x01, 0x00, 0x01, 0x49, 0x00, 0x21,1100x00, 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x04,1110x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x05,1120x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,1130x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,1140x00, 0x05, 0x2a, (byte)0xb7, 0x00, 0x01, (byte)0xb1, 0x00,1150x00, 0x00, 0x00, 0x00, 0x00116};117118ClassLoader cl = new ClassLoader() {119protected Class<?> findClass(String name) {120if (name.equals("I")) {121return defineClass("I", I_bytes, 0, I_bytes.length);122} else if (name.equals("C")) {123return defineClass("C", C_bytes, 0, C_bytes.length);124} else {125return null;126}127}128};129try {130Class.forName("C", true, cl);131} catch (Exception e) {132// unmodified verifier will throw VerifyError133throw new RuntimeException(e);134}135}136void assertEquals(Object o1, Object o2) {137System.out.print("Expected: " + o1);138System.out.println(", Obtained: " + o2);139if (!o1.equals(o2)) {140throw new RuntimeException("got unexpected values");141}142}143}144145146