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 ThreeLevelDefaultHierarchy1 extends ATest<ThreeLevelDefaultHierarchy1.I> {
  30     public ThreeLevelDefaultHierarchy1() {
  31         super(I.class, C.class);
  32     }
  33 
  34     interface J1                { Object m();}
  35     interface J2 extends J1     { default Object m() { return WRONG; }  }
  36     interface I  extends J1, J2 {}
  37 
  38     static class C  implements I { public Object m() { return CORRECT; }}
  39 
  40     interface K1 extends I {}
  41     interface K2 extends I { Object m(); }
  42     interface K3 extends I { default Object m() { return WRONG; }}
  43 
  44     static class DI implements I { public Object m() { return WRONG; }}
  45     static class DJ1 implements J1 { public Object m() { return WRONG; }}
  46     static class DJ2 implements J2 { public Object m() { return WRONG; }}
  47 
  48     @DontInline
  49     public Object test(I i) {
  50         return i.m(); // no inlining since J.m is a default method
  51     }
  52 
  53     @TestCase
  54     public void testMega() {
  55         // 0. Trigger compilation of a megamorphic call site
  56         compile(megamorphic());
  57         assertCompiled();
  58 
  59         // Dependency: none
  60 
  61         checkInvalidReceiver(); // ensure proper type check on receiver is preserved
  62 
  63         // 1. No deoptimization/invalidation on not-yet-seen receiver
  64         repeat(100, () -> call(new C() {}));
  65         assertCompiled();
  66 
  67         // 2. No dependency, no inlining
  68         // CHA doesn't support default methods yet.
  69         initialize(DJ1.class,
  70                    DJ2.class,
  71                    DI.class,
  72                    K1.class,
  73                    K2.class,
  74                    K3.class);
  75         assertCompiled();
  76     }
  77 
  78     @Override
  79     public void checkInvalidReceiver() {
  80         shouldThrow(IncompatibleClassChangeError.class, () -> {
  81             I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
  82             test(o);
  83         });
  84         assertCompiled();
  85 
  86         shouldThrow(IncompatibleClassChangeError.class, () -> {
  87             I j = (I) unsafeCastMH(I.class).invokeExact((Object) new J1() {
  88                 public Object m() {
  89                     return WRONG;
  90                 }
  91             }); // super interface
  92             test(j);
  93         });
  94         assertCompiled();
  95 
  96         shouldThrow(IncompatibleClassChangeError.class, () -> {
  97             I j = (I) unsafeCastMH(I.class).invokeExact((Object) new J2() {
  98             }); // super interface
  99             test(j);
 100         });
 101         assertCompiled();
 102     }
 103 }