1 /*
   2  * Copyright (c) 2012, 2018, 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 package org.graalvm.compiler.api.test;
  26 
  27 import java.util.Formatter;
  28 
  29 import org.graalvm.compiler.api.runtime.GraalJVMCICompiler;
  30 import org.graalvm.compiler.api.runtime.GraalRuntime;
  31 
  32 import jdk.vm.ci.runtime.JVMCI;
  33 import jdk.vm.ci.runtime.JVMCICompiler;
  34 import jdk.vm.ci.services.Services;
  35 
  36 /**
  37  * Access point for {@linkplain #getRuntime() retrieving} the {@link GraalRuntime} instance of the
  38  * system compiler from unit tests.
  39  */
  40 public class Graal {
  41 
  42     private static final GraalRuntime runtime = initializeRuntime();
  43 
  44     private static GraalRuntime initializeRuntime() {
  45         Services.initializeJVMCI();
  46         JVMCICompiler compiler = JVMCI.getRuntime().getCompiler();
  47         if (compiler instanceof GraalJVMCICompiler) {
  48             GraalJVMCICompiler graal = (GraalJVMCICompiler) compiler;
  49             return graal.getGraalRuntime();
  50         } else {
  51             return new InvalidGraalRuntime();
  52         }
  53     }
  54 
  55     /**
  56      * Gets the singleton {@link GraalRuntime} instance available to unit tests.
  57      */
  58     public static GraalRuntime getRuntime() {
  59         return runtime;
  60     }
  61 
  62     /**
  63      * Gets a capability provided by the {@link GraalRuntime} instance available to the application.
  64      *
  65      * @throws UnsupportedOperationException if the capability is not available
  66      */
  67     public static <T> T getRequiredCapability(Class<T> clazz) {
  68         T t = getRuntime().getCapability(clazz);
  69         if (t == null) {
  70             String javaHome = System.getProperty("java.home");
  71             String vmName = System.getProperty("java.vm.name");
  72             Formatter errorMessage = new Formatter();
  73             if (getRuntime().getClass() == InvalidGraalRuntime.class) {
  74                 errorMessage.format("The VM does not support the Graal API.%n");
  75             } else {
  76                 errorMessage.format("The VM does not expose required Graal capability %s.%n", clazz.getName());
  77             }
  78             errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
  79             errorMessage.format("Currently used VM configuration is: %s", vmName);
  80             throw new UnsupportedOperationException(errorMessage.toString());
  81         }
  82         return t;
  83     }
  84 
  85     private static final class InvalidGraalRuntime implements GraalRuntime {
  86 
  87         @Override
  88         public String getName() {
  89             return "";
  90         }
  91 
  92         @Override
  93         public <T> T getCapability(Class<T> clazz) {
  94             return null;
  95         }
  96     }
  97 }