author | serb |
Sat, 09 Jun 2018 13:33:35 -0700 | |
changeset 50647 | a98ff7c2103d |
parent 47989 | f1ad41a05fb7 |
child 51685 | 1f70116be2df |
permissions | -rw-r--r-- |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
1 |
/* |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
2 |
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
4 |
* |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
10 |
* |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
15 |
* accompanied this code). |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
16 |
* |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
20 |
* |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
23 |
* questions. |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
24 |
*/ |
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
30846
diff
changeset
|
25 |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
26 |
package com.sun.tools.jdeps; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
27 |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41443
diff
changeset
|
28 |
import com.sun.tools.classfile.AccessFlags; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
29 |
import com.sun.tools.classfile.ClassFile; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
30 |
import com.sun.tools.classfile.ConstantPoolException; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
31 |
import com.sun.tools.classfile.Dependencies.ClassFileError; |
36526 | 32 |
|
38530 | 33 |
import java.io.Closeable; |
27579 | 34 |
import java.io.File; |
35 |
import java.io.FileNotFoundException; |
|
36 |
import java.io.IOException; |
|
37 |
import java.io.InputStream; |
|
36526 | 38 |
import java.io.UncheckedIOException; |
27579 | 39 |
import java.nio.file.FileSystem; |
40 |
import java.nio.file.FileSystems; |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
41 |
import java.nio.file.Files; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
42 |
import java.nio.file.Path; |
36526 | 43 |
import java.util.ArrayList; |
44 |
import java.util.Collections; |
|
45 |
import java.util.Enumeration; |
|
46 |
import java.util.Iterator; |
|
47 |
import java.util.List; |
|
48 |
import java.util.NoSuchElementException; |
|
49 |
import java.util.Set; |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
50 |
import java.util.jar.JarEntry; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
51 |
import java.util.jar.JarFile; |
27579 | 52 |
import java.util.stream.Collectors; |
38529
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
53 |
import java.util.stream.Stream; |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
54 |
import java.util.zip.ZipFile; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
55 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
56 |
/** |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
57 |
* ClassFileReader reads ClassFile(s) of a given path that can be |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
58 |
* a .class file, a directory, or a JAR file. |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
59 |
*/ |
38530 | 60 |
public class ClassFileReader implements Closeable { |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
61 |
/** |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
62 |
* Returns a ClassFileReader instance of a given path. |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
63 |
*/ |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16550
diff
changeset
|
64 |
public static ClassFileReader newInstance(Path path) throws IOException { |
41442
14db641d4a9f
8166846: jdeps fails to generate module info if there is any class in unnamed package
mchung
parents:
41164
diff
changeset
|
65 |
return newInstance(path, null); |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
66 |
} |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
67 |
|
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
68 |
/** |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
69 |
* Returns a ClassFileReader instance of a given path. |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
70 |
*/ |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
71 |
public static ClassFileReader newInstance(Path path, Runtime.Version version) throws IOException { |
38529
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
72 |
if (Files.notExists(path)) { |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16550
diff
changeset
|
73 |
throw new FileNotFoundException(path.toString()); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
74 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
75 |
|
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16550
diff
changeset
|
76 |
if (Files.isDirectory(path)) { |
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16550
diff
changeset
|
77 |
return new DirectoryReader(path); |
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16550
diff
changeset
|
78 |
} else if (path.getFileName().toString().endsWith(".jar")) { |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
79 |
return new JarFileReader(path, version); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
80 |
} else { |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16550
diff
changeset
|
81 |
return new ClassFileReader(path); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
82 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
83 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
84 |
|
16550
f20e2521f3df
8005428: Update jdeps to read the same profile information as by javac
mchung
parents:
15030
diff
changeset
|
85 |
/** |
36526 | 86 |
* Returns a ClassFileReader instance of a given FileSystem and path. |
87 |
* |
|
88 |
* This method is used for reading classes from jrtfs. |
|
89 |
*/ |
|
90 |
public static ClassFileReader newInstance(FileSystem fs, Path path) throws IOException { |
|
91 |
return new DirectoryReader(fs, path); |
|
92 |
} |
|
93 |
||
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
94 |
protected final Path path; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
95 |
protected final String baseFileName; |
36526 | 96 |
protected Set<String> entries; // binary names |
97 |
||
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
98 |
protected final List<String> skippedEntries = new ArrayList<>(); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
99 |
protected ClassFileReader(Path path) { |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
100 |
this.path = path; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
101 |
this.baseFileName = path.getFileName() != null |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
102 |
? path.getFileName().toString() |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
103 |
: path.toString(); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
104 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
105 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
106 |
public String getFileName() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
107 |
return baseFileName; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
108 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
109 |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
110 |
public List<String> skippedEntries() { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
111 |
return skippedEntries; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
112 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
113 |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
114 |
/** |
36526 | 115 |
* Returns all entries in this archive. |
116 |
*/ |
|
117 |
public Set<String> entries() { |
|
118 |
Set<String> es = this.entries; |
|
119 |
if (es == null) { |
|
120 |
// lazily scan the entries |
|
121 |
this.entries = scan(); |
|
122 |
} |
|
123 |
return this.entries; |
|
124 |
} |
|
125 |
||
126 |
/** |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
127 |
* Returns the ClassFile matching the given binary name |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
128 |
* or a fully-qualified class name. |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
129 |
*/ |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
130 |
public ClassFile getClassFile(String name) throws IOException { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
131 |
if (name.indexOf('.') > 0) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
132 |
int i = name.lastIndexOf('.'); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
133 |
String pathname = name.replace('.', File.separatorChar) + ".class"; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
134 |
if (baseFileName.equals(pathname) || |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
135 |
baseFileName.equals(pathname.substring(0, i) + "$" + |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
136 |
pathname.substring(i+1, pathname.length()))) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
137 |
return readClassFile(path); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
138 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
139 |
} else { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
140 |
if (baseFileName.equals(name.replace('/', File.separatorChar) + ".class")) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
141 |
return readClassFile(path); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
142 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
143 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
144 |
return null; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
145 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
146 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
147 |
public Iterable<ClassFile> getClassFiles() throws IOException { |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
148 |
return FileIterator::new; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
149 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
150 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
151 |
protected ClassFile readClassFile(Path p) throws IOException { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
152 |
InputStream is = null; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
153 |
try { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
154 |
is = Files.newInputStream(p); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
155 |
return ClassFile.read(is); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
156 |
} catch (ConstantPoolException e) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
157 |
throw new ClassFileError(e); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
158 |
} finally { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
159 |
if (is != null) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
160 |
is.close(); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
161 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
162 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
163 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
164 |
|
36526 | 165 |
protected Set<String> scan() { |
166 |
try { |
|
167 |
ClassFile cf = ClassFile.read(path); |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41443
diff
changeset
|
168 |
String name = cf.access_flags.is(AccessFlags.ACC_MODULE) |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41443
diff
changeset
|
169 |
? "module-info" : cf.getName(); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41443
diff
changeset
|
170 |
return Collections.singleton(name); |
36526 | 171 |
} catch (ConstantPoolException|IOException e) { |
172 |
throw new ClassFileError(e); |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
static boolean isClass(Path file) { |
|
177 |
String fn = file.getFileName().toString(); |
|
38524 | 178 |
return fn.endsWith(".class"); |
36526 | 179 |
} |
180 |
||
38530 | 181 |
@Override |
182 |
public void close() throws IOException { |
|
183 |
} |
|
184 |
||
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
185 |
class FileIterator implements Iterator<ClassFile> { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
186 |
int count; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
187 |
FileIterator() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
188 |
this.count = 0; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
189 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
190 |
public boolean hasNext() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
191 |
return count == 0 && baseFileName.endsWith(".class"); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
192 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
193 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
194 |
public ClassFile next() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
195 |
if (!hasNext()) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
196 |
throw new NoSuchElementException(); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
197 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
198 |
try { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
199 |
ClassFile cf = readClassFile(path); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
200 |
count++; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
201 |
return cf; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
202 |
} catch (IOException e) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
203 |
throw new ClassFileError(e); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
204 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
205 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
206 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
207 |
public void remove() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
208 |
throw new UnsupportedOperationException("Not supported yet."); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
209 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
210 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
211 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
212 |
public String toString() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
213 |
return path.toString(); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
214 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
215 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
216 |
private static class DirectoryReader extends ClassFileReader { |
27579 | 217 |
protected final String fsSep; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
218 |
DirectoryReader(Path path) throws IOException { |
27579 | 219 |
this(FileSystems.getDefault(), path); |
220 |
} |
|
221 |
DirectoryReader(FileSystem fs, Path path) throws IOException { |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
222 |
super(path); |
27579 | 223 |
this.fsSep = fs.getSeparator(); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
224 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
225 |
|
36526 | 226 |
protected Set<String> scan() { |
38529
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
227 |
try (Stream<Path> stream = Files.walk(path, Integer.MAX_VALUE)) { |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
228 |
return stream.filter(ClassFileReader::isClass) |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
229 |
.map(path::relativize) |
38529
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
230 |
.map(Path::toString) |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
231 |
.map(p -> p.replace(File.separatorChar, '/')) |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
232 |
.collect(Collectors.toSet()); |
36526 | 233 |
} catch (IOException e) { |
234 |
throw new UncheckedIOException(e); |
|
235 |
} |
|
236 |
} |
|
237 |
||
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
238 |
public ClassFile getClassFile(String name) throws IOException { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
239 |
if (name.indexOf('.') > 0) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
240 |
int i = name.lastIndexOf('.'); |
27579 | 241 |
String pathname = name.replace(".", fsSep) + ".class"; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
242 |
Path p = path.resolve(pathname); |
38529
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
243 |
if (Files.notExists(p)) { |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
244 |
p = path.resolve(pathname.substring(0, i) + "$" + |
27579 | 245 |
pathname.substring(i+1, pathname.length())); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
246 |
} |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16550
diff
changeset
|
247 |
if (Files.exists(p)) { |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
248 |
return readClassFile(p); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
249 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
250 |
} else { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
251 |
Path p = path.resolve(name + ".class"); |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16550
diff
changeset
|
252 |
if (Files.exists(p)) { |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
253 |
return readClassFile(p); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
254 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
255 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
256 |
return null; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
257 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
258 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
259 |
public Iterable<ClassFile> getClassFiles() throws IOException { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
260 |
final Iterator<ClassFile> iter = new DirectoryIterator(); |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
261 |
return () -> iter; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
262 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
263 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
264 |
class DirectoryIterator implements Iterator<ClassFile> { |
38529
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
265 |
private final List<Path> entries; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
266 |
private int index = 0; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
267 |
DirectoryIterator() throws IOException { |
38529
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
268 |
List<Path> paths = null; |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
269 |
try (Stream<Path> stream = Files.walk(path, Integer.MAX_VALUE)) { |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
270 |
paths = stream.filter(ClassFileReader::isClass) |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
271 |
.collect(Collectors.toList()); |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
272 |
} |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
273 |
this.entries = paths; |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38524
diff
changeset
|
274 |
this.index = 0; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
275 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
276 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
277 |
public boolean hasNext() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
278 |
return index != entries.size(); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
279 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
280 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
281 |
public ClassFile next() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
282 |
if (!hasNext()) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
283 |
throw new NoSuchElementException(); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
284 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
285 |
Path path = entries.get(index++); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
286 |
try { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
287 |
return readClassFile(path); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
288 |
} catch (IOException e) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
289 |
throw new ClassFileError(e); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
290 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
291 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
292 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
293 |
public void remove() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
294 |
throw new UnsupportedOperationException("Not supported yet."); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
295 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
296 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
297 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
298 |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
299 |
static class JarFileReader extends ClassFileReader { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
300 |
private final JarFile jarfile; |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
301 |
private final Runtime.Version version; |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
302 |
|
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
303 |
JarFileReader(Path path, Runtime.Version version) throws IOException { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
304 |
this(path, openJarFile(path.toFile(), version), version); |
16550
f20e2521f3df
8005428: Update jdeps to read the same profile information as by javac
mchung
parents:
15030
diff
changeset
|
305 |
} |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
306 |
|
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
307 |
JarFileReader(Path path, JarFile jf, Runtime.Version version) throws IOException { |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
308 |
super(path); |
16550
f20e2521f3df
8005428: Update jdeps to read the same profile information as by javac
mchung
parents:
15030
diff
changeset
|
309 |
this.jarfile = jf; |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
310 |
this.version = version; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
311 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
312 |
|
38530 | 313 |
@Override |
314 |
public void close() throws IOException { |
|
315 |
jarfile.close(); |
|
316 |
} |
|
317 |
||
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
318 |
private static JarFile openJarFile(File f, Runtime.Version version) |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
319 |
throws IOException { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
320 |
JarFile jf; |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
321 |
if (version == null) { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
322 |
jf = new JarFile(f, false); |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
323 |
if (jf.isMultiRelease()) { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
324 |
throw new MultiReleaseException("err.multirelease.option.notfound", f.getName()); |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
325 |
} |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
326 |
} else { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
327 |
jf = new JarFile(f, false, ZipFile.OPEN_READ, version); |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
328 |
if (!jf.isMultiRelease()) { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
329 |
throw new MultiReleaseException("err.multirelease.option.exists", f.getName()); |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
330 |
} |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
331 |
} |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
332 |
return jf; |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
333 |
} |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
334 |
|
36526 | 335 |
protected Set<String> scan() { |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
336 |
try (JarFile jf = openJarFile(path.toFile(), version)) { |
47989
f1ad41a05fb7
8191942: Replace jdeps use of jdk.internal.util.jar.VersionedStream with new public API
mchung
parents:
47216
diff
changeset
|
337 |
return jf.versionedStream().map(JarEntry::getName) |
38524 | 338 |
.filter(n -> n.endsWith(".class")) |
36526 | 339 |
.collect(Collectors.toSet()); |
340 |
} catch (IOException e) { |
|
341 |
throw new UncheckedIOException(e); |
|
342 |
} |
|
343 |
} |
|
344 |
||
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
345 |
public ClassFile getClassFile(String name) throws IOException { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
346 |
if (name.indexOf('.') > 0) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
347 |
int i = name.lastIndexOf('.'); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
348 |
String entryName = name.replace('.', '/') + ".class"; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
349 |
JarEntry e = jarfile.getJarEntry(entryName); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
350 |
if (e == null) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
351 |
e = jarfile.getJarEntry(entryName.substring(0, i) + "$" |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
352 |
+ entryName.substring(i + 1, entryName.length())); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
353 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
354 |
if (e != null) { |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
355 |
return readClassFile(jarfile, e); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
356 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
357 |
} else { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
358 |
JarEntry e = jarfile.getJarEntry(name + ".class"); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
359 |
if (e != null) { |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
360 |
return readClassFile(jarfile, e); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
361 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
362 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
363 |
return null; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
364 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
365 |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
366 |
protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException { |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
367 |
try (InputStream is = jarfile.getInputStream(e)) { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
368 |
ClassFile cf = ClassFile.read(is); |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
369 |
if (jarfile.isMultiRelease()) { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
370 |
VersionHelper.add(jarfile, e, cf); |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
371 |
} |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
372 |
return cf; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
373 |
} catch (ConstantPoolException ex) { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
374 |
throw new ClassFileError(ex); |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
375 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
376 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
377 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
378 |
public Iterable<ClassFile> getClassFiles() throws IOException { |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
379 |
final Iterator<ClassFile> iter = new JarFileIterator(this, jarfile); |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
380 |
return () -> iter; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
381 |
} |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
382 |
} |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
383 |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
384 |
class JarFileIterator implements Iterator<ClassFile> { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
385 |
protected final JarFileReader reader; |
47989
f1ad41a05fb7
8191942: Replace jdeps use of jdk.internal.util.jar.VersionedStream with new public API
mchung
parents:
47216
diff
changeset
|
386 |
protected Iterator<JarEntry> entries; |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
387 |
protected JarFile jf; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
388 |
protected JarEntry nextEntry; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
389 |
protected ClassFile cf; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
390 |
JarFileIterator(JarFileReader reader) { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
391 |
this(reader, null); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
392 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
393 |
JarFileIterator(JarFileReader reader, JarFile jarfile) { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
394 |
this.reader = reader; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
395 |
setJarFile(jarfile); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
396 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
397 |
|
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
398 |
void setJarFile(JarFile jarfile) { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
399 |
if (jarfile == null) return; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
400 |
|
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
401 |
this.jf = jarfile; |
47989
f1ad41a05fb7
8191942: Replace jdeps use of jdk.internal.util.jar.VersionedStream with new public API
mchung
parents:
47216
diff
changeset
|
402 |
this.entries = jarfile.versionedStream().iterator(); |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
403 |
this.nextEntry = nextEntry(); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
404 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
405 |
|
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
406 |
public boolean hasNext() { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
407 |
if (nextEntry != null && cf != null) { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
408 |
return true; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
409 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
410 |
while (nextEntry != null) { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
411 |
try { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
412 |
cf = reader.readClassFile(jf, nextEntry); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
413 |
return true; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
414 |
} catch (ClassFileError | IOException ex) { |
41443
6398801363f2
8167014: jdeps: Missing message: warn.skipped.entry
mchung
parents:
41442
diff
changeset
|
415 |
skippedEntries.add(String.format("%s: %s (%s)", |
6398801363f2
8167014: jdeps: Missing message: warn.skipped.entry
mchung
parents:
41442
diff
changeset
|
416 |
ex.getMessage(), |
6398801363f2
8167014: jdeps: Missing message: warn.skipped.entry
mchung
parents:
41442
diff
changeset
|
417 |
nextEntry.getName(), |
6398801363f2
8167014: jdeps: Missing message: warn.skipped.entry
mchung
parents:
41442
diff
changeset
|
418 |
jf.getName())); |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
419 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
420 |
nextEntry = nextEntry(); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
421 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
422 |
return false; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
423 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
424 |
|
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
425 |
public ClassFile next() { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
426 |
if (!hasNext()) { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
427 |
throw new NoSuchElementException(); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
428 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
429 |
ClassFile classFile = cf; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
430 |
cf = null; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
431 |
nextEntry = nextEntry(); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
432 |
return classFile; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
433 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
434 |
|
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
435 |
protected JarEntry nextEntry() { |
47989
f1ad41a05fb7
8191942: Replace jdeps use of jdk.internal.util.jar.VersionedStream with new public API
mchung
parents:
47216
diff
changeset
|
436 |
while (entries.hasNext()) { |
f1ad41a05fb7
8191942: Replace jdeps use of jdk.internal.util.jar.VersionedStream with new public API
mchung
parents:
47216
diff
changeset
|
437 |
JarEntry e = entries.next(); |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
438 |
String name = e.getName(); |
38524 | 439 |
if (name.endsWith(".class")) { |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
440 |
return e; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
441 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
442 |
} |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
443 |
return null; |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
444 |
} |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
445 |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
446 |
public void remove() { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22449
diff
changeset
|
447 |
throw new UnsupportedOperationException("Not supported yet."); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
448 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
449 |
} |
36526 | 450 |
private static final String MODULE_INFO = "module-info.class"; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
451 |
} |