1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /**
  27  * @test
  28  * @bug 8003639
  29  * @summary defaultMethod resolution and verification
  30  * @run main DefaultMethodRegressionTests
  31  */
  32 
  33 import java.util.ArrayList;
  34 import java.util.Arrays;
  35 import java.util.List;
  36 
  37 /**
  38  * This set of classes/interfaces (K/I/C) is specially designed to expose a
  39  * bug in the JVM where it did not find some overloaded methods in some
  40  * specific situations. (fixed by hotspot changeset ffb9316fd9ed).
  41  */
  42 interface K {
  43     int bbb(Long l);
  44 }
  45 
  46 interface I extends K {
  47     default void aaa() {}
  48     default void aab() {}
  49     default void aac() {}
  50 
  51     default int bbb(Integer i) { return 22; }
  52     default int bbb(Float f) { return 33; }
  53     default int bbb(Long l) { return 44; }
  54     default int bbb(Double d) { return 55; }
  55     default int bbb(String s) { return 66; }
  56 
  57     default void caa() {}
  58     default void cab() {}
  59     default void cac() {}
  60 }
  61 
  62 class C implements I {}
  63 
  64 public class DefaultMethodRegressionTests {
  65     public static void main(String... args) {
  66         new DefaultMethodRegressionTests().run(args);
  67     }
  68     void run(String... args) {
  69         testLostOverloadedMethod();
  70         System.out.println("testLostOverloadedMethod: OK");
  71         testInferenceVerifier();
  72         System.out.println("testInferenceVerifier: OK");
  73     }
  74     void testLostOverloadedMethod() {
  75         C c = new C();
  76         assertEquals(c.bbb(new Integer(1)), 22);
  77         assertEquals(c.bbb(new Float(1.1)), 33);
  78         assertEquals(c.bbb(new Long(1L)), 44);
  79         assertEquals(c.bbb(new Double(0.01)), 55);
  80         assertEquals(c.bbb(new String("")), 66);
  81     }
  82     // Test to ensure that the inference verifier accepts older classfiles
  83     // with classes that implement interfaces with defaults.
  84     void testInferenceVerifier() {
  85         // interface I { int m() default { return 99; } }
  86         byte I_bytes[] = {
  87             (byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe, 0x00, 0x00, 0x00, 0x34,
  88             0x00, 0x08, 0x07, 0x00, 0x06, 0x07, 0x00, 0x07,
  89             0x01, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x01, 0x00,
  90             0x03, 0x28, 0x29, 0x49, 0x01, 0x00, 0x04, 0x43,
  91             0x6f, 0x64, 0x65, 0x01, 0x00, 0x01, 0x49, 0x01,
  92             0x00, 0x10, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c,
  93             0x61, 0x6e, 0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65,
  94             0x63, 0x74, 0x06, 0x00, 0x00, 0x01, 0x00, 0x02,
  95             0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x01,
  96             0x00, 0x03, 0x00, 0x04, 0x00, 0x01, 0x00, 0x05,
  97             0x00, 0x00, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x01,
  98             0x00, 0x00, 0x00, 0x03, 0x10, 0x63, (byte)0xac, 0x00,
  99             0x00, 0x00, 0x00, 0x00, 0x00
 100         };
 101         // public class C implements I {}  /* -target 1.5 */
 102         byte C_bytes[] = {
 103             (byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe, 0x00, 0x00, 0x00, 0x31,
 104             0x00, 0x0c, 0x0a, 0x00, 0x03, 0x00, 0x08, 0x07,
 105             0x00, 0x09, 0x07, 0x00, 0x0a, 0x07, 0x00, 0x0b,
 106             0x01, 0x00, 0x06, 0x3c, 0x69, 0x6e, 0x69, 0x74,
 107             0x3e, 0x01, 0x00, 0x03, 0x28, 0x29, 0x56, 0x01,
 108             0x00, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x0c, 0x00,
 109             0x05, 0x00, 0x06, 0x01, 0x00, 0x01, 0x43, 0x01,
 110             0x00, 0x10, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c,
 111             0x61, 0x6e, 0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65,
 112             0x63, 0x74, 0x01, 0x00, 0x01, 0x49, 0x00, 0x21,
 113             0x00, 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x04,
 114             0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x05,
 115             0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
 116             0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
 117             0x00, 0x05, 0x2a, (byte)0xb7, 0x00, 0x01, (byte)0xb1, 0x00,
 118             0x00, 0x00, 0x00, 0x00, 0x00
 119         };
 120 
 121         ClassLoader cl = new ClassLoader() {
 122             protected Class<?> findClass(String name) {
 123                 if (name.equals("I")) {
 124                     return defineClass("I", I_bytes, 0, I_bytes.length);
 125                 } else if (name.equals("C")) {
 126                     return defineClass("C", C_bytes, 0, C_bytes.length);
 127                 } else {
 128                     return null;
 129                 }
 130             }
 131         };
 132         try {
 133             Class.forName("C", true, cl);
 134         } catch (Exception e) {
 135             // unmodified verifier will throw VerifyError
 136             throw new RuntimeException(e);
 137         }
 138     }
 139     void assertEquals(Object o1, Object o2) {
 140         System.out.print("Expected: " + o1);
 141         System.out.println(", Obtained: " + o2);
 142         if (!o1.equals(o2)) {
 143             throw new RuntimeException("got unexpected values");
 144         }
 145     }
 146 }