This repository has been archived by the owner on Mar 2, 2019. It is now read-only.
/
ClassPath.java
488 lines (438 loc) · 16.6 KB
/
ClassPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.tools.java;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.zip.*;
import java.util.Enumeration;
import java.util.Map;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Set;
import java.util.LinkedHashSet;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.ProviderNotFoundException;
import java.nio.file.spi.FileSystemProvider;
/**
* This class is used to represent a class path, which can contain both
* directories and zip files.
*
* WARNING: The contents of this source file are not part of any
* supported API. Code that depends on them does so at its own risk:
* they are subject to change or removal without notice.
*/
public
class ClassPath {
private FileSystem getJrtFileSystem() {
return FileSystems.getFileSystem(URI.create("jrt:/"));
}
static final char dirSeparator = File.pathSeparatorChar;
/**
* The original class path string
*/
String pathstr;
/**
* List of class path entries
*/
private ClassPathEntry[] path;
/**
* Build a class path from the specified path string
*/
public ClassPath(String pathstr) {
init(pathstr);
}
/**
* Build a class path from the specified array of class path
* element strings. This constructor, and the corresponding
* "init" method, were added as part of the fix for 6473331, which
* adds support for Class-Path manifest entries in JAR files to
* rmic. It is conceivable that the value of a Class-Path
* manifest entry will contain a path separator, which would cause
* incorrect behavior if the expanded path were passed to the
* previous constructor as a single path-separator-delimited
* string; use of this constructor avoids that problem.
*/
public ClassPath(String[] patharray) {
init(patharray);
}
/**
* Build a default class path from the path strings specified by
* the properties sun.boot.class.path and env.class.path, in that
* order.
*/
public ClassPath() {
// though this property is removed. Check for null and use only
// if it is not null (when bootstrap JDK is used).
String syscp = System.getProperty("sun.boot.class.path");
String envcp = System.getProperty("env.class.path");
if (envcp == null) envcp = ".";
// add syscp only if not null!
String cp = syscp == null? envcp : (syscp + File.pathSeparator + envcp);
init(cp);
}
private void init(String pathstr) {
int i, j, n;
// Save original class path string
this.pathstr = pathstr;
if (pathstr.length() == 0) {
this.path = new ClassPathEntry[0];
}
// Count the number of path separators
i = n = 0;
while ((i = pathstr.indexOf(dirSeparator, i)) != -1) {
n++; i++;
}
// Build the class path
ClassPathEntry[] path = new ClassPathEntry[n+1];
int len = pathstr.length();
for (i = n = 0; i < len; i = j + 1) {
if ((j = pathstr.indexOf(dirSeparator, i)) == -1) {
j = len;
}
if (i == j) {
path[n++] = new DirClassPathEntry(new File("."));
} else {
String filename = pathstr.substring(i, j);
File file = new File(filename);
if (file.isFile()) {
try {
ZipFile zip = new ZipFile(file);
path[n++] = new ZipClassPathEntry(zip);
} catch (ZipException e) {
} catch (IOException e) {
// Ignore exceptions, at least for now...
}
} else {
path[n++] = new DirClassPathEntry(file);
}
}
}
// add jrt file system at the end
try {
FileSystem fs = getJrtFileSystem();
path[n++] = new JrtClassPathEntry(fs);
} catch (ProviderNotFoundException ignored) {
// this could happen during jdk build with earlier JDK as bootstrap
}
// Trim class path to exact size
this.path = new ClassPathEntry[n];
System.arraycopy((Object)path, 0, (Object)this.path, 0, n);
}
private void init(String[] patharray) {
// Save original class path string
if (patharray.length == 0) {
this.pathstr = "";
} else {
StringBuilder sb = new StringBuilder(patharray[0]);
for (int i = 1; i < patharray.length; i++) {
sb.append(File.pathSeparatorChar);
sb.append(patharray[i]);
}
this.pathstr = sb.toString();
}
// Build the class path
ClassPathEntry[] path = new ClassPathEntry[patharray.length + 1];
int n = 0;
for (String name : patharray) {
File file = new File(name);
if (file.isFile()) {
try {
ZipFile zip = new ZipFile(file);
path[n++] = new ZipClassPathEntry(zip);
} catch (ZipException e) {
} catch (IOException e) {
// Ignore exceptions, at least for now...
}
} else {
path[n++] = new DirClassPathEntry(file);
}
}
// add jrt file system at the end
try {
FileSystem fs = getJrtFileSystem();
path[n++] = new JrtClassPathEntry(fs);
} catch (ProviderNotFoundException ignored) {
// this could happen with earlier version of JDK used as bootstrap
}
// Trim class path to exact size
this.path = new ClassPathEntry[n];
System.arraycopy((Object)path, 0, (Object)this.path, 0, n);
}
/**
* Find the specified directory in the class path
*/
public ClassFile getDirectory(String name) {
return getFile(name, true);
}
/**
* Load the specified file from the class path
*/
public ClassFile getFile(String name) {
return getFile(name, false);
}
private final String fileSeparatorChar = "" + File.separatorChar;
private ClassFile getFile(String name, boolean isDirectory) {
String subdir = name;
String basename = "";
if (!isDirectory) {
int i = name.lastIndexOf(File.separatorChar);
subdir = name.substring(0, i + 1);
basename = name.substring(i + 1);
} else if (!subdir.equals("")
&& !subdir.endsWith(fileSeparatorChar)) {
// zip files are picky about "foo" vs. "foo/".
// also, the getFiles caches are keyed with a trailing /
subdir = subdir + File.separatorChar;
name = subdir; // Note: isDirectory==true & basename==""
}
for (int i = 0; i < path.length; i++) {
ClassFile cf = path[i].getFile(name, subdir, basename, isDirectory);
if (cf != null) {
return cf;
}
}
return null;
}
/**
* Returns list of files given a package name and extension.
*/
public Enumeration<ClassFile> getFiles(String pkg, String ext) {
Hashtable<String, ClassFile> files = new Hashtable<>();
for (int i = path.length; --i >= 0; ) {
path[i].fillFiles(pkg, ext, files);
}
return files.elements();
}
/**
* Release resources.
*/
public void close() throws IOException {
for (int i = path.length; --i >= 0; ) {
path[i].close();
}
}
/**
* Returns original class path string
*/
public String toString() {
return pathstr;
}
}
/**
* A class path entry, which can either be a directory or an open zip file or an open jimage filesystem.
*/
abstract class ClassPathEntry {
abstract ClassFile getFile(String name, String subdir, String basename, boolean isDirectory);
abstract void fillFiles(String pkg, String ext, Hashtable<String, ClassFile> files);
abstract void close() throws IOException;
}
// a ClassPathEntry that represents a directory
final class DirClassPathEntry extends ClassPathEntry {
private final File dir;
DirClassPathEntry(File dir) {
this.dir = dir;
}
private final Hashtable<String, String[]> subdirs = new Hashtable<>(29); // cache of sub-directory listings:
private String[] getFiles(String subdir) {
String files[] = subdirs.get(subdir);
if (files == null) {
files = computeFiles(subdir);
subdirs.put(subdir, files);
}
return files;
}
private String[] computeFiles(String subdir) {
File sd = new File(dir.getPath(), subdir);
String[] files = null;
if (sd.isDirectory()) {
files = sd.list();
if (files == null) {
// should not happen, but just in case, fail silently
files = new String[0];
}
if (files.length == 0) {
String nonEmpty[] = { "" };
files = nonEmpty;
}
} else {
files = new String[0];
}
return files;
}
ClassFile getFile(String name, String subdir, String basename, boolean isDirectory) {
File file = new File(dir.getPath(), name);
String list[] = getFiles(subdir);
if (isDirectory) {
if (list.length > 0) {
return ClassFile.newClassFile(file);
}
} else {
for (int j = 0; j < list.length; j++) {
if (basename.equals(list[j])) {
// Don't bother checking !file.isDir,
// since we only look for names which
// cannot already be packages (foo.java, etc).
return ClassFile.newClassFile(file);
}
}
}
return null;
}
void fillFiles(String pkg, String ext, Hashtable<String, ClassFile> files) {
String[] list = getFiles(pkg);
for (int j = 0; j < list.length; j++) {
String name = list[j];
if (name.endsWith(ext)) {
name = pkg + File.separatorChar + name;
File file = new File(dir.getPath(), name);
files.put(name, ClassFile.newClassFile(file));
}
}
}
void close() throws IOException {
}
}
// a ClassPathEntry that represents a .zip or a .jar file
final class ZipClassPathEntry extends ClassPathEntry {
private final ZipFile zip;
ZipClassPathEntry(ZipFile zip) {
this.zip = zip;
}
void close() throws IOException {
zip.close();
}
ClassFile getFile(String name, String subdir, String basename, boolean isDirectory) {
String newname = name.replace(File.separatorChar, '/');
ZipEntry entry = zip.getEntry(newname);
return entry != null? ClassFile.newClassFile(zip, entry) : null;
}
void fillFiles(String pkg, String ext, Hashtable<String, ClassFile> files) {
Enumeration<? extends ZipEntry> e = zip.entries();
while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry)e.nextElement();
String name = entry.getName();
name = name.replace('/', File.separatorChar);
if (name.startsWith(pkg) && name.endsWith(ext)) {
files.put(name, ClassFile.newClassFile(zip, entry));
}
}
}
}
// a ClassPathEntry that represents jrt file system
final class JrtClassPathEntry extends ClassPathEntry {
private final FileSystem fs;
// package name to package directory path mapping (lazily filled)
private final Map<String, Path> pkgDirs;
JrtClassPathEntry(FileSystem fs) {
this.fs = fs;
this.pkgDirs = new HashMap<>();
}
void close() throws IOException {
}
// from pkgName (internal separator '/') to it's Path in jrtfs
synchronized Path getPackagePath(String pkgName) throws IOException {
// check the cache first
if (pkgDirs.containsKey(pkgName)) {
return pkgDirs.get(pkgName);
}
Path pkgLink = fs.getPath("/packages/" + pkgName.replace('/', '.'));
// check if /packages/$PACKAGE directory exists
if (Files.isDirectory(pkgLink)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(pkgLink)) {
for (Path p : stream) {
// find first symbolic link to module directory
if (Files.isSymbolicLink(p)) {
Path modDir = Files.readSymbolicLink(p);
if (Files.isDirectory(modDir)) {
// get package subdirectory under /modules/$MODULE/
Path pkgDir = fs.getPath(modDir.toString() + "/" + pkgName);
if (Files.isDirectory(pkgDir)) {
// it is a package directory only if contains
// at least one .class file
try (DirectoryStream<Path> pstream =
Files.newDirectoryStream(pkgDir)) {
for (Path f : pstream) {
if (Files.isRegularFile(f)
&& f.toString().endsWith(".class")) {
pkgDirs.put(pkgName, pkgDir);
return pkgDir;
}
}
}
}
}
}
}
}
}
return null;
}
// fully qualified (internal) class name to it's Path in jrtfs
Path getClassPath(String clsName) throws IOException {
int index = clsName.lastIndexOf('/');
if (index == -1) {
return null;
}
Path pkgPath = getPackagePath(clsName.substring(0, index));
return pkgPath == null? null : fs.getPath(pkgPath + "/" + clsName.substring(index + 1));
}
ClassFile getFile(String name, String subdir, String basename, boolean isDirectory) {
try {
name = name.replace(File.separatorChar, '/');
Path cp = getClassPath(name);
return cp == null? null : ClassFile.newClassFile(cp);
} catch (IOException ioExp) {
throw new UncheckedIOException(ioExp);
}
}
void fillFiles(String pkg, String ext, Hashtable<String, ClassFile> files) {
Path dir;
try {
dir = getPackagePath(pkg);
if (dir == null) {
return;
}
} catch (IOException ioExp) {
throw new UncheckedIOException(ioExp);
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path p : stream) {
String name = p.toString();
name = name.replace('/', File.separatorChar);
if (name.startsWith(pkg) && name.endsWith(ext)) {
files.put(name, ClassFile.newClassFile(p));
}
}
} catch (IOException ioExp) {
throw new UncheckedIOException(ioExp);
}
}
}