1 /*
   2  * Copyright (c) 2019, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package compiler.cha;
  25 
  26 import jdk.internal.vm.annotation.DontInline;
  27 import static compiler.cha.StrengthReduceInterfaceCall.*;
  28 
  29 public class ThreeLevelDefaultHierarchy extends ATest<ThreeLevelDefaultHierarchy.I> {
  30     public ThreeLevelDefaultHierarchy() {
  31         super(I.class, C.class);
  32     }
  33 
  34     interface J           { default Object m() { return WRONG; }}
  35     interface I extends J {}
  36 
  37     static class C  implements I { public Object m() { return CORRECT; }}
  38 
  39     interface K1 extends I {}
  40     interface K2 extends I { Object m(); }
  41     interface K3 extends I { default Object m() { return WRONG; }}
  42 
  43     static class DI implements I { public Object m() { return WRONG; }}
  44     static class DJ implements J { public Object m() { return WRONG; }}
  45 
  46     @DontInline
  47     public Object test(I i) {
  48         return i.m(); // no inlining since J.m is a default method
  49     }
  50 
  51     @TestCase
  52     public void testMega() {
  53         // 0. Trigger compilation of a megamorphic call site
  54         compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I <: intf J.m ABSTRACT
  55         assertCompiled();
  56 
  57         // Dependency: none
  58 
  59         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
  60 
  61         // 1. No deoptimization/invalidation on not-yet-seen receiver
  62         repeat(100, () -> call(new C() {}));
  63         assertCompiled();
  64 
  65         // 2. No dependency and no inlining
  66         initialize(DJ.class,  //      DJ.m                    <: intf J.m ABSTRACT
  67                    DI.class,  //      DI.m          <: intf I <: intf J.m ABSTRACT
  68                    K1.class,  // intf K1            <: intf I <: intf J.m ABSTRACT
  69                    K2.class); // intf K2.m ABSTRACT <: intf I <: intf J.m ABSTRACT
  70         assertCompiled();
  71     }
  72 
  73     @Override
  74     public void checkInvalidReceiver() {
  75         shouldThrow(IncompatibleClassChangeError.class, () -> {
  76             I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
  77             test(o);
  78         });
  79         assertCompiled();
  80 
  81         shouldThrow(IncompatibleClassChangeError.class, () -> {
  82             I j = (I) unsafeCastMH(I.class).invokeExact((Object) new J() {
  83             }); // super interface
  84             test(j);
  85         });
  86         assertCompiled();
  87     }
  88 }