1 /*
   2  * Copyright (c) 2020, 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 /**
  25  * @test
  26  * @requires vm.jvmci
  27  * @library ../../../../../
  28  * @modules java.base/jdk.internal.reflect
  29  *          jdk.internal.vm.ci/jdk.vm.ci.meta
  30  *          jdk.internal.vm.ci/jdk.vm.ci.runtime
  31  *          jdk.internal.vm.ci/jdk.vm.ci.common
  32  *          java.base/jdk.internal.misc
  33  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:-UseJVMCICompiler jdk.vm.ci.runtime.test.TestSingleImplementor
  34  */
  35 
  36 package jdk.vm.ci.runtime.test;
  37 
  38 import org.junit.Assert;
  39 import org.junit.Test;
  40 
  41 import jdk.vm.ci.meta.ResolvedJavaType;
  42 
  43 
  44 public class TestSingleImplementor extends TypeUniverse {
  45 
  46     static int SideEffect;
  47 
  48     interface I {
  49         void foo();
  50     }
  51 
  52     interface I1 extends I {
  53     }
  54 
  55     interface I2 extends I {
  56     }
  57 
  58     static class Impl implements I1, I2 {
  59         @Override
  60         public void foo() {
  61             SideEffect = 42;
  62         }
  63     }
  64 
  65     public static void snippetDiamond(I i) {
  66         i.foo();
  67     }
  68 
  69     @Test
  70     public void testDiamondShape() throws Throwable {
  71         snippetDiamond(new Impl());
  72         ResolvedJavaType interfaceType = metaAccess.lookupJavaType(I.class);
  73         ResolvedJavaType implementationType = metaAccess.lookupJavaType(Impl.class);
  74         Assert.assertEquals(implementationType, interfaceType.getSingleImplementor());
  75     }
  76 
  77     interface IF1 {
  78         void foo();
  79     }
  80 
  81     static class Impl1 implements IF1 {
  82         @Override
  83         public void foo() {
  84             SideEffect = 43;
  85         }
  86     }
  87 
  88     public static void snippetRegular(IF1 i) {
  89         i.foo();
  90     }
  91 
  92     @Test
  93     public void testRegularShape() throws Throwable {
  94         snippetRegular(new Impl1());
  95         ResolvedJavaType interfaceType = metaAccess.lookupJavaType(IF1.class);
  96         ResolvedJavaType implementationType = metaAccess.lookupJavaType(Impl1.class);
  97         Assert.assertEquals(implementationType, interfaceType.getSingleImplementor());
  98     }
  99 
 100 }