4524
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
|
4524
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
4524
|
22 |
*
|
|
23 |
*/
|
|
24 |
package com.sun.classanalyzer;
|
|
25 |
|
|
26 |
import java.io.BufferedInputStream;
|
|
27 |
import java.io.File;
|
|
28 |
import java.io.FileInputStream;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.InputStream;
|
|
31 |
import java.util.ArrayList;
|
|
32 |
import java.util.Enumeration;
|
|
33 |
import java.util.HashSet;
|
|
34 |
import java.util.List;
|
|
35 |
import java.util.Set;
|
|
36 |
import java.util.jar.JarEntry;
|
|
37 |
import java.util.jar.JarFile;
|
|
38 |
|
|
39 |
/**
|
|
40 |
*
|
|
41 |
* @author mchung
|
|
42 |
*/
|
|
43 |
public class ClassPath {
|
|
44 |
|
|
45 |
public class FileInfo {
|
|
46 |
|
|
47 |
File file;
|
|
48 |
JarFile jarfile;
|
|
49 |
int classCount;
|
|
50 |
long filesize;
|
|
51 |
|
|
52 |
FileInfo(File f) throws IOException {
|
|
53 |
this.file = f;
|
|
54 |
this.classCount = 0;
|
|
55 |
if (file.getName().endsWith(".jar")) {
|
|
56 |
this.filesize = file.length();
|
|
57 |
jarfile = new JarFile(f);
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
File getFile() {
|
|
62 |
return file;
|
|
63 |
}
|
|
64 |
|
|
65 |
JarFile getJarFile() {
|
|
66 |
return jarfile;
|
|
67 |
}
|
|
68 |
|
|
69 |
String getName() throws IOException {
|
|
70 |
return file.getCanonicalPath();
|
|
71 |
}
|
|
72 |
}
|
|
73 |
private List<FileInfo> fileList = new ArrayList<FileInfo>();
|
|
74 |
private static ClassPath instance = new ClassPath();
|
|
75 |
|
|
76 |
static List<FileInfo> getFileInfos() {
|
|
77 |
return instance.fileList;
|
|
78 |
}
|
|
79 |
|
|
80 |
static ClassPath setJDKHome(String jdkhome) throws IOException {
|
|
81 |
List<File> files = new ArrayList<File>();
|
|
82 |
File jre = new File(jdkhome, "jre");
|
|
83 |
File lib = new File(jdkhome, "lib");
|
|
84 |
if (jre.exists() && jre.isDirectory()) {
|
|
85 |
listFiles(new File(jre, "lib"), ".jar", files);
|
|
86 |
} else if (lib.exists() && lib.isDirectory()) {
|
|
87 |
// either a JRE or a jdk build image
|
|
88 |
listFiles(lib, ".jar", files);
|
|
89 |
|
|
90 |
File classes = new File(jdkhome, "classes");
|
|
91 |
if (classes.exists() && classes.isDirectory()) {
|
|
92 |
// jdk build outputdir
|
|
93 |
instance.add(classes);
|
|
94 |
}
|
|
95 |
} else {
|
|
96 |
throw new RuntimeException("\"" + jdkhome + "\" not a JDK home");
|
|
97 |
}
|
|
98 |
|
|
99 |
for (File f : files) {
|
|
100 |
instance.add(f);
|
|
101 |
}
|
|
102 |
return instance;
|
|
103 |
}
|
|
104 |
|
|
105 |
static ClassPath setClassPath(String path) throws IOException {
|
|
106 |
if (path.endsWith(".class")) {
|
|
107 |
// one class file
|
|
108 |
File f = new File(path);
|
|
109 |
if (!f.exists()) {
|
|
110 |
throw new RuntimeException("Classfile \"" + f + "\" doesn't exist");
|
|
111 |
}
|
|
112 |
|
|
113 |
instance.add(f);
|
|
114 |
} else {
|
|
115 |
List<File> jarFiles = new ArrayList<File>();
|
|
116 |
String[] locs = path.split(File.pathSeparator);
|
|
117 |
for (String p : locs) {
|
|
118 |
File f = new File(p);
|
|
119 |
if (!f.exists()) {
|
|
120 |
throw new RuntimeException("\"" + f + "\" doesn't exist");
|
|
121 |
}
|
|
122 |
|
|
123 |
if (f.isDirectory()) {
|
|
124 |
instance.add(f); // add the directory to look up .class files
|
|
125 |
listFiles(f, ".jar", jarFiles);
|
|
126 |
} else if (p.endsWith(".jar")) {
|
|
127 |
// jar files
|
|
128 |
jarFiles.add(f);
|
|
129 |
} else {
|
|
130 |
throw new RuntimeException("Invalid file \"" + f);
|
|
131 |
}
|
|
132 |
}
|
|
133 |
// add jarFiles if any
|
|
134 |
for (File f : jarFiles) {
|
|
135 |
instance.add(f);
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
return instance;
|
|
140 |
}
|
|
141 |
|
|
142 |
private void add(File f) throws IOException {
|
|
143 |
fileList.add(new FileInfo(f));
|
|
144 |
}
|
|
145 |
|
|
146 |
public static InputStream open(String pathname) throws IOException {
|
|
147 |
for (FileInfo fi : instance.fileList) {
|
|
148 |
if (fi.getName().endsWith(".jar")) {
|
|
149 |
String path = pathname.replace(File.separatorChar, '/');
|
|
150 |
JarEntry e = fi.jarfile.getJarEntry(path);
|
|
151 |
if (e != null) {
|
|
152 |
return fi.jarfile.getInputStream(e);
|
|
153 |
}
|
|
154 |
} else if (fi.getFile().isDirectory()) {
|
|
155 |
File f = new File(fi.getFile(), pathname);
|
|
156 |
if (f.exists()) {
|
|
157 |
return new FileInputStream(f);
|
|
158 |
}
|
|
159 |
} else if (fi.file.isFile()) {
|
|
160 |
if (fi.getName().endsWith(File.separator + pathname)) {
|
|
161 |
return new FileInputStream(fi.file);
|
|
162 |
}
|
|
163 |
}
|
|
164 |
}
|
|
165 |
return null;
|
|
166 |
}
|
|
167 |
|
|
168 |
static ClassFileParser parserForClass(String classname) throws IOException {
|
|
169 |
String pathname = classname.replace('.', File.separatorChar) + ".class";
|
|
170 |
|
|
171 |
ClassFileParser cfparser = null;
|
|
172 |
for (FileInfo fi : instance.fileList) {
|
|
173 |
if (fi.getName().endsWith(".class")) {
|
|
174 |
if (fi.getName().endsWith(File.separator + pathname)) {
|
|
175 |
cfparser = ClassFileParser.newParser(fi.getFile(), true);
|
|
176 |
break;
|
|
177 |
}
|
|
178 |
} else if (fi.getName().endsWith(".jar")) {
|
|
179 |
JarEntry e = fi.jarfile.getJarEntry(classname.replace('.', '/') + ".class");
|
|
180 |
if (e != null) {
|
|
181 |
cfparser = ClassFileParser.newParser(fi.jarfile.getInputStream(e), e.getSize(), true);
|
|
182 |
break;
|
|
183 |
}
|
|
184 |
} else if (fi.getFile().isDirectory()) {
|
|
185 |
File f = new File(fi.getFile(), pathname);
|
|
186 |
if (f.exists()) {
|
|
187 |
cfparser = ClassFileParser.newParser(f, true);
|
|
188 |
break;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
return cfparser;
|
|
193 |
}
|
|
194 |
|
|
195 |
public static void parseAllClassFiles() throws IOException {
|
|
196 |
instance.parseFiles();
|
|
197 |
}
|
|
198 |
|
|
199 |
private void parseFiles() throws IOException {
|
|
200 |
Set<Klass> classes = new HashSet<Klass>();
|
|
201 |
|
|
202 |
int count = 0;
|
|
203 |
for (FileInfo fi : fileList) {
|
|
204 |
// filter out public generated classes (i.e. not public API)
|
|
205 |
// javax.management.remote.rmi._RMIConnectionImpl_Tie
|
|
206 |
// javax.management.remote.rmi._RMIServerImpl_Tie
|
|
207 |
if (fi.getName().endsWith(".class")) {
|
|
208 |
parseClass(fi);
|
|
209 |
} else if (fi.getName().endsWith(".jar")) {
|
|
210 |
Enumeration<JarEntry> entries = fi.jarfile.entries();
|
|
211 |
while (entries.hasMoreElements()) {
|
|
212 |
JarEntry e = entries.nextElement();
|
|
213 |
if (e.getName().endsWith(".class")) {
|
|
214 |
ClassFileParser cfparser = ClassFileParser.newParser(fi.jarfile.getInputStream(e), e.getSize(), true);
|
|
215 |
cfparser.parseDependency(false);
|
|
216 |
fi.classCount++;
|
|
217 |
} else if (!e.isDirectory() && ResourceFile.isResource(e.getName())) {
|
|
218 |
ResourceFile.addResource(e.getName(), fi.jarfile.getInputStream(e));
|
|
219 |
}
|
|
220 |
}
|
|
221 |
} else if (fi.getFile().isDirectory()) {
|
|
222 |
List<File> files = new ArrayList<File>();
|
|
223 |
listFiles(fi.getFile(), "", files);
|
|
224 |
for (File f : files) {
|
|
225 |
if (f.getName().endsWith(".class")) {
|
|
226 |
parseClass(fi, f);
|
|
227 |
} else if (!f.isDirectory() && ResourceFile.isResource(f.getCanonicalPath())) {
|
|
228 |
String pathname = f.getCanonicalPath();
|
|
229 |
String dir = fi.getName();
|
|
230 |
if (!pathname.startsWith(dir)) {
|
|
231 |
throw new RuntimeException("Incorrect pathname " + pathname);
|
|
232 |
}
|
|
233 |
String name = pathname.substring(dir.length() + 1, pathname.length());
|
|
234 |
BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
|
|
235 |
try {
|
|
236 |
ResourceFile.addResource(name, in);
|
|
237 |
} finally {
|
|
238 |
in.close();
|
|
239 |
}
|
|
240 |
}
|
|
241 |
}
|
|
242 |
} else {
|
|
243 |
// should not reach here
|
|
244 |
throw new RuntimeException("Unexpected class path: " + fi.getFile());
|
|
245 |
}
|
|
246 |
}
|
|
247 |
}
|
|
248 |
|
|
249 |
private void parseClass(FileInfo fi) throws IOException {
|
|
250 |
parseClass(fi, fi.getFile());
|
|
251 |
}
|
|
252 |
|
|
253 |
private void parseClass(FileInfo fi, File f) throws IOException {
|
|
254 |
ClassFileParser cfparser = ClassFileParser.newParser(f, true);
|
|
255 |
cfparser.parseDependency(false);
|
|
256 |
fi.classCount++;
|
|
257 |
// need to update the filesize for this directory
|
|
258 |
fi.filesize += fi.getFile().length();
|
|
259 |
|
|
260 |
}
|
|
261 |
|
|
262 |
public static void listFiles(File path, String suffix, List<File> result) {
|
|
263 |
if (path.isDirectory()) {
|
|
264 |
File[] children = path.listFiles();
|
|
265 |
for (File c : children) {
|
|
266 |
listFiles(c, suffix, result);
|
|
267 |
}
|
|
268 |
|
|
269 |
} else {
|
|
270 |
if (suffix.isEmpty() || path.getName().endsWith(suffix)) {
|
|
271 |
result.add(path);
|
|
272 |
}
|
|
273 |
}
|
|
274 |
}
|
|
275 |
}
|