author | rriggs |
Sun, 29 Oct 2017 18:20:53 -0400 | |
changeset 47471 | 304ef03403b1 |
parent 47216 | 71c04702a3d5 |
child 47866 | 39db80b32b69 |
permissions | -rw-r--r-- |
42703 | 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. |
42703 | 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 jdk.internal.module; |
|
27 |
||
28 |
import java.io.BufferedInputStream; |
|
29 |
import java.io.BufferedReader; |
|
30 |
import java.io.File; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.InputStreamReader; |
|
34 |
import java.io.UncheckedIOException; |
|
35 |
import java.lang.module.FindException; |
|
36 |
import java.lang.module.InvalidModuleDescriptorException; |
|
37 |
import java.lang.module.ModuleDescriptor; |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
38 |
import java.lang.module.ModuleDescriptor.Builder; |
42703 | 39 |
import java.lang.module.ModuleFinder; |
40 |
import java.lang.module.ModuleReference; |
|
41 |
import java.net.URI; |
|
42 |
import java.nio.file.DirectoryStream; |
|
43 |
import java.nio.file.Files; |
|
44 |
import java.nio.file.NoSuchFileException; |
|
45 |
import java.nio.file.Path; |
|
46 |
import java.nio.file.Paths; |
|
47 |
import java.nio.file.attribute.BasicFileAttributes; |
|
48 |
import java.util.ArrayList; |
|
49 |
import java.util.Collections; |
|
50 |
import java.util.HashMap; |
|
51 |
import java.util.List; |
|
52 |
import java.util.Map; |
|
53 |
import java.util.Objects; |
|
54 |
import java.util.Optional; |
|
55 |
import java.util.Set; |
|
56 |
import java.util.jar.Attributes; |
|
57 |
import java.util.jar.JarEntry; |
|
58 |
import java.util.jar.JarFile; |
|
59 |
import java.util.jar.Manifest; |
|
60 |
import java.util.regex.Matcher; |
|
61 |
import java.util.regex.Pattern; |
|
62 |
import java.util.stream.Collectors; |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
63 |
import java.util.zip.ZipException; |
42703 | 64 |
import java.util.zip.ZipFile; |
65 |
||
66 |
import jdk.internal.jmod.JmodFile; |
|
67 |
import jdk.internal.jmod.JmodFile.Section; |
|
68 |
import jdk.internal.perf.PerfCounter; |
|
69 |
import jdk.internal.util.jar.VersionedStream; |
|
70 |
||
71 |
||
72 |
/** |
|
73 |
* A {@code ModuleFinder} that locates modules on the file system by searching |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
74 |
* a sequence of directories or packaged modules. The ModuleFinder can be |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
75 |
* created to work in either the run-time or link-time phases. In both cases it |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
76 |
* locates modular JAR and exploded modules. When created for link-time then it |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
77 |
* additionally locates modules in JMOD files. The ModuleFinder can also |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
78 |
* optionally patch any modules that it locates with a ModulePatcher. |
42703 | 79 |
*/ |
80 |
||
81 |
public class ModulePath implements ModuleFinder { |
|
82 |
private static final String MODULE_INFO = "module-info.class"; |
|
83 |
||
84 |
// the version to use for multi-release modular JARs |
|
85 |
private final Runtime.Version releaseVersion; |
|
86 |
||
87 |
// true for the link phase (supports modules packaged in JMOD format) |
|
88 |
private final boolean isLinkPhase; |
|
89 |
||
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
90 |
// for patching modules, can be null |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
91 |
private final ModulePatcher patcher; |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
92 |
|
42703 | 93 |
// the entries on this module path |
94 |
private final Path[] entries; |
|
95 |
private int next; |
|
96 |
||
97 |
// map of module name to module reference map for modules already located |
|
98 |
private final Map<String, ModuleReference> cachedModules = new HashMap<>(); |
|
99 |
||
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
100 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
101 |
private ModulePath(Runtime.Version version, |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
102 |
boolean isLinkPhase, |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
103 |
ModulePatcher patcher, |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
104 |
Path... entries) { |
42703 | 105 |
this.releaseVersion = version; |
106 |
this.isLinkPhase = isLinkPhase; |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
107 |
this.patcher = patcher; |
42703 | 108 |
this.entries = entries.clone(); |
109 |
for (Path entry : this.entries) { |
|
110 |
Objects.requireNonNull(entry); |
|
111 |
} |
|
112 |
} |
|
113 |
||
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
114 |
/** |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
115 |
* Returns a ModuleFinder that that locates modules on the file system by |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
116 |
* searching a sequence of directories and/or packaged modules. The modules |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
117 |
* may be patched by the given ModulePatcher. |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
118 |
*/ |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
119 |
public static ModuleFinder of(ModulePatcher patcher, Path... entries) { |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
120 |
return new ModulePath(JarFile.runtimeVersion(), false, patcher, entries); |
42703 | 121 |
} |
122 |
||
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
123 |
/** |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
124 |
* Returns a ModuleFinder that that locates modules on the file system by |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
125 |
* searching a sequence of directories and/or packaged modules. |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
126 |
*/ |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
127 |
public static ModuleFinder of(Path... entries) { |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
128 |
return of((ModulePatcher)null, entries); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
129 |
} |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
130 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
131 |
/** |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
132 |
* Returns a ModuleFinder that that locates modules on the file system by |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
133 |
* searching a sequence of directories and/or packaged modules. |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
134 |
* |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
135 |
* @param version The release version to use for multi-release JAR files |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
136 |
* @param isLinkPhase {@code true} if the link phase to locate JMOD files |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
137 |
*/ |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
138 |
public static ModuleFinder of(Runtime.Version version, |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
139 |
boolean isLinkPhase, |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
140 |
Path... entries) { |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
141 |
return new ModulePath(version, isLinkPhase, null, entries); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
142 |
} |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
143 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
144 |
|
42703 | 145 |
@Override |
146 |
public Optional<ModuleReference> find(String name) { |
|
147 |
Objects.requireNonNull(name); |
|
148 |
||
149 |
// try cached modules |
|
150 |
ModuleReference m = cachedModules.get(name); |
|
151 |
if (m != null) |
|
152 |
return Optional.of(m); |
|
153 |
||
154 |
// the module may not have been encountered yet |
|
155 |
while (hasNextEntry()) { |
|
156 |
scanNextEntry(); |
|
157 |
m = cachedModules.get(name); |
|
158 |
if (m != null) |
|
159 |
return Optional.of(m); |
|
160 |
} |
|
161 |
return Optional.empty(); |
|
162 |
} |
|
163 |
||
164 |
@Override |
|
165 |
public Set<ModuleReference> findAll() { |
|
166 |
// need to ensure that all entries have been scanned |
|
167 |
while (hasNextEntry()) { |
|
168 |
scanNextEntry(); |
|
169 |
} |
|
170 |
return cachedModules.values().stream().collect(Collectors.toSet()); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Returns {@code true} if there are additional entries to scan |
|
175 |
*/ |
|
176 |
private boolean hasNextEntry() { |
|
177 |
return next < entries.length; |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Scans the next entry on the module path. A no-op if all entries have |
|
182 |
* already been scanned. |
|
183 |
* |
|
184 |
* @throws FindException if an error occurs scanning the next entry |
|
185 |
*/ |
|
186 |
private void scanNextEntry() { |
|
187 |
if (hasNextEntry()) { |
|
188 |
||
189 |
long t0 = System.nanoTime(); |
|
190 |
||
191 |
Path entry = entries[next]; |
|
192 |
Map<String, ModuleReference> modules = scan(entry); |
|
193 |
next++; |
|
194 |
||
195 |
// update cache, ignoring duplicates |
|
196 |
int initialSize = cachedModules.size(); |
|
197 |
for (Map.Entry<String, ModuleReference> e : modules.entrySet()) { |
|
198 |
cachedModules.putIfAbsent(e.getKey(), e.getValue()); |
|
199 |
} |
|
200 |
||
201 |
// update counters |
|
202 |
int added = cachedModules.size() - initialSize; |
|
203 |
moduleCount.add(added); |
|
204 |
||
205 |
scanTime.addElapsedTimeFrom(t0); |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
||
210 |
/** |
|
211 |
* Scan the given module path entry. If the entry is a directory then it is |
|
212 |
* a directory of modules or an exploded module. If the entry is a regular |
|
213 |
* file then it is assumed to be a packaged module. |
|
214 |
* |
|
215 |
* @throws FindException if an error occurs scanning the entry |
|
216 |
*/ |
|
217 |
private Map<String, ModuleReference> scan(Path entry) { |
|
218 |
||
219 |
BasicFileAttributes attrs; |
|
220 |
try { |
|
221 |
attrs = Files.readAttributes(entry, BasicFileAttributes.class); |
|
222 |
} catch (NoSuchFileException e) { |
|
223 |
return Collections.emptyMap(); |
|
224 |
} catch (IOException ioe) { |
|
225 |
throw new FindException(ioe); |
|
226 |
} |
|
227 |
||
228 |
try { |
|
229 |
||
230 |
if (attrs.isDirectory()) { |
|
231 |
Path mi = entry.resolve(MODULE_INFO); |
|
232 |
if (!Files.exists(mi)) { |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
233 |
// assume a directory of modules |
42703 | 234 |
return scanDirectory(entry); |
235 |
} |
|
236 |
} |
|
237 |
||
238 |
// packaged or exploded module |
|
239 |
ModuleReference mref = readModule(entry, attrs); |
|
240 |
if (mref != null) { |
|
241 |
String name = mref.descriptor().name(); |
|
242 |
return Collections.singletonMap(name, mref); |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
243 |
} |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
244 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
245 |
// not recognized |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
246 |
String msg; |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
247 |
if (!isLinkPhase && entry.toString().endsWith(".jmod")) { |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
248 |
msg = "JMOD format not supported at execution time"; |
42703 | 249 |
} else { |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
250 |
msg = "Module format not recognized"; |
42703 | 251 |
} |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
252 |
throw new FindException(msg + ": " + entry); |
42703 | 253 |
|
254 |
} catch (IOException ioe) { |
|
255 |
throw new FindException(ioe); |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
||
260 |
/** |
|
261 |
* Scans the given directory for packaged or exploded modules. |
|
262 |
* |
|
263 |
* @return a map of module name to ModuleReference for the modules found |
|
264 |
* in the directory |
|
265 |
* |
|
266 |
* @throws IOException if an I/O error occurs |
|
267 |
* @throws FindException if an error occurs scanning the entry or the |
|
268 |
* directory contains two or more modules with the same name |
|
269 |
*/ |
|
270 |
private Map<String, ModuleReference> scanDirectory(Path dir) |
|
271 |
throws IOException |
|
272 |
{ |
|
273 |
// The map of name -> mref of modules found in this directory. |
|
274 |
Map<String, ModuleReference> nameToReference = new HashMap<>(); |
|
275 |
||
276 |
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { |
|
277 |
for (Path entry : stream) { |
|
278 |
BasicFileAttributes attrs; |
|
279 |
try { |
|
280 |
attrs = Files.readAttributes(entry, BasicFileAttributes.class); |
|
281 |
} catch (NoSuchFileException ignore) { |
|
282 |
// file has been removed or moved, ignore for now |
|
283 |
continue; |
|
284 |
} |
|
285 |
||
286 |
ModuleReference mref = readModule(entry, attrs); |
|
287 |
||
288 |
// module found |
|
289 |
if (mref != null) { |
|
290 |
// can have at most one version of a module in the directory |
|
291 |
String name = mref.descriptor().name(); |
|
292 |
ModuleReference previous = nameToReference.put(name, mref); |
|
293 |
if (previous != null) { |
|
294 |
String fn1 = fileName(mref); |
|
295 |
String fn2 = fileName(previous); |
|
296 |
throw new FindException("Two versions of module " |
|
297 |
+ name + " found in " + dir |
|
298 |
+ " (" + fn1 + " and " + fn2 + ")"); |
|
299 |
} |
|
300 |
} |
|
301 |
} |
|
302 |
} |
|
303 |
||
304 |
return nameToReference; |
|
305 |
} |
|
306 |
||
307 |
||
308 |
/** |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
309 |
* Reads a packaged or exploded module, returning a {@code ModuleReference} |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
310 |
* to the module. Returns {@code null} if the entry is not recognized. |
42703 | 311 |
* |
312 |
* @throws IOException if an I/O error occurs |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
313 |
* @throws FindException if an error occurs parsing its module descriptor |
42703 | 314 |
*/ |
315 |
private ModuleReference readModule(Path entry, BasicFileAttributes attrs) |
|
316 |
throws IOException |
|
317 |
{ |
|
318 |
try { |
|
319 |
||
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
320 |
// exploded module |
42703 | 321 |
if (attrs.isDirectory()) { |
322 |
return readExplodedModule(entry); // may return null |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
323 |
} |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
324 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
325 |
// JAR or JMOD file |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
326 |
if (attrs.isRegularFile()) { |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
327 |
String fn = entry.getFileName().toString(); |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
328 |
boolean isDefaultFileSystem = isDefaultFileSystem(entry); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
329 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
330 |
// JAR file |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
331 |
if (fn.endsWith(".jar")) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
332 |
if (isDefaultFileSystem) { |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
333 |
return readJar(entry); |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
334 |
} else { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
335 |
// the JAR file is in a custom file system so |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
336 |
// need to copy it to the local file system |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
337 |
Path tmpdir = Files.createTempDirectory("mlib"); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
338 |
Path target = Files.copy(entry, tmpdir.resolve(fn)); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
339 |
return readJar(target); |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
340 |
} |
42703 | 341 |
} |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
342 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
343 |
// JMOD file |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
344 |
if (isDefaultFileSystem && isLinkPhase && fn.endsWith(".jmod")) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
345 |
return readJMod(entry); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
346 |
} |
42703 | 347 |
} |
348 |
||
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
349 |
return null; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
350 |
|
42703 | 351 |
} catch (InvalidModuleDescriptorException e) { |
352 |
throw new FindException("Error reading module: " + entry, e); |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Returns a string with the file name of the module if possible. |
|
358 |
* If the module location is not a file URI then return the URI |
|
359 |
* as a string. |
|
360 |
*/ |
|
361 |
private String fileName(ModuleReference mref) { |
|
362 |
URI uri = mref.location().orElse(null); |
|
363 |
if (uri != null) { |
|
364 |
if (uri.getScheme().equalsIgnoreCase("file")) { |
|
365 |
Path file = Paths.get(uri); |
|
366 |
return file.getFileName().toString(); |
|
367 |
} else { |
|
368 |
return uri.toString(); |
|
369 |
} |
|
370 |
} else { |
|
371 |
return "<unknown>"; |
|
372 |
} |
|
373 |
} |
|
374 |
||
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
375 |
// -- JMOD files -- |
42703 | 376 |
|
377 |
private Set<String> jmodPackages(JmodFile jf) { |
|
378 |
return jf.stream() |
|
379 |
.filter(e -> e.section() == Section.CLASSES) |
|
380 |
.map(JmodFile.Entry::name) |
|
381 |
.map(this::toPackageName) |
|
382 |
.flatMap(Optional::stream) |
|
383 |
.collect(Collectors.toSet()); |
|
384 |
} |
|
385 |
||
386 |
/** |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
387 |
* Returns a {@code ModuleReference} to a module in JMOD file on the |
42703 | 388 |
* file system. |
389 |
* |
|
390 |
* @throws IOException |
|
391 |
* @throws InvalidModuleDescriptorException |
|
392 |
*/ |
|
393 |
private ModuleReference readJMod(Path file) throws IOException { |
|
394 |
try (JmodFile jf = new JmodFile(file)) { |
|
395 |
ModuleInfo.Attributes attrs; |
|
396 |
try (InputStream in = jf.getInputStream(Section.CLASSES, MODULE_INFO)) { |
|
397 |
attrs = ModuleInfo.read(in, () -> jmodPackages(jf)); |
|
398 |
} |
|
399 |
return ModuleReferences.newJModModule(attrs, file); |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
||
404 |
// -- JAR files -- |
|
405 |
||
406 |
private static final String SERVICES_PREFIX = "META-INF/services/"; |
|
407 |
||
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
408 |
private static final Attributes.Name AUTOMATIC_MODULE_NAME |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
409 |
= new Attributes.Name("Automatic-Module-Name"); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
410 |
|
42703 | 411 |
/** |
412 |
* Returns the service type corresponding to the name of a services |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
413 |
* configuration file if it is a legal type name. |
42703 | 414 |
* |
415 |
* For example, if called with "META-INF/services/p.S" then this method |
|
416 |
* returns a container with the value "p.S". |
|
417 |
*/ |
|
418 |
private Optional<String> toServiceName(String cf) { |
|
419 |
assert cf.startsWith(SERVICES_PREFIX); |
|
420 |
int index = cf.lastIndexOf("/") + 1; |
|
421 |
if (index < cf.length()) { |
|
422 |
String prefix = cf.substring(0, index); |
|
423 |
if (prefix.equals(SERVICES_PREFIX)) { |
|
424 |
String sn = cf.substring(index); |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
425 |
if (Checks.isClassName(sn)) |
42703 | 426 |
return Optional.of(sn); |
427 |
} |
|
428 |
} |
|
429 |
return Optional.empty(); |
|
430 |
} |
|
431 |
||
432 |
/** |
|
433 |
* Reads the next line from the given reader and trims it of comments and |
|
434 |
* leading/trailing white space. |
|
435 |
* |
|
436 |
* Returns null if the reader is at EOF. |
|
437 |
*/ |
|
438 |
private String nextLine(BufferedReader reader) throws IOException { |
|
439 |
String ln = reader.readLine(); |
|
440 |
if (ln != null) { |
|
441 |
int ci = ln.indexOf('#'); |
|
442 |
if (ci >= 0) |
|
443 |
ln = ln.substring(0, ci); |
|
444 |
ln = ln.trim(); |
|
445 |
} |
|
446 |
return ln; |
|
447 |
} |
|
448 |
||
449 |
/** |
|
450 |
* Treat the given JAR file as a module as follows: |
|
451 |
* |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
452 |
* 1. The value of the Automatic-Module-Name attribute is the module name |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
453 |
* 2. The version, and the module name when the Automatic-Module-Name |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
454 |
* attribute is not present, is derived from the file ame of the JAR file |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
455 |
* 3. All packages are derived from the .class files in the JAR file |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
456 |
* 4. The contents of any META-INF/services configuration files are mapped |
42703 | 457 |
* to "provides" declarations |
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
458 |
* 5. The Main-Class attribute in the main attributes of the JAR manifest |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
459 |
* is mapped to the module descriptor mainClass if possible |
42703 | 460 |
*/ |
461 |
private ModuleDescriptor deriveModuleDescriptor(JarFile jf) |
|
462 |
throws IOException |
|
463 |
{ |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
464 |
// Read Automatic-Module-Name attribute if present |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
465 |
Manifest man = jf.getManifest(); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
466 |
Attributes attrs = null; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
467 |
String moduleName = null; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
468 |
if (man != null) { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
469 |
attrs = man.getMainAttributes(); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
470 |
if (attrs != null) { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
471 |
moduleName = attrs.getValue(AUTOMATIC_MODULE_NAME); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
472 |
} |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
473 |
} |
42703 | 474 |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
475 |
// Derive the version, and the module name if needed, from JAR file name |
42703 | 476 |
String fn = jf.getName(); |
477 |
int i = fn.lastIndexOf(File.separator); |
|
478 |
if (i != -1) |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
479 |
fn = fn.substring(i + 1); |
42703 | 480 |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
481 |
// drop ".jar" |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
482 |
String name = fn.substring(0, fn.length() - 4); |
42703 | 483 |
String vs = null; |
484 |
||
485 |
// find first occurrence of -${NUMBER}. or -${NUMBER}$ |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
486 |
Matcher matcher = Patterns.DASH_VERSION.matcher(name); |
42703 | 487 |
if (matcher.find()) { |
488 |
int start = matcher.start(); |
|
489 |
||
490 |
// attempt to parse the tail as a version string |
|
491 |
try { |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
492 |
String tail = name.substring(start + 1); |
42703 | 493 |
ModuleDescriptor.Version.parse(tail); |
494 |
vs = tail; |
|
495 |
} catch (IllegalArgumentException ignore) { } |
|
496 |
||
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
497 |
name = name.substring(0, start); |
42703 | 498 |
} |
499 |
||
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
500 |
// Create builder, using the name derived from file name when |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
501 |
// Automatic-Module-Name not present |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
502 |
Builder builder; |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
503 |
if (moduleName != null) { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
504 |
try { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
505 |
builder = ModuleDescriptor.newAutomaticModule(moduleName); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
506 |
} catch (IllegalArgumentException e) { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
507 |
throw new FindException(AUTOMATIC_MODULE_NAME + ": " + e.getMessage()); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
508 |
} |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
509 |
} else { |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
510 |
builder = ModuleDescriptor.newAutomaticModule(cleanModuleName(name)); |
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
511 |
} |
42703 | 512 |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
513 |
// module version if present |
42703 | 514 |
if (vs != null) |
515 |
builder.version(vs); |
|
516 |
||
517 |
// scan the names of the entries in the JAR file |
|
518 |
Map<Boolean, Set<String>> map = VersionedStream.stream(jf) |
|
519 |
.filter(e -> !e.isDirectory()) |
|
520 |
.map(JarEntry::getName) |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
521 |
.filter(e -> (e.endsWith(".class") ^ e.startsWith(SERVICES_PREFIX))) |
42703 | 522 |
.collect(Collectors.partitioningBy(e -> e.startsWith(SERVICES_PREFIX), |
523 |
Collectors.toSet())); |
|
524 |
||
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
525 |
Set<String> classFiles = map.get(Boolean.FALSE); |
42703 | 526 |
Set<String> configFiles = map.get(Boolean.TRUE); |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
527 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
528 |
// the packages containing class files |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
529 |
Set<String> packages = classFiles.stream() |
42703 | 530 |
.map(this::toPackageName) |
531 |
.flatMap(Optional::stream) |
|
532 |
.distinct() |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
533 |
.collect(Collectors.toSet()); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
534 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
535 |
// all packages are exported and open |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
536 |
builder.packages(packages); |
42703 | 537 |
|
538 |
// map names of service configuration files to service names |
|
539 |
Set<String> serviceNames = configFiles.stream() |
|
540 |
.map(this::toServiceName) |
|
541 |
.flatMap(Optional::stream) |
|
542 |
.collect(Collectors.toSet()); |
|
543 |
||
544 |
// parse each service configuration file |
|
545 |
for (String sn : serviceNames) { |
|
546 |
JarEntry entry = jf.getJarEntry(SERVICES_PREFIX + sn); |
|
547 |
List<String> providerClasses = new ArrayList<>(); |
|
548 |
try (InputStream in = jf.getInputStream(entry)) { |
|
549 |
BufferedReader reader |
|
550 |
= new BufferedReader(new InputStreamReader(in, "UTF-8")); |
|
551 |
String cn; |
|
552 |
while ((cn = nextLine(reader)) != null) { |
|
553 |
if (cn.length() > 0) { |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
554 |
String pn = packageName(cn); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
555 |
if (!packages.contains(pn)) { |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
556 |
String msg = "Provider class " + cn + " not in module"; |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
557 |
throw new InvalidModuleDescriptorException(msg); |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
558 |
} |
42703 | 559 |
providerClasses.add(cn); |
560 |
} |
|
561 |
} |
|
562 |
} |
|
563 |
if (!providerClasses.isEmpty()) |
|
564 |
builder.provides(sn, providerClasses); |
|
565 |
} |
|
566 |
||
567 |
// Main-Class attribute if it exists |
|
45652
33342314ce89
8181087: Module system implementation refresh (6/2017)
alanb
parents:
45004
diff
changeset
|
568 |
if (attrs != null) { |
42703 | 569 |
String mainClass = attrs.getValue(Attributes.Name.MAIN_CLASS); |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
570 |
if (mainClass != null) { |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
571 |
mainClass = mainClass.replace("/", "."); |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
572 |
if (Checks.isClassName(mainClass)) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
573 |
String pn = packageName(mainClass); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
574 |
if (packages.contains(pn)) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
575 |
builder.mainClass(mainClass); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
576 |
} |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
577 |
} |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
578 |
} |
42703 | 579 |
} |
580 |
||
581 |
return builder.build(); |
|
582 |
} |
|
583 |
||
584 |
/** |
|
585 |
* Patterns used to derive the module name from a JAR file name. |
|
586 |
*/ |
|
587 |
private static class Patterns { |
|
588 |
static final Pattern DASH_VERSION = Pattern.compile("-(\\d+(\\.|$))"); |
|
589 |
static final Pattern NON_ALPHANUM = Pattern.compile("[^A-Za-z0-9]"); |
|
590 |
static final Pattern REPEATING_DOTS = Pattern.compile("(\\.)(\\1)+"); |
|
591 |
static final Pattern LEADING_DOTS = Pattern.compile("^\\."); |
|
592 |
static final Pattern TRAILING_DOTS = Pattern.compile("\\.$"); |
|
593 |
} |
|
594 |
||
595 |
/** |
|
596 |
* Clean up candidate module name derived from a JAR file name. |
|
597 |
*/ |
|
598 |
private static String cleanModuleName(String mn) { |
|
599 |
// replace non-alphanumeric |
|
600 |
mn = Patterns.NON_ALPHANUM.matcher(mn).replaceAll("."); |
|
601 |
||
602 |
// collapse repeating dots |
|
603 |
mn = Patterns.REPEATING_DOTS.matcher(mn).replaceAll("."); |
|
604 |
||
605 |
// drop leading dots |
|
606 |
if (mn.length() > 0 && mn.charAt(0) == '.') |
|
607 |
mn = Patterns.LEADING_DOTS.matcher(mn).replaceAll(""); |
|
608 |
||
609 |
// drop trailing dots |
|
610 |
int len = mn.length(); |
|
611 |
if (len > 0 && mn.charAt(len-1) == '.') |
|
612 |
mn = Patterns.TRAILING_DOTS.matcher(mn).replaceAll(""); |
|
613 |
||
614 |
return mn; |
|
615 |
} |
|
616 |
||
617 |
private Set<String> jarPackages(JarFile jf) { |
|
618 |
return VersionedStream.stream(jf) |
|
619 |
.filter(e -> !e.isDirectory()) |
|
620 |
.map(JarEntry::getName) |
|
621 |
.map(this::toPackageName) |
|
622 |
.flatMap(Optional::stream) |
|
623 |
.collect(Collectors.toSet()); |
|
624 |
} |
|
625 |
||
626 |
/** |
|
627 |
* Returns a {@code ModuleReference} to a module in modular JAR file on |
|
628 |
* the file system. |
|
629 |
* |
|
630 |
* @throws IOException |
|
631 |
* @throws FindException |
|
632 |
* @throws InvalidModuleDescriptorException |
|
633 |
*/ |
|
634 |
private ModuleReference readJar(Path file) throws IOException { |
|
635 |
try (JarFile jf = new JarFile(file.toFile(), |
|
636 |
true, // verify |
|
637 |
ZipFile.OPEN_READ, |
|
638 |
releaseVersion)) |
|
639 |
{ |
|
640 |
ModuleInfo.Attributes attrs; |
|
641 |
JarEntry entry = jf.getJarEntry(MODULE_INFO); |
|
642 |
if (entry == null) { |
|
643 |
||
644 |
// no module-info.class so treat it as automatic module |
|
645 |
try { |
|
646 |
ModuleDescriptor md = deriveModuleDescriptor(jf); |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
647 |
attrs = new ModuleInfo.Attributes(md, null, null, null); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
648 |
} catch (RuntimeException e) { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
649 |
throw new FindException("Unable to derive module descriptor for " |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
650 |
+ jf.getName(), e); |
42703 | 651 |
} |
652 |
||
653 |
} else { |
|
654 |
attrs = ModuleInfo.read(jf.getInputStream(entry), |
|
655 |
() -> jarPackages(jf)); |
|
656 |
} |
|
657 |
||
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
658 |
return ModuleReferences.newJarModule(attrs, patcher, file); |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
659 |
} catch (ZipException e) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
660 |
throw new FindException("Error reading " + file, e); |
42703 | 661 |
} |
662 |
} |
|
663 |
||
664 |
||
665 |
// -- exploded directories -- |
|
666 |
||
667 |
private Set<String> explodedPackages(Path dir) { |
|
668 |
try { |
|
669 |
return Files.find(dir, Integer.MAX_VALUE, |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
670 |
((path, attrs) -> attrs.isRegularFile() && !isHidden(path))) |
42703 | 671 |
.map(path -> dir.relativize(path)) |
672 |
.map(this::toPackageName) |
|
673 |
.flatMap(Optional::stream) |
|
674 |
.collect(Collectors.toSet()); |
|
675 |
} catch (IOException x) { |
|
676 |
throw new UncheckedIOException(x); |
|
677 |
} |
|
678 |
} |
|
679 |
||
680 |
/** |
|
681 |
* Returns a {@code ModuleReference} to an exploded module on the file |
|
682 |
* system or {@code null} if {@code module-info.class} not found. |
|
683 |
* |
|
684 |
* @throws IOException |
|
685 |
* @throws InvalidModuleDescriptorException |
|
686 |
*/ |
|
687 |
private ModuleReference readExplodedModule(Path dir) throws IOException { |
|
688 |
Path mi = dir.resolve(MODULE_INFO); |
|
689 |
ModuleInfo.Attributes attrs; |
|
690 |
try (InputStream in = Files.newInputStream(mi)) { |
|
691 |
attrs = ModuleInfo.read(new BufferedInputStream(in), |
|
692 |
() -> explodedPackages(dir)); |
|
693 |
} catch (NoSuchFileException e) { |
|
694 |
// for now |
|
695 |
return null; |
|
696 |
} |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
697 |
return ModuleReferences.newExplodedModule(attrs, patcher, dir); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
698 |
} |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
699 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
700 |
/** |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
701 |
* Maps a type name to its package name. |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
702 |
*/ |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
703 |
private static String packageName(String cn) { |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
704 |
int index = cn.lastIndexOf('.'); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
705 |
return (index == -1) ? "" : cn.substring(0, index); |
42703 | 706 |
} |
707 |
||
708 |
/** |
|
709 |
* Maps the name of an entry in a JAR or ZIP file to a package name. |
|
710 |
* |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
711 |
* @throws InvalidModuleDescriptorException if the name is a class file in |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
712 |
* the top-level directory of the JAR/ZIP file (and it's not |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
713 |
* module-info.class) |
42703 | 714 |
*/ |
715 |
private Optional<String> toPackageName(String name) { |
|
716 |
assert !name.endsWith("/"); |
|
717 |
int index = name.lastIndexOf("/"); |
|
718 |
if (index == -1) { |
|
719 |
if (name.endsWith(".class") && !name.equals(MODULE_INFO)) { |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
720 |
String msg = name + " found in top-level directory" |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
721 |
+ " (unnamed package not allowed in module)"; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
722 |
throw new InvalidModuleDescriptorException(msg); |
42703 | 723 |
} |
724 |
return Optional.empty(); |
|
725 |
} |
|
726 |
||
727 |
String pn = name.substring(0, index).replace('/', '.'); |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
728 |
if (Checks.isPackageName(pn)) { |
42703 | 729 |
return Optional.of(pn); |
730 |
} else { |
|
731 |
// not a valid package name |
|
732 |
return Optional.empty(); |
|
733 |
} |
|
734 |
} |
|
735 |
||
736 |
/** |
|
737 |
* Maps the relative path of an entry in an exploded module to a package |
|
738 |
* name. |
|
739 |
* |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
740 |
* @throws InvalidModuleDescriptorException if the name is a class file in |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
741 |
* the top-level directory (and it's not module-info.class) |
42703 | 742 |
*/ |
743 |
private Optional<String> toPackageName(Path file) { |
|
744 |
assert file.getRoot() == null; |
|
745 |
||
746 |
Path parent = file.getParent(); |
|
747 |
if (parent == null) { |
|
748 |
String name = file.toString(); |
|
749 |
if (name.endsWith(".class") && !name.equals(MODULE_INFO)) { |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
750 |
String msg = name + " found in top-level directory" |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
751 |
+ " (unnamed package not allowed in module)"; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
752 |
throw new InvalidModuleDescriptorException(msg); |
42703 | 753 |
} |
754 |
return Optional.empty(); |
|
755 |
} |
|
756 |
||
757 |
String pn = parent.toString().replace(File.separatorChar, '.'); |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42703
diff
changeset
|
758 |
if (Checks.isPackageName(pn)) { |
42703 | 759 |
return Optional.of(pn); |
760 |
} else { |
|
761 |
// not a valid package name |
|
762 |
return Optional.empty(); |
|
763 |
} |
|
764 |
} |
|
765 |
||
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
766 |
/** |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
767 |
* Returns true if the given file exists and is a hidden file |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
768 |
*/ |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
769 |
private boolean isHidden(Path file) { |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
770 |
try { |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
771 |
return Files.isHidden(file); |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
772 |
} catch (IOException ioe) { |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
773 |
return false; |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
774 |
} |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
775 |
} |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
776 |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
777 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
778 |
/** |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
779 |
* Return true if a path locates a path in the default file system |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
780 |
*/ |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
781 |
private boolean isDefaultFileSystem(Path path) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
782 |
return path.getFileSystem().provider() |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
783 |
.getScheme().equalsIgnoreCase("file"); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
784 |
} |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
785 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
786 |
|
42703 | 787 |
private static final PerfCounter scanTime |
788 |
= PerfCounter.newPerfCounter("jdk.module.finder.modulepath.scanTime"); |
|
789 |
private static final PerfCounter moduleCount |
|
790 |
= PerfCounter.newPerfCounter("jdk.module.finder.modulepath.modules"); |
|
791 |
} |