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