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