author | serb |
Sat, 09 Jun 2018 13:33:35 -0700 | |
changeset 50647 | a98ff7c2103d |
parent 47216 | 71c04702a3d5 |
child 52649 | e00cf18e2593 |
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:
22448
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 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
28 |
import com.sun.tools.classfile.Dependency.Location; |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
29 |
|
38530 | 30 |
import java.io.Closeable; |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
31 |
import java.io.IOException; |
27579 | 32 |
import java.io.UncheckedIOException; |
36526 | 33 |
import java.net.URI; |
34 |
import java.nio.file.Files; |
|
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16290
diff
changeset
|
35 |
import java.nio.file.Path; |
36526 | 36 |
import java.nio.file.Paths; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
37 |
import java.util.HashSet; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
38 |
import java.util.Map; |
36526 | 39 |
import java.util.Objects; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
40 |
import java.util.Set; |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
41 |
import java.util.concurrent.ConcurrentHashMap; |
38524 | 42 |
import java.util.stream.Stream; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
43 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
44 |
/** |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
45 |
* Represents the source of the class files. |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
46 |
*/ |
38530 | 47 |
public class Archive implements Closeable { |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
48 |
public static Archive getInstance(Path p, Runtime.Version version) { |
27579 | 49 |
try { |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
38530
diff
changeset
|
50 |
return new Archive(p, ClassFileReader.newInstance(p, version)); |
27579 | 51 |
} catch (IOException e) { |
52 |
throw new UncheckedIOException(e); |
|
53 |
} |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
54 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
55 |
|
36526 | 56 |
private final URI location; |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16290
diff
changeset
|
57 |
private final Path path; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
58 |
private final String filename; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
59 |
private final ClassFileReader reader; |
36526 | 60 |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
61 |
protected Map<Location, Set<Location>> deps = new ConcurrentHashMap<>(); |
16290
b0b4f52de7ea
8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
15030
diff
changeset
|
62 |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
63 |
protected Archive(String name) { |
36526 | 64 |
this(name, null, null); |
25874 | 65 |
} |
36526 | 66 |
protected Archive(String name, URI location, ClassFileReader reader) { |
67 |
this.location = location; |
|
68 |
this.path = location != null ? Paths.get(location) : null; |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
69 |
this.filename = name; |
25874 | 70 |
this.reader = reader; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
71 |
} |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
72 |
protected Archive(Path p, ClassFileReader reader) { |
36526 | 73 |
this.location = null; |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16290
diff
changeset
|
74 |
this.path = p; |
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16290
diff
changeset
|
75 |
this.filename = path.getFileName().toString(); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
76 |
this.reader = reader; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
77 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
78 |
|
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
79 |
public ClassFileReader reader() { |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
80 |
return reader; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
81 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
82 |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
83 |
public String getName() { |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
84 |
return filename; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
85 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
86 |
|
36526 | 87 |
public Module getModule() { |
88 |
return Module.UNNAMED_MODULE; |
|
89 |
} |
|
90 |
||
38524 | 91 |
public boolean contains(String entry) { |
92 |
return reader.entries().contains(entry); |
|
93 |
} |
|
94 |
||
16290
b0b4f52de7ea
8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
15030
diff
changeset
|
95 |
public void addClass(Location origin) { |
28706
a724585645ce
8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents:
27579
diff
changeset
|
96 |
deps.computeIfAbsent(origin, _k -> new HashSet<>()); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
97 |
} |
21503
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
98 |
|
16290
b0b4f52de7ea
8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
15030
diff
changeset
|
99 |
public void addClass(Location origin, Location target) { |
28706
a724585645ce
8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents:
27579
diff
changeset
|
100 |
deps.computeIfAbsent(origin, _k -> new HashSet<>()).add(target); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
101 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
102 |
|
21503
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
103 |
public Set<Location> getClasses() { |
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
104 |
return deps.keySet(); |
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
105 |
} |
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
106 |
|
38524 | 107 |
public Stream<Location> getDependencies() { |
108 |
return deps.values().stream() |
|
109 |
.flatMap(Set::stream); |
|
110 |
} |
|
111 |
||
112 |
public boolean hasDependences() { |
|
113 |
return getDependencies().count() > 0; |
|
114 |
} |
|
115 |
||
21503
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
116 |
public void visitDependences(Visitor v) { |
16290
b0b4f52de7ea
8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
15030
diff
changeset
|
117 |
for (Map.Entry<Location,Set<Location>> e: deps.entrySet()) { |
b0b4f52de7ea
8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
15030
diff
changeset
|
118 |
for (Location target : e.getValue()) { |
b0b4f52de7ea
8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
15030
diff
changeset
|
119 |
v.visit(e.getKey(), target); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
120 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
121 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
122 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
123 |
|
36526 | 124 |
/** |
125 |
* Tests if any class has been parsed. |
|
126 |
*/ |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
127 |
public boolean isEmpty() { |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
128 |
return getClasses().isEmpty(); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
129 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
130 |
|
21503
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
131 |
public String getPathName() { |
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
132 |
return path != null ? path.toString() : filename; |
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
133 |
} |
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
134 |
|
36526 | 135 |
@Override |
136 |
public int hashCode() { |
|
137 |
return Objects.hash(this.filename, this.path); |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
141 |
public boolean equals(Object o) { |
|
142 |
if (o instanceof Archive) { |
|
143 |
Archive other = (Archive)o; |
|
144 |
if (path == other.path || isSameLocation(this, other)) |
|
145 |
return true; |
|
146 |
} |
|
147 |
return false; |
|
148 |
} |
|
149 |
||
150 |
@Override |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
151 |
public String toString() { |
21503
45fc62482cae
8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents:
21046
diff
changeset
|
152 |
return filename; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
153 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
154 |
|
28706
a724585645ce
8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents:
27579
diff
changeset
|
155 |
public Path path() { |
a724585645ce
8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents:
27579
diff
changeset
|
156 |
return path; |
a724585645ce
8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents:
27579
diff
changeset
|
157 |
} |
a724585645ce
8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents:
27579
diff
changeset
|
158 |
|
36526 | 159 |
public static boolean isSameLocation(Archive archive, Archive other) { |
160 |
if (archive.path == null || other.path == null) |
|
161 |
return false; |
|
162 |
||
163 |
if (archive.location != null && other.location != null && |
|
164 |
archive.location.equals(other.location)) { |
|
165 |
return true; |
|
166 |
} |
|
167 |
||
168 |
if (archive.isJrt() || other.isJrt()) { |
|
169 |
return false; |
|
170 |
} |
|
171 |
||
172 |
try { |
|
173 |
return Files.isSameFile(archive.path, other.path); |
|
174 |
} catch (IOException e) { |
|
175 |
throw new UncheckedIOException(e); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
private boolean isJrt() { |
|
180 |
return location != null && location.getScheme().equals("jrt"); |
|
181 |
} |
|
38530 | 182 |
|
183 |
@Override |
|
184 |
public void close() throws IOException { |
|
185 |
if (reader != null) |
|
186 |
reader.close(); |
|
187 |
} |
|
188 |
||
16290
b0b4f52de7ea
8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
15030
diff
changeset
|
189 |
interface Visitor { |
b0b4f52de7ea
8006225: tools/jdeps/Basic.java failes with AssertionError
mchung
parents:
15030
diff
changeset
|
190 |
void visit(Location origin, Location target); |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
191 |
} |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
192 |
} |