view graal/com.oracle.max.graal.tests/src/com/oracle/graal/compiler/tests/GraalRuntimeAccess.java @ 5060:4ed4295ce15f

Update import statements.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Thu, 08 Mar 2012 19:11:12 +0100
parents ed559a528128
children
line wrap: on
line source

/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.graal.compiler.tests;

import java.lang.reflect.*;
import java.util.*;

import com.oracle.graal.cri.*;

/**
 * Utility for getting a {@link GraalRuntime} instance from the current execution environment.
 */
class GraalRuntimeAccess {

    /**
     * The known classes declaring a {@code getGraalRuntime()} method. These class names
     * have aliases that can be used with the {@code "graal.runtime"} system property to
     * specify the VM environment to try first when getting a Graal runtime instance.
     */
    private static Map<String, String> graalRuntimeFactoryClasses = new LinkedHashMap<>();
    static {
        graalRuntimeFactoryClasses.put("HotSpot", "com.oracle.graal.hotspot.CompilerImpl");
        graalRuntimeFactoryClasses.put("Maxine", "com.oracle.max.vm.ext.maxri.MaxRuntime");
    }

    /**
     * Gets a Graal runtime instance from the current execution environment.
     */
    static GraalRuntime getGraalRuntime() {
        String vm = System.getProperty("graal.runtime");
        if (vm != null) {
            String cn = graalRuntimeFactoryClasses.get(vm);
            if (cn != null) {
                GraalRuntime graal = getGraalRuntime(cn);
                if (graal != null) {
                    return graal;
                }
            }
        }

        for (String className : graalRuntimeFactoryClasses.values()) {
            GraalRuntime graal = getGraalRuntime(className);
            if (graal != null) {
                return graal;
            }
        }
        throw new InternalError("Could not create a GraalRuntime instance");
    }

    /**
     * Calls {@code getGraalRuntime()} via reflection on a given class.
     *
     * @return {@code null} if there was an error invoking the methodor if the method return {@code null} itself
     */
    private static GraalRuntime getGraalRuntime(String className) {
        try {
            Class<?> c = Class.forName(className);
            Method m = c.getDeclaredMethod("getGraalRuntime");
            return (GraalRuntime) m.invoke(null);
        } catch (Exception e) {
            //e.printStackTrace();
            System.err.println(e);
            return null;
        }
    }
}