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 static compiler.cha.StrengthReduceInterfaceCall.*;
  27 
  28 public class ObjectToString extends ATest<ObjectToString.I> {
  29     public ObjectToString() {
  30         super(I.class, C.class);
  31     }
  32 
  33     interface J           { String toString(); }
  34     interface I extends J {}
  35 
  36     static class C implements I {}
  37 
  38     interface K1 extends I {}
  39     interface K2 extends I { String toString(); } // K2.tS() ABSTRACT
  40     // interface K3 extends I { default String toString() { return "K3"; } // K2.tS() DEFAULT
  41 
  42     static class D implements I { public String toString() { return "D"; }}
  43 
  44     static class DJ1 implements J {}
  45     static class DJ2 implements J { public String toString() { return "DJ2"; }}
  46 
  47     @Override
  48     public Object test(I i) { return ObjectToStringHelper.test(i); /* invokeinterface I.toString() */ }
  49 
  50     @TestCase
  51     public void testMono() {
  52         // 0. Trigger compilation of a monomorphic call site
  53         compile(monomophic()); // C1 <: C <: intf I <: intf J <: Object.toString()
  54         assertCompiled();
  55 
  56         // Dependency: none
  57 
  58         call(new C() { public String toString() { return "Cn"; }}); // Cn.tS <: C.tS <: intf I
  59         assertCompiled();
  60     }
  61 
  62     @TestCase
  63     public void testBi() {
  64         // 0. Trigger compilation of a bimorphic call site
  65         compile(bimorphic()); // C1 <: C <: intf I <: intf J <: Object.toString()
  66         assertCompiled();
  67 
  68         // Dependency: none
  69 
  70         call(new C() { public String toString() { return "Cn"; }}); // Cn.tS <: C.tS <: intf I
  71         assertCompiled();
  72     }
  73 
  74     @TestCase
  75     public void testMega() {
  76         // 0. Trigger compilation of a megamorphic call site
  77         compile(megamorphic()); // C1,C2,C3 <: C <: intf I <: intf J <: Object.toString()
  78         assertCompiled();
  79 
  80         // Dependency: none
  81         // compiler.cha.StrengthReduceInterfaceCall$ObjectToString::test (5 bytes)
  82         //     @ 1   compiler.cha.StrengthReduceInterfaceCall$ObjectToStringHelper::test (7 bytes)   inline (hot)
  83         //       @ 1   java.lang.Object::toString (36 bytes)   virtual call
  84 
  85         // No dependency - no invalidation
  86         repeat(100, () -> call(new C(){})); // Cn <: C <: intf I
  87         assertCompiled();
  88 
  89         initialize(K1.class,   // intf  K1             <: intf I <: intf J
  90                    K2.class,   // intf  K2.tS ABSTRACT <: intf I <: intf J
  91                    DJ1.class,  //      DJ1                       <: intf J
  92                    DJ2.class); //      DJ2.tS                    <: intf J
  93         assertCompiled();
  94 
  95         initialize(D.class); // D.tS <: intf I <: intf J
  96         assertCompiled();
  97 
  98         call(new C() { public String toString() { return "Cn"; }}); // Cn.tS <: C.tS <: intf I
  99         assertCompiled();
 100     }
 101 
 102     @Override
 103     public void checkInvalidReceiver() {
 104         shouldThrow(IncompatibleClassChangeError.class, () -> {
 105             I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
 106             test(o);
 107         });
 108         assertCompiled();
 109 
 110         shouldThrow(IncompatibleClassChangeError.class, () -> {
 111             I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J() {}); // super interface
 112             test(j);
 113         });
 114         assertCompiled();
 115     }
 116 }
 117 
 118