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.LookupTypeTest
  35  */
  36 
  37 package compiler.jvmci.compilerToVM;
  38 
  39 import compiler.jvmci.common.testcases.DoNotExtendClass;
  40 import compiler.jvmci.common.testcases.MultiSubclassedClass;
  41 import compiler.jvmci.common.testcases.SingleSubclass;
  42 import jdk.test.lib.Asserts;
  43 import jdk.test.lib.Utils;
  44 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  45 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  46 
  47 import java.util.HashSet;
  48 import java.util.Set;
  49 
  50 public class LookupTypeTest {
  51     public static void main(String args[]) {
  52         LookupTypeTest test = new LookupTypeTest();
  53         for (TestCase tcase : createTestCases()) {
  54             test.runTest(tcase);
  55         }
  56     }
  57 
  58     private static Set<TestCase> createTestCases() {
  59         Set<TestCase> result = new HashSet<>();
  60         // a primitive class
  61         result.add(new TestCase(Utils.toJVMTypeSignature(int.class),
  62                 LookupTypeTest.class, true, false, InternalError.class));
  63         // lookup not existing class
  64         result.add(new TestCase("Lsome_not_existing;", LookupTypeTest.class,
  65                 true, false, ClassNotFoundException.class));
  66         // lookup invalid classname
  67         result.add(new TestCase("L!@#$%^&**()[]{}?;", LookupTypeTest.class,
  68                 true, false, ClassNotFoundException.class));
  69         // lookup package private class
  70         result.add(new TestCase(
  71                 "Lcompiler/jvmci/compilerToVM/testcases/PackagePrivateClass;",
  72                 LookupTypeTest.class, true, false,
  73                 ClassNotFoundException.class));
  74         // lookup usual class with resolve=true
  75         result.add(new TestCase(Utils.toJVMTypeSignature(SingleSubclass.class),
  76                 LookupTypeTest.class, true, true));
  77         // lookup usual class with resolve=false
  78         result.add(new TestCase(
  79                 Utils.toJVMTypeSignature(DoNotExtendClass.class),
  80                 LookupTypeTest.class, false, true));
  81         // lookup usual class with null accessor
  82         result.add(new TestCase(
  83                 Utils.toJVMTypeSignature(MultiSubclassedClass.class), null,
  84                 false, false, NullPointerException.class));
  85         return result;
  86     }
  87 
  88     private void runTest(TestCase tcase) {
  89         System.out.println(tcase);
  90         HotSpotResolvedObjectType metaspaceKlass;
  91         try {
  92             metaspaceKlass = CompilerToVMHelper.lookupType(tcase.className,
  93                     tcase.accessing, tcase.resolve);
  94         } catch (Throwable t) {
  95             Asserts.assertNotNull(tcase.expectedException,
  96                     "Assumed no exception, but got " + t);
  97             Asserts.assertFalse(tcase.isPositive,
  98                     "Got unexpected exception " + t);
  99             Asserts.assertEQ(t.getClass(), tcase.expectedException,
 100                     "Unexpected exception");
 101             // passed
 102             return;
 103         }
 104         if (tcase.expectedException != null) {
 105             throw new AssertionError("Expected exception was not thrown: "
 106                     + tcase.expectedException.getName());
 107         }
 108         if (tcase.isPositive) {
 109             Asserts.assertNotNull(metaspaceKlass,
 110                     "Unexpected null metaspace klass");
 111             Asserts.assertEQ(metaspaceKlass.getName(), tcase.className,
 112                     "Got unexpected resolved class name");
 113         } else {
 114             Asserts.assertNull(metaspaceKlass, "Unexpected metaspace klass");
 115         }
 116     }
 117 
 118     private static class TestCase {
 119         public final String className;
 120         public final Class<?> accessing;
 121         public final boolean resolve;
 122         public final boolean isPositive;
 123         public final Class<? extends Throwable> expectedException;
 124 
 125         public TestCase(String className, Class<?> accessing, boolean resolve,
 126                 boolean isPositive,
 127                 Class<? extends Throwable> expectedException) {
 128             this.className = className;
 129             this.accessing = accessing;
 130             this.resolve = resolve;
 131             this.isPositive = isPositive;
 132             this.expectedException = expectedException;
 133         }
 134 
 135         public TestCase(String className, Class<?> accessing, boolean resolve,
 136                 boolean isPositive) {
 137             this.className = className;
 138             this.accessing = accessing;
 139             this.resolve = resolve;
 140             this.isPositive = isPositive;
 141             this.expectedException = null;
 142         }
 143 
 144         @Override
 145         public String toString() {
 146             return String.format("CASE: class=%s, accessing=%s,"
 147                 + " resolve=%s, positive=%s, expectedException=%s", className,
 148                 accessing, resolve, isPositive, expectedException);
 149         }
 150     }
 151 }