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