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