author | zgu |
Fri, 18 Oct 2019 13:07:46 -0400 | |
changeset 58693 | 3f35a9efd7de |
parent 54669 | ad45b3802d4e |
child 58851 | f1e6442241ca |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
52433
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
43972 | 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 jdk.vm.ci.services; |
|
24 |
||
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
25 |
import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
26 |
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
27 |
|
43972 | 28 |
import java.util.ArrayList; |
29 |
import java.util.List; |
|
44836 | 30 |
import java.util.ServiceLoader; |
31 |
||
43972 | 32 |
/** |
33 |
* Service-provider class for the runtime to locate providers of JVMCI services where the latter are |
|
44836 | 34 |
* not in packages exported by the JVMCI module. As part of instantiating a |
35 |
* {@link JVMCIServiceLocator}, all JVMCI packages will be opened to the module defining the class |
|
36 |
* of the instantiated object. |
|
43972 | 37 |
* |
38 |
* While the {@link #getProvider(Class)} method can be used directly, it's usually easier to use |
|
39 |
* {@link #getProviders(Class)}. |
|
40 |
*/ |
|
41 |
public abstract class JVMCIServiceLocator { |
|
42 |
||
43 |
private static Void checkPermission() { |
|
44 |
SecurityManager sm = System.getSecurityManager(); |
|
45 |
if (sm != null) { |
|
46 |
sm.checkPermission(new JVMCIPermission()); |
|
47 |
} |
|
48 |
return null; |
|
49 |
} |
|
50 |
||
51 |
@SuppressWarnings("unused") |
|
52 |
private JVMCIServiceLocator(Void ignore) { |
|
53 |
} |
|
54 |
||
55 |
/** |
|
44836 | 56 |
* Creates a capability for accessing JVMCI. Once successfully instantiated, JVMCI opens all its |
57 |
* packages to the module defining the type of this object. |
|
43972 | 58 |
* |
59 |
* @throws SecurityException if a security manager has been installed and it denies |
|
60 |
* {@link JVMCIPermission} |
|
61 |
*/ |
|
62 |
protected JVMCIServiceLocator() { |
|
63 |
this(checkPermission()); |
|
44836 | 64 |
Services.checkJVMCIEnabled(); |
52433
a0ea20bc7af0
8213347: [JVMCI] remove use of reflection in JVMCI
dnsimon
parents:
47216
diff
changeset
|
65 |
Services.openJVMCITo(getClass().getModule()); |
43972 | 66 |
} |
67 |
||
68 |
/** |
|
69 |
* Gets the provider of the service defined by {@code service} or {@code null} if this object |
|
70 |
* does not have a provider for {@code service}. |
|
71 |
*/ |
|
44836 | 72 |
protected abstract <S> S getProvider(Class<S> service); |
43972 | 73 |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
74 |
private static volatile List<JVMCIServiceLocator> cachedLocators; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
75 |
|
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
76 |
private static Iterable<JVMCIServiceLocator> getJVMCIServiceLocators() { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
77 |
Iterable<JVMCIServiceLocator> result = cachedLocators; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
78 |
if (result != null) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
79 |
return result; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
80 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
81 |
result = ServiceLoader.load(JVMCIServiceLocator.class, ClassLoader.getSystemClassLoader()); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
82 |
if (IS_BUILDING_NATIVE_IMAGE) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
83 |
ArrayList<JVMCIServiceLocator> l = new ArrayList<>(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
84 |
for (JVMCIServiceLocator locator: result) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
85 |
l.add(locator); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
86 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
87 |
l.trimToSize(); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
88 |
cachedLocators = l; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
89 |
return l; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
90 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
91 |
return result; |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
92 |
} |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
93 |
|
43972 | 94 |
/** |
44836 | 95 |
* Gets the providers of the service defined by {@code service} by querying the available |
96 |
* {@link JVMCIServiceLocator} providers. |
|
97 |
* |
|
98 |
* @throws SecurityException if a security manager is present and it denies |
|
99 |
* {@link JVMCIPermission} |
|
43972 | 100 |
*/ |
101 |
public static <S> List<S> getProviders(Class<S> service) { |
|
44836 | 102 |
Services.checkJVMCIEnabled(); |
103 |
SecurityManager sm = System.getSecurityManager(); |
|
104 |
if (sm != null) { |
|
105 |
sm.checkPermission(new JVMCIPermission()); |
|
106 |
} |
|
43972 | 107 |
List<S> providers = new ArrayList<>(); |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52433
diff
changeset
|
108 |
for (JVMCIServiceLocator access : getJVMCIServiceLocators()) { |
43972 | 109 |
S provider = access.getProvider(service); |
110 |
if (provider != null) { |
|
111 |
providers.add(provider); |
|
112 |
} |
|
113 |
} |
|
114 |
return providers; |
|
115 |
} |
|
116 |
} |