1 /*
   2  * Copyright (c) 2015, 2016, 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  * @bug 8136421
  27  * @requires vm.jvmci
  28  * @library / /test/lib/
  29  * @library ../common/patches
  30  * @modules java.base/jdk.internal.misc
  31  * @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot
  32  * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  33  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  34  *                   compiler.jvmci.compilerToVM.GetImplementorTest
  35  */
  36 
  37 package compiler.jvmci.compilerToVM;
  38 
  39 import compiler.jvmci.common.testcases.AbstractClass;
  40 import compiler.jvmci.common.testcases.AbstractClassExtender;
  41 import compiler.jvmci.common.testcases.DoNotExtendClass;
  42 import compiler.jvmci.common.testcases.DoNotImplementInterface;
  43 import compiler.jvmci.common.testcases.MultipleImplementer1;
  44 import compiler.jvmci.common.testcases.MultipleImplementer2;
  45 import compiler.jvmci.common.testcases.MultipleImplementersInterface;
  46 import compiler.jvmci.common.testcases.SingleImplementer;
  47 import compiler.jvmci.common.testcases.SingleImplementerInterface;
  48 import compiler.jvmci.common.testcases.SingleSubclass;
  49 import compiler.jvmci.common.testcases.SingleSubclassedClass;
  50 import jdk.test.lib.Asserts;
  51 import jdk.test.lib.Utils;
  52 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  53 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  54 
  55 import java.util.HashSet;
  56 import java.util.Set;
  57 import java.util.stream.Stream;
  58 
  59 public class GetImplementorTest {
  60     public static void main(String args[]) {
  61         GetImplementorTest test = new GetImplementorTest();
  62         for (TestCase tcase : createTestCases()) {
  63             test.runTest(tcase);
  64         }
  65     }
  66 
  67     private static Set<TestCase> createTestCases() {
  68         Set<TestCase> result = new HashSet<>();
  69         Stream.of(
  70                     SingleSubclass.class,
  71                     AbstractClassExtender.class,
  72                     MultipleImplementer2.class,
  73                     MultipleImplementer1.class,
  74                     MultipleImplementersInterface.class,
  75                     DoNotImplementInterface.class,
  76                     DoNotExtendClass.class,
  77                     AbstractClass.class,
  78                     SingleSubclassedClass.class)
  79                 .forEach(Utils::ensureClassIsLoaded);
  80         // an interface with single class implementing it
  81         result.add(new TestCase(SingleImplementerInterface.class,
  82                 SingleImplementer.class));
  83         /* an interface with multiple implementers. According to getImplementor
  84            javadoc, an itself should be returned in case of more than one
  85            implementor
  86          */
  87         result.add(new TestCase(MultipleImplementersInterface.class,
  88                 MultipleImplementersInterface.class));
  89         // an interface with no implementors
  90         result.add(new TestCase(DoNotImplementInterface.class, null));
  91         // an abstract class with extender class
  92         result.add(new TestCase(AbstractClass.class, null));
  93         // a simple class, which is not extended
  94         result.add(new TestCase(DoNotExtendClass.class, null));
  95         // a usual class, which is extended
  96         result.add(new TestCase(SingleSubclassedClass.class, null));
  97         return result;
  98     }
  99 
 100     private void runTest(TestCase tcase) {
 101         System.out.println(tcase);
 102         HotSpotResolvedObjectType resolvedIface = CompilerToVMHelper
 103                 .lookupType(Utils.toJVMTypeSignature(tcase.anInterface),
 104                         getClass(), /* resolve = */ true);
 105         HotSpotResolvedObjectType resolvedImplementer = CompilerToVMHelper
 106                 .getImplementor(resolvedIface);
 107         HotSpotResolvedObjectType resolvedExpected = null;
 108         if (tcase.expectedImplementer != null) {
 109             resolvedExpected = CompilerToVMHelper.lookupType(Utils
 110                     .toJVMTypeSignature(tcase.expectedImplementer),
 111                     getClass(), /* resolve = */ true);
 112         }
 113         Asserts.assertEQ(resolvedImplementer, resolvedExpected,
 114                 "Unexpected implementer for " + tcase.anInterface.getName());
 115     }
 116 
 117     private static class TestCase {
 118         public final Class<?> anInterface;
 119         public final Class<?> expectedImplementer;
 120 
 121         public TestCase(Class<?> iface, Class<?> expectedImplementer) {
 122             this.anInterface = iface;
 123             this.expectedImplementer = expectedImplementer;
 124         }
 125 
 126         @Override
 127         public String toString() {
 128             return String.format("CASE: interface=%s, expected=%s",
 129                     anInterface.getName(),
 130                     expectedImplementer == null
 131                             ? null
 132                             : expectedImplementer.getName());
 133         }
 134     }
 135 }