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