author | alanb |
Fri, 07 Apr 2017 08:05:54 +0000 | |
changeset 44545 | 83b611b88ac8 |
parent 44359 | c6761862ca0b |
child 45004 | ea3137042a61 |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
2 |
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. |
36511 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.lang.module; |
|
27 |
||
28 |
import java.nio.file.Files; |
|
29 |
import java.nio.file.Path; |
|
30 |
import java.nio.file.Paths; |
|
31 |
import java.security.AccessController; |
|
32 |
import java.security.Permission; |
|
33 |
import java.security.PrivilegedAction; |
|
34 |
import java.util.Collections; |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
35 |
import java.util.HashMap; |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
36 |
import java.util.HashSet; |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
37 |
import java.util.List; |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
38 |
import java.util.Map; |
36511 | 39 |
import java.util.Objects; |
40 |
import java.util.Optional; |
|
41 |
import java.util.Set; |
|
39050
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38457
diff
changeset
|
42 |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
43 |
import jdk.internal.module.ModuleBootstrap; |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
44 |
import jdk.internal.module.ModulePatcher; |
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
45 |
import jdk.internal.module.ModulePath; |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
46 |
import jdk.internal.module.SystemModuleFinder; |
36511 | 47 |
|
48 |
/** |
|
49 |
* A finder of modules. A {@code ModuleFinder} is used to find modules during |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
50 |
* <a href="package-summary.html#resolution">resolution</a> or |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
51 |
* <a href="package-summary.html#servicebinding">service binding</a>. |
36511 | 52 |
* |
53 |
* <p> A {@code ModuleFinder} can only find one module with a given name. A |
|
54 |
* {@code ModuleFinder} that finds modules in a sequence of directories, for |
|
55 |
* example, will locate the first occurrence of a module of a given name and |
|
56 |
* will ignore other modules of that name that appear in directories later in |
|
57 |
* the sequence. </p> |
|
58 |
* |
|
59 |
* <p> Example usage: </p> |
|
60 |
* |
|
61 |
* <pre>{@code |
|
62 |
* Path dir1, dir2, dir3; |
|
63 |
* |
|
64 |
* ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3); |
|
65 |
* |
|
66 |
* Optional<ModuleReference> omref = finder.find("jdk.foo"); |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
67 |
* omref.ifPresent(mref -> ... ); |
36511 | 68 |
* |
69 |
* }</pre> |
|
70 |
* |
|
71 |
* <p> The {@link #find(String) find} and {@link #findAll() findAll} methods |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
72 |
* defined here can fail for several reasons. These include I/O errors, errors |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
73 |
* detected parsing a module descriptor ({@code module-info.class}), or in the |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
74 |
* case of {@code ModuleFinder} returned by {@link #of ModuleFinder.of}, that |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
75 |
* two or more modules with the same name are found in a directory. |
36511 | 76 |
* When an error is detected then these methods throw {@link FindException |
77 |
* FindException} with an appropriate {@link Throwable#getCause cause}. |
|
78 |
* The behavior of a {@code ModuleFinder} after a {@code FindException} is |
|
79 |
* thrown is undefined. For example, invoking {@code find} after an exception |
|
80 |
* is thrown may or may not scan the same modules that lead to the exception. |
|
81 |
* It is recommended that a module finder be discarded after an exception is |
|
82 |
* thrown. </p> |
|
83 |
* |
|
84 |
* <p> A {@code ModuleFinder} is not required to be thread safe. </p> |
|
85 |
* |
|
86 |
* @since 9 |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
87 |
* @spec JPMS |
36511 | 88 |
*/ |
89 |
||
90 |
public interface ModuleFinder { |
|
91 |
||
92 |
/** |
|
93 |
* Finds a reference to a module of a given name. |
|
94 |
* |
|
95 |
* <p> A {@code ModuleFinder} provides a consistent view of the |
|
96 |
* modules that it locates. If {@code find} is invoked several times to |
|
97 |
* locate the same module (by name) then it will return the same result |
|
98 |
* each time. If a module is located then it is guaranteed to be a member |
|
99 |
* of the set of modules returned by the {@link #findAll() findAll} |
|
100 |
* method. </p> |
|
101 |
* |
|
102 |
* @param name |
|
103 |
* The name of the module to find |
|
104 |
* |
|
105 |
* @return A reference to a module with the given name or an empty |
|
106 |
* {@code Optional} if not found |
|
107 |
* |
|
108 |
* @throws FindException |
|
109 |
* If an error occurs finding the module |
|
110 |
* |
|
111 |
* @throws SecurityException |
|
112 |
* If denied by the security manager |
|
113 |
*/ |
|
114 |
Optional<ModuleReference> find(String name); |
|
115 |
||
116 |
/** |
|
117 |
* Returns the set of all module references that this finder can locate. |
|
118 |
* |
|
119 |
* <p> A {@code ModuleFinder} provides a consistent view of the modules |
|
120 |
* that it locates. If {@link #findAll() findAll} is invoked several times |
|
121 |
* then it will return the same (equals) result each time. For each {@code |
|
122 |
* ModuleReference} element in the returned set then it is guaranteed that |
|
123 |
* {@link #find find} will locate the {@code ModuleReference} if invoked |
|
124 |
* to find that module. </p> |
|
125 |
* |
|
126 |
* @apiNote This is important to have for methods such as {@link |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
127 |
* Configuration#resolveAndBind resolveAndBind} that need to scan the |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
128 |
* module path to find modules that provide a specific service. |
36511 | 129 |
* |
130 |
* @return The set of all module references that this finder locates |
|
131 |
* |
|
132 |
* @throws FindException |
|
133 |
* If an error occurs finding all modules |
|
134 |
* |
|
135 |
* @throws SecurityException |
|
136 |
* If denied by the security manager |
|
137 |
*/ |
|
138 |
Set<ModuleReference> findAll(); |
|
139 |
||
140 |
/** |
|
141 |
* Returns a module finder that locates the <em>system modules</em>. The |
|
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
142 |
* system modules are the modules in the Java run-time image. |
36511 | 143 |
* The module finder will always find {@code java.base}. |
144 |
* |
|
145 |
* <p> If there is a security manager set then its {@link |
|
146 |
* SecurityManager#checkPermission(Permission) checkPermission} method is |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
147 |
* invoked to check that the caller has been granted |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
148 |
* {@link RuntimePermission RuntimePermission("accessSystemModules")} |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
149 |
* to access the system modules. </p> |
36511 | 150 |
* |
151 |
* @return A {@code ModuleFinder} that locates the system modules |
|
152 |
* |
|
153 |
* @throws SecurityException |
|
154 |
* If denied by the security manager |
|
155 |
*/ |
|
156 |
static ModuleFinder ofSystem() { |
|
157 |
SecurityManager sm = System.getSecurityManager(); |
|
158 |
if (sm != null) { |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
159 |
sm.checkPermission(new RuntimePermission("accessSystemModules")); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
160 |
PrivilegedAction<ModuleFinder> pa = ModuleFinder::privilegedOfSystem; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
161 |
return AccessController.doPrivileged(pa); |
36511 | 162 |
} else { |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
163 |
return privilegedOfSystem(); |
36511 | 164 |
} |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
165 |
} |
36511 | 166 |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
167 |
/** |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
168 |
* Returns a module finder that locates the system modules. This method |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
169 |
* assumes it has permissions to access the runtime image. |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
170 |
*/ |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
171 |
private static ModuleFinder privilegedOfSystem() { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
172 |
String home = System.getProperty("java.home"); |
36511 | 173 |
Path modules = Paths.get(home, "lib", "modules"); |
174 |
if (Files.isRegularFile(modules)) { |
|
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
175 |
return SystemModuleFinder.getInstance(); |
36511 | 176 |
} else { |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
177 |
Path dir = Paths.get(home, "modules"); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
178 |
if (Files.isDirectory(dir)) { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
179 |
return privilegedOf(ModuleBootstrap.patcher(), dir); |
36511 | 180 |
} else { |
181 |
throw new InternalError("Unable to detect the run-time image"); |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
/** |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
187 |
* Returns a module finder that locates the system modules in an exploded |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
188 |
* image. The image may be patched. |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
189 |
*/ |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
190 |
private static ModuleFinder privilegedOf(ModulePatcher patcher, Path dir) { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
191 |
ModuleFinder finder = ModulePath.of(patcher, dir); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
192 |
return new ModuleFinder() { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
193 |
@Override |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
194 |
public Optional<ModuleReference> find(String name) { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
195 |
PrivilegedAction<Optional<ModuleReference>> pa = () -> finder.find(name); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
196 |
return AccessController.doPrivileged(pa); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
197 |
} |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
198 |
@Override |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
199 |
public Set<ModuleReference> findAll() { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
200 |
PrivilegedAction<Set<ModuleReference>> pa = finder::findAll; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
201 |
return AccessController.doPrivileged(pa); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
202 |
} |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
203 |
}; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
204 |
} |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
205 |
|
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
206 |
/** |
36511 | 207 |
* Returns a module finder that locates modules on the file system by |
208 |
* searching a sequence of directories and/or packaged modules. |
|
209 |
* |
|
210 |
* Each element in the given array is one of: |
|
211 |
* <ol> |
|
212 |
* <li><p> A path to a directory of modules.</p></li> |
|
213 |
* <li><p> A path to the <em>top-level</em> directory of an |
|
214 |
* <em>exploded module</em>. </p></li> |
|
215 |
* <li><p> A path to a <em>packaged module</em>. </p></li> |
|
216 |
* </ol> |
|
217 |
* |
|
218 |
* The module finder locates modules by searching each directory, exploded |
|
219 |
* module, or packaged module in array index order. It finds the first |
|
220 |
* occurrence of a module with a given name and ignores other modules of |
|
221 |
* that name that appear later in the sequence. |
|
222 |
* |
|
223 |
* <p> If an element is a path to a directory of modules then each entry in |
|
224 |
* the directory is a packaged module or the top-level directory of an |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
225 |
* exploded module. It is an error if a directory contains more than one |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
226 |
* module with the same name. If an element is a path to a directory, and |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
227 |
* that directory contains a file named {@code module-info.class}, then the |
36511 | 228 |
* directory is treated as an exploded module rather than a directory of |
229 |
* modules. </p> |
|
230 |
* |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
231 |
* <p id="automatic-modules"> The module finder returned by this method |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
232 |
* supports modules packaged as JAR files. A JAR file with a {@code |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
233 |
* module-info.class} in its top-level directory, or in a versioned entry |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
234 |
* in a {@linkplain java.util.jar.JarFile#isMultiRelease() multi-release} |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
235 |
* JAR file, is a modular JAR file and thus defines an <em>explicit</em> |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
236 |
* module. A JAR file that does not have a {@code module-info.class} in its |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
237 |
* top-level directory defines an <em>automatic module</em>, as follows: |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
238 |
* </p> |
36511 | 239 |
* |
240 |
* <ul> |
|
241 |
* |
|
242 |
* <li><p> The module {@link ModuleDescriptor#name() name}, and {@link |
|
243 |
* ModuleDescriptor#version() version} if applicable, is derived from |
|
244 |
* the file name of the JAR file as follows: </p> |
|
245 |
* |
|
246 |
* <ul> |
|
247 |
* |
|
248 |
* <li><p> The {@code .jar} suffix is removed. </p></li> |
|
249 |
* |
|
250 |
* <li><p> If the name matches the regular expression {@code |
|
251 |
* "-(\\d+(\\.|$))"} then the module name will be derived from the |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41482
diff
changeset
|
252 |
* subsequence preceding the hyphen of the first occurrence. The |
36511 | 253 |
* subsequence after the hyphen is parsed as a {@link |
254 |
* ModuleDescriptor.Version} and ignored if it cannot be parsed as |
|
255 |
* a {@code Version}. </p></li> |
|
256 |
* |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
257 |
* <li><p> All non-alphanumeric characters ({@code [^A-Za-z0-9]}) |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
258 |
* in the module name are replaced with a dot ({@code "."}), all |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
259 |
* repeating dots are replaced with one dot, and all leading and |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
260 |
* trailing dots are removed. </p></li> |
36511 | 261 |
* |
262 |
* <li><p> As an example, a JAR file named {@code foo-bar.jar} will |
|
263 |
* derive a module name {@code foo.bar} and no version. A JAR file |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
264 |
* named {@code foo-bar-1.2.3-SNAPSHOT.jar} will derive a module |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
265 |
* name {@code foo.bar} and {@code 1.2.3-SNAPSHOT} as the version. |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
266 |
* </p></li> |
36511 | 267 |
* |
268 |
* </ul></li> |
|
269 |
* |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
270 |
* <li><p> The set of packages in the module is derived from the |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
271 |
* non-directory entries in the JAR file that have names ending in |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
272 |
* "{@code .class}". A candidate package name is derived from the name |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
273 |
* using the characters up to, but not including, the last forward slash. |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
274 |
* All remaining forward slashes are replaced with dot ({@code "."}). If |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
275 |
* the resulting string is a legal package name then it is assumed to be |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
276 |
* a package name. For example, if the JAR file contains the entry |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
277 |
* "{@code p/q/Foo.class}" then the package name derived is |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
278 |
* "{@code p.q}".</p></li> |
36511 | 279 |
* |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41482
diff
changeset
|
280 |
* <li><p> The contents of entries starting with {@code |
36511 | 281 |
* META-INF/services/} are assumed to be service configuration files |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41482
diff
changeset
|
282 |
* (see {@link java.util.ServiceLoader}). If the name of a file |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
283 |
* (that follows {@code META-INF/services/}) is a legal class name |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
284 |
* then it is assumed to be the fully-qualified class name of a service |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
285 |
* type. The entries in the file are assumed to be the fully-qualified |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
286 |
* class names of provider classes. </p></li> |
36511 | 287 |
* |
288 |
* <li><p> If the JAR file has a {@code Main-Class} attribute in its |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
289 |
* main manifest then its value is the module {@link |
36511 | 290 |
* ModuleDescriptor#mainClass() main class}. </p></li> |
291 |
* |
|
292 |
* </ul> |
|
293 |
* |
|
39050
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38457
diff
changeset
|
294 |
* <p> If a {@code ModuleDescriptor} cannot be created (by means of the |
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38457
diff
changeset
|
295 |
* {@link ModuleDescriptor.Builder ModuleDescriptor.Builder} API) for an |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
296 |
* automatic module then {@code FindException} is thrown. This can arise |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
297 |
* when a legal module name cannot be derived from the file name of the JAR |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
298 |
* file, where the JAR file contains a {@code .class} in the top-level |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
299 |
* directory of the JAR file, where an entry in a service configuration |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
300 |
* file is not a legal class name or its package name is not in the set of |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
301 |
* packages derived for the module, or where the module main class is not |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
302 |
* a legal class name or its package is not in the module. </p> |
39050
9de41b79ec7e
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents:
38457
diff
changeset
|
303 |
* |
36511 | 304 |
* <p> In addition to JAR files, an implementation may also support modules |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
305 |
* that are packaged in other implementation specific module formats. If |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
306 |
* an element in the array specified to this method is a path to a directory |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
307 |
* of modules then entries in the directory that not recognized as modules |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
308 |
* are ignored. If an element in the array is a path to a packaged module |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
309 |
* that is not recognized then a {@code FindException} is thrown when the |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
310 |
* file is encountered. Paths to files that do not exist are always ignored. |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
311 |
* </p> |
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
312 |
* |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
313 |
* <p> As with automatic modules, the contents of a packaged or exploded |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
314 |
* module may need to be <em>scanned</em> in order to determine the packages |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
315 |
* in the module. Whether {@linkplain java.nio.file.Files#isHidden(Path) |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
316 |
* hidden files} are ignored or not is implementation specific and therefore |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
317 |
* not specified. If a {@code .class} file (other than {@code |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41482
diff
changeset
|
318 |
* module-info.class}) is found in the top-level directory then it is |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41482
diff
changeset
|
319 |
* assumed to be a class in the unnamed package and so {@code FindException} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41482
diff
changeset
|
320 |
* is thrown. </p> |
36511 | 321 |
* |
322 |
* <p> Finders created by this method are lazy and do not eagerly check |
|
323 |
* that the given file paths are directories or packaged modules. |
|
324 |
* Consequently, the {@code find} or {@code findAll} methods will only |
|
325 |
* fail if invoking these methods results in searching a directory or |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
326 |
* packaged module and an error is encountered. </p> |
36511 | 327 |
* |
328 |
* @param entries |
|
329 |
* A possibly-empty array of paths to directories of modules |
|
330 |
* or paths to packaged or exploded modules |
|
331 |
* |
|
332 |
* @return A {@code ModuleFinder} that locates modules on the file system |
|
333 |
*/ |
|
334 |
static ModuleFinder of(Path... entries) { |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
335 |
// special case zero entries |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
336 |
if (entries.length == 0) { |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
337 |
return new ModuleFinder() { |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
338 |
@Override |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
339 |
public Optional<ModuleReference> find(String name) { |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
340 |
Objects.requireNonNull(name); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
341 |
return Optional.empty(); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
342 |
} |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
343 |
|
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
344 |
@Override |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
345 |
public Set<ModuleReference> findAll() { |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
346 |
return Collections.emptySet(); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
347 |
} |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
348 |
}; |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
349 |
} |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
350 |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
351 |
return ModulePath.of(entries); |
36511 | 352 |
} |
353 |
||
354 |
/** |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
355 |
* Returns a module finder that is composed from a sequence of zero or more |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
356 |
* module finders. The {@link #find(String) find} method of the resulting |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
357 |
* module finder will locate a module by invoking the {@code find} method |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
358 |
* of each module finder, in array index order, until either the module is |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
359 |
* found or all module finders have been searched. The {@link #findAll() |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
360 |
* findAll} method of the resulting module finder will return a set of |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
361 |
* modules that includes all modules located by the first module finder. |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
362 |
* The set of modules will include all modules located by the second or |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
363 |
* subsequent module finder that are not located by previous module finders |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
364 |
* in the sequence. |
36511 | 365 |
* |
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
366 |
* <p> When locating modules then any exceptions or errors thrown by the |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
367 |
* {@code find} or {@code findAll} methods of the underlying module finders |
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
39050
diff
changeset
|
368 |
* will be propagated to the caller of the resulting module finder's |
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
369 |
* {@code find} or {@code findAll} methods. </p> |
36511 | 370 |
* |
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
371 |
* @param finders |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
372 |
* The array of module finders |
36511 | 373 |
* |
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
374 |
* @return A {@code ModuleFinder} that composes a sequence of module finders |
36511 | 375 |
*/ |
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
376 |
static ModuleFinder compose(ModuleFinder... finders) { |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41482
diff
changeset
|
377 |
// copy the list and check for nulls |
41482
05e75e5f3afb
8134373: use collections convenience factories in the JDK
smarks
parents:
39050
diff
changeset
|
378 |
final List<ModuleFinder> finderList = List.of(finders); |
36511 | 379 |
|
380 |
return new ModuleFinder() { |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
381 |
private final Map<String, ModuleReference> nameToModule = new HashMap<>(); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
382 |
private Set<ModuleReference> allModules; |
36511 | 383 |
|
384 |
@Override |
|
385 |
public Optional<ModuleReference> find(String name) { |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
386 |
// cached? |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
387 |
ModuleReference mref = nameToModule.get(name); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
388 |
if (mref != null) |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
389 |
return Optional.of(mref); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
390 |
Optional<ModuleReference> omref = finderList.stream() |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
391 |
.map(f -> f.find(name)) |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
392 |
.flatMap(Optional::stream) |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
393 |
.findFirst(); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
394 |
omref.ifPresent(m -> nameToModule.put(name, m)); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
395 |
return omref; |
36511 | 396 |
} |
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
397 |
|
36511 | 398 |
@Override |
399 |
public Set<ModuleReference> findAll() { |
|
38457
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
400 |
if (allModules != null) |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
401 |
return allModules; |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
402 |
// seed with modules already found |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
403 |
Set<ModuleReference> result = new HashSet<>(nameToModule.values()); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
404 |
finderList.stream() |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
405 |
.flatMap(f -> f.findAll().stream()) |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
406 |
.forEach(mref -> { |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
407 |
String name = mref.descriptor().name(); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
408 |
if (nameToModule.putIfAbsent(name, mref) == null) { |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
409 |
result.add(mref); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
410 |
} |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
411 |
}); |
3d019217e322
8152650: ModuleFinder.compose should accept varargs
alanb
parents:
37780
diff
changeset
|
412 |
allModules = Collections.unmodifiableSet(result); |
36511 | 413 |
return allModules; |
414 |
} |
|
415 |
}; |
|
416 |
} |
|
417 |
||
418 |
} |