author | vasya |
Mon, 14 Dec 2015 20:18:19 +0100 | |
changeset 34752 | 9c262a013456 |
parent 31753 | 72417309a675 |
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:
21046
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:
31753
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 |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
28 |
import com.sun.tools.classfile.ClassFile; |
27579 | 29 |
import com.sun.tools.jdeps.ClassFileReader.ModuleClassReader; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
30 |
import java.io.IOException; |
25874 | 31 |
import java.io.InputStream; |
26276
0dca2378aa34
8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents:
25874
diff
changeset
|
32 |
import java.io.BufferedInputStream; |
27579 | 33 |
import java.net.URI; |
34 |
import java.nio.file.*; |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
35 |
import java.nio.file.attribute.BasicFileAttributes; |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
36 |
import java.util.*; |
27579 | 37 |
import java.util.stream.Collectors; |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
38 |
|
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
39 |
/** |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
40 |
* ClassPath for Java SE and JDK |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
41 |
*/ |
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
42 |
class PlatformClassPath { |
25874 | 43 |
private static List<Archive> modules; |
27579 | 44 |
static synchronized List<Archive> getModules(Path mpath) throws IOException { |
25874 | 45 |
if (modules == null) { |
27579 | 46 |
initPlatformModules(mpath); |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16560
diff
changeset
|
47 |
} |
25874 | 48 |
return modules; |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
49 |
} |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16560
diff
changeset
|
50 |
|
27579 | 51 |
private static void initPlatformModules(Path mpath) throws IOException { |
52 |
ImageHelper helper = ImageHelper.getInstance(mpath); |
|
53 |
String fn = System.getProperty("jdeps.modules.xml"); |
|
54 |
if (fn != null) { |
|
55 |
Path p = Paths.get(fn); |
|
56 |
try (InputStream in = new BufferedInputStream(Files.newInputStream(p))) { |
|
57 |
modules = new ArrayList<>(ModulesXmlReader.load(helper, in)); |
|
58 |
} |
|
59 |
} else { |
|
60 |
try (InputStream in = PlatformClassPath.class |
|
61 |
.getResourceAsStream("resources/jdeps-modules.xml")) { |
|
62 |
modules = new ArrayList<>(ModulesXmlReader.load(helper, in)); |
|
63 |
} |
|
64 |
} |
|
65 |
if (findModule("java.base") != null) { |
|
66 |
Profile.initProfiles(modules); |
|
67 |
} |
|
68 |
} |
|
69 |
||
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16560
diff
changeset
|
70 |
/** |
25874 | 71 |
* Finds the module with the given name. Returns null |
72 |
* if such module doesn't exist. |
|
73 |
* |
|
74 |
* @param mn module name |
|
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16560
diff
changeset
|
75 |
*/ |
25874 | 76 |
static Module findModule(String mn) { |
77 |
for (Archive a : modules) { |
|
78 |
if (Module.class.isInstance(a)) { |
|
79 |
Module m = (Module)a; |
|
80 |
if (mn.equals(m.name())) { |
|
81 |
return m; |
|
82 |
} |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
83 |
} |
25874 | 84 |
} |
85 |
return null; |
|
86 |
} |
|
87 |
||
27579 | 88 |
/** |
89 |
* Returns JAR files in $java.home/lib. This is for transition until |
|
90 |
* all components are linked into jimage. |
|
91 |
*/ |
|
92 |
static List<Archive> getJarFiles() throws IOException { |
|
93 |
Path home = Paths.get(System.getProperty("java.home"), "lib"); |
|
94 |
return Files.find(home, 1, (Path p, BasicFileAttributes attr) |
|
95 |
-> p.getFileName().toString().endsWith(".jar")) |
|
96 |
.map(Archive::getInstance) |
|
97 |
.collect(Collectors.toList()); |
|
25874 | 98 |
} |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
99 |
|
27579 | 100 |
static class ImageHelper { |
31753 | 101 |
private static boolean isJrtAvailable() { |
102 |
try { |
|
103 |
FileSystems.getFileSystem(URI.create("jrt:/")); |
|
104 |
return true; |
|
105 |
} catch (ProviderNotFoundException | FileSystemNotFoundException e) { |
|
106 |
return false; |
|
107 |
} |
|
108 |
} |
|
109 |
||
27579 | 110 |
static ImageHelper getInstance(Path mpath) throws IOException { |
111 |
if (mpath != null) { |
|
112 |
return new ImageHelper(mpath); |
|
113 |
} |
|
31753 | 114 |
|
115 |
if (isJrtAvailable()) { |
|
116 |
// jrt file system |
|
27579 | 117 |
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/")); |
31753 | 118 |
return new ImageHelper(fs, fs.getPath("/modules")); |
27579 | 119 |
} else { |
120 |
// exploded modules |
|
31753 | 121 |
String home = System.getProperty("java.home"); |
122 |
Path exploded = Paths.get(home, "modules"); |
|
123 |
if (!Files.isDirectory(exploded)) { |
|
124 |
throw new InternalError(home + " not a modular image"); |
|
25874 | 125 |
} |
31753 | 126 |
return new ImageHelper(exploded); |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
127 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
128 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
129 |
|
27579 | 130 |
private final FileSystem fs; |
131 |
private final Path mpath; |
|
132 |
||
133 |
ImageHelper(Path path) throws IOException { |
|
134 |
this(FileSystems.getDefault(), path); |
|
25874 | 135 |
} |
136 |
||
27579 | 137 |
ImageHelper(FileSystem fs, Path path) throws IOException { |
138 |
this.fs = fs; |
|
139 |
this.mpath = path; |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
140 |
} |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
141 |
|
25874 | 142 |
/** |
27579 | 143 |
* Returns a ModuleClassReader that only reads classes for the given modulename. |
25874 | 144 |
*/ |
27579 | 145 |
public ModuleClassReader getModuleClassReader(String modulename) |
146 |
throws IOException |
|
147 |
{ |
|
148 |
Path mp = mpath.resolve(modulename); |
|
149 |
if (Files.exists(mp) && Files.isDirectory(mp)) { |
|
150 |
return new ModuleClassReader(fs, modulename, mp); |
|
151 |
} else { |
|
152 |
// aggregator module or os-specific module in jdeps-modules.xml |
|
153 |
// mdir not exist |
|
154 |
return new NonExistModuleReader(fs, modulename, mp); |
|
25874 | 155 |
} |
27579 | 156 |
} |
25874 | 157 |
|
27579 | 158 |
static class NonExistModuleReader extends ModuleClassReader { |
159 |
private final List<ClassFile> classes = Collections.emptyList(); |
|
25874 | 160 |
|
27579 | 161 |
private NonExistModuleReader(FileSystem fs, String mn, Path mpath) |
162 |
throws IOException |
|
163 |
{ |
|
164 |
super(fs, mn, mpath); |
|
25874 | 165 |
} |
166 |
||
167 |
public ClassFile getClassFile(String name) throws IOException { |
|
168 |
return null; |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
169 |
} |
25874 | 170 |
|
171 |
public Iterable<ClassFile> getClassFiles() throws IOException { |
|
27579 | 172 |
return classes; |
25874 | 173 |
} |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
174 |
|
27579 | 175 |
public Set<String> packages() { |
176 |
return Collections.emptySet(); |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
21046
diff
changeset
|
177 |
} |
21046
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16560
diff
changeset
|
178 |
} |
ebf16a1a6328
8015912: jdeps support to output in dot file format
mchung
parents:
16560
diff
changeset
|
179 |
} |
15030
2d8dec41f029
8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff
changeset
|
180 |
} |