1 /*
   2  * Copyright (c) 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug 8153540
  29  * @modules java.base/jdk.internal.misc
  30  *
  31  * @run main/bootclasspath -Xbatch -XX:CompileCommand=dontinline,*AllocateInstance.test*
  32  *                         compiler.unsafe.AllocateInstance
  33  */
  34 package compiler.unsafe;
  35 
  36 import jdk.internal.misc.Unsafe;
  37 import java.util.concurrent.Callable;
  38 
  39 public class AllocateInstance {
  40     static final Unsafe U = Unsafe.getUnsafe();
  41 
  42     static class T {}
  43     static final Class<?> T_CLASS;
  44     static {
  45         try {
  46             T_CLASS = Class.forName(T.class.getName(), false, ClassLoader.getSystemClassLoader());
  47         } catch (ClassNotFoundException e) {
  48             throw new Error(e);
  49         }
  50     }
  51 
  52     static abstract class A {}
  53 
  54     static Object testNull() throws InstantiationException {
  55         return U.allocateInstance(null);
  56     }
  57     static Object testPrim() throws InstantiationException {
  58         return U.allocateInstance(int.class);
  59     }
  60     static Object testPrimArray() throws InstantiationException {
  61         return U.allocateInstance(int[].class);
  62     }
  63     static Object testObjArray() throws InstantiationException {
  64         return U.allocateInstance(Object[].class);
  65     }
  66     static Object testIntf() throws InstantiationException {
  67         return U.allocateInstance(Runnable.class);
  68     }
  69     static Object testAbstract() throws InstantiationException {
  70         return U.allocateInstance(A.class);
  71     }
  72     static Object testObject() throws InstantiationException {
  73         return U.allocateInstance(Object.class);
  74     }
  75     static Object testObjectInit() throws InstantiationException {
  76         return U.allocateInstance(T_CLASS);
  77     }
  78     static Object testReflective(Class<?> c) throws InstantiationException {
  79         return U.allocateInstance(c);
  80     }
  81 
  82     static void run(String name, Callable<Object> c, boolean shouldSucceed, boolean verbose) {
  83         if (verbose) System.out.print(name);
  84         run(c, shouldSucceed, verbose);
  85     }
  86 
  87     static void run(Class<?> c, boolean shouldSucceed, boolean verbose) {
  88         if (verbose) System.out.print(c != null ? c.getName() : "null");
  89         run(() -> testReflective(c), shouldSucceed, verbose);
  90     }
  91 
  92     static void run(Callable<Object> c, boolean shouldSucceed, boolean verbose) {
  93         try {
  94             Object o = c.call();
  95             if (o == null) {
  96                 System.out.println(" => null");
  97             } else {
  98                 if (verbose) System.out.println(" => allocated");
  99             }
 100             if (!shouldSucceed) {
 101                 throw new AssertionError("allocation should fail");
 102             }
 103         } catch (Exception e) {
 104             if (verbose) {
 105                 System.out.println(" => exception " + e.getClass().getName() + ": " + e.getMessage());
 106             }
 107             if (shouldSucceed) {
 108                 throw new AssertionError("allocation should succeed");
 109             }
 110         }
 111     }
 112 
 113     static void runTests(boolean verbose) {
 114         run("null",       AllocateInstance::testNull,       false, verbose);
 115         run("int",        AllocateInstance::testPrim,       false, verbose);
 116         run("int[]",      AllocateInstance::testPrimArray,  false, verbose);
 117         run("Object[]",   AllocateInstance::testObjArray,   false, verbose);
 118         run("interface",  AllocateInstance::testIntf,       false, verbose);
 119         run("abstract",   AllocateInstance::testAbstract,   false, verbose);
 120         run("Object",     AllocateInstance::testObject,     true,  verbose);
 121         run("ObjectInit", AllocateInstance::testObjectInit, true,  verbose);
 122 
 123         run((Class<?>)null, false, verbose);
 124         run(Object.class,   true,  verbose);
 125         run(A.class,        false, verbose);
 126         run(Runnable.class, false, verbose);
 127         run(Object[].class, false, verbose);
 128         run(int.class,      false, verbose);
 129         run(int[].class,    false, verbose);
 130     }
 131 
 132     static void warmup() {
 133         for (int i = 0; i < 20_000; i ++) {
 134             runTests(false);
 135         }
 136     }
 137     public static void main(String[] args) {
 138         runTests(true);
 139         warmup();
 140         runTests(true);
 141         System.out.println("TEST PASSED");
 142     }
 143 }