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