Mercurial > hg > graal-compiler
comparison graal/com.oracle.jvmci.runtime/src/com/oracle/jvmci/runtime/Services.java @ 21538:c1e2fdb5fea3
removed more dependencies from JVMCI classes to non-JVMCI classes (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Mon, 25 May 2015 17:20:39 +0200 |
parents | graal/com.oracle.graal.api.runtime/src/com/oracle/graal/api/runtime/Services.java@c198e397bb59 |
children | d563baeca9df |
comparison
equal
deleted
inserted
replaced
21537:ca14581fadc4 | 21538:c1e2fdb5fea3 |
---|---|
1 /* | |
2 * Copyright (c) 2014, 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 package com.oracle.jvmci.runtime; | |
24 | |
25 import static java.lang.String.*; | |
26 | |
27 import java.util.*; | |
28 | |
29 /** | |
30 * A mechanism on top of the standard {@link ServiceLoader} that enables a runtime to efficiently | |
31 * load services marked by {@link Service}. This may be important for services loaded early in the | |
32 * runtime initialization process. | |
33 */ | |
34 public class Services { | |
35 | |
36 private static final ClassValue<List<Service>> cache = new ClassValue<List<Service>>() { | |
37 @Override | |
38 protected List<Service> computeValue(Class<?> type) { | |
39 Service[] names = getServiceImpls(type); | |
40 if (names == null || names.length == 0) { | |
41 throw new InternalError( | |
42 format("No implementations for %s found (ensure %s extends %s and that in suite.py the \"annotationProcessors\" attribute for the project enclosing %s includes \"com.oracle.graal.service.processor\")", | |
43 type.getSimpleName(), type.getSimpleName(), Service.class, type.getSimpleName())); | |
44 } | |
45 return Arrays.asList(names); | |
46 } | |
47 }; | |
48 | |
49 /** | |
50 * Gets an {@link Iterable} of the implementations available for a given service. | |
51 */ | |
52 @SuppressWarnings("unchecked") | |
53 public static <S> Iterable<S> load(Class<S> service) { | |
54 if (Service.class.isAssignableFrom(service)) { | |
55 try { | |
56 return (Iterable<S>) cache.get(service); | |
57 } catch (UnsatisfiedLinkError e) { | |
58 // Fall back to standard SerivceLoader | |
59 } | |
60 } | |
61 return ServiceLoader.load(service, Services.class.getClassLoader()); | |
62 } | |
63 | |
64 private static native <S> S[] getServiceImpls(Class<?> service); | |
65 } |