1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Main.java The main application class.
*
* Created on May 1, 2007, 6:32 PM
*
* Copyright (C) 2007,2008 Petteri Räty <betelgeuse@gentoo.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package javadepchecker;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.EmptyVisitor;
/**
*
* @author betelgeuse
* @author serkan
*/
public final class Main extends EmptyVisitor {
static private String image = "";
private Set<String> deps = new HashSet<String>();
private Set<String> current = new HashSet<String>();
/** Creates a new instance of Main */
public Main() {
}
private static Collection<String> getPackageJars(String pkg) {
ArrayList<String> jars = new ArrayList<String>();
try {
Process p = Runtime.getRuntime().exec("java-config -p " + pkg);
p.waitFor();
BufferedReader in;
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String output = in.readLine();
if (output!=null/* package somehow missing*/ && !output.trim().equals("")) {
for (String jar : output.split(":")) {
jars.add(jar);
}
}
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
return jars;
}
public void processJar(JarFile jar) throws IOException {
for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
JarEntry entry = e.nextElement();
String name = entry.getName();
if (!entry.isDirectory() && name.endsWith(".class")) {
this.current.add(name);
InputStream stream = jar.getInputStream(entry);
new ClassReader(stream).accept(this, 0);
}
}
}
private static boolean depNeeded(String pkg, Collection<String> deps) throws IOException {
Collection<String> jars = getPackageJars(pkg);
// We have a virtual with VM provider here
if (jars.size() == 0) {
return true;
}
for (String jarName : jars) {
JarFile jar = new JarFile(jarName);
for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
String name = e.nextElement().getName();
if (deps.contains(name)) {
return true;
}
}
}
return false;
}
private static boolean depsFound(Collection<String> pkgs, Collection<String> deps) throws IOException {
boolean found = true;
Collection<String> jars = new ArrayList<String>();
String[] bootClassPathJars = System.getProperty("sun.boot.class.path").split(":");
// Do we need "java-config -r" here?
for (String jar : bootClassPathJars) {
File jarFile = new File(jar);
if (jarFile.exists()) {
jars.add(jar);
}
}
for (Iterator<String> pkg = pkgs.iterator(); pkg.hasNext();) {
jars.addAll(getPackageJars(pkg.next()));
}
if (jars.size() == 0) {
return false;
}
ArrayList<String> jarClasses = new ArrayList<String>();
for (String jarName : jars) {
JarFile jar = new JarFile(jarName);
for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
jarClasses.add(e.nextElement().getName());
}
}
for (String dep : deps) {
if (!jarClasses.contains(dep)) {
if (found) {
System.out.println("Class files not found via DEPEND in package.env");
}
System.out.println("\t" + dep);
found = false;
}
}
return found;
}
private static boolean checkPkg(File env) {
boolean needed = true;
boolean found = true;
HashSet<String> pkgs = new HashSet<String>();
Collection<String> deps = null;
BufferedReader in = null;
try {
Pattern dep_re = Pattern.compile("^DEPEND=\"([^\"]*)\"$");
Pattern cp_re = Pattern.compile("^CLASSPATH=\"([^\"]*)\"$");
String line;
in = new BufferedReader(new FileReader(env));
while ((line = in.readLine()) != null) {
Matcher m = dep_re.matcher(line);
if (m.matches()) {
String atoms = m.group(1);
for (String atom : atoms.split(":")) {
String pkg = atom;
if (atom.contains("@")) {
pkg = atom.split("@")[1];
}
pkgs.add(pkg);
}
continue;
}
m = cp_re.matcher(line);
if (m.matches()) {
Main classParser = new Main();
for (String jar : m.group(1).split(":")) {
if (jar.endsWith(".jar")) {
classParser.processJar(new JarFile(image + jar));
}
}
deps = classParser.getDeps();
}
}
for (String pkg : pkgs) {
if (!depNeeded(pkg, deps)) {
if (needed) {
System.out.println("Possibly unneeded dependencies found");
}
System.out.println("\t" + pkg);
needed = false;
}
}
found = depsFound(pkgs, deps);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
in.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
return needed && found;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
int exit = 0;
try {
CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption("h", "help", false, "print help");
options.addOption("i", "image", true, "image directory");
options.addOption("v", "verbose", false, "print verbose output");
CommandLine line = parser.parse(options, args);
String[] files = line.getArgs();
if (line.hasOption("h") || files.length == 0) {
HelpFormatter h = new HelpFormatter();
h.printHelp("java-dep-check [-i <image>] <package.env>+", options);
} else {
image = line.getOptionValue("i", "");
for (String arg : files) {
if (line.hasOption('v')) {
System.out.println("Checking " + arg);
}
if (!checkPkg(new File(arg))) {
exit = 1;
}
}
}
} catch (ParseException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(exit);
}
private void addDep(String dep) {
deps.add(dep + ".class");
}
private void addDep(Type dep) {
if (dep.getSort() == Type.ARRAY) {
addDep(dep.getElementType());
}
if (dep.getSort() == Type.OBJECT) {
addDep(dep.getInternalName());
}
}
private Collection<String> getDeps() {
ArrayList<String> result = new ArrayList<String>();
for (String s : deps) {
if (!current.contains(s)) {
result.add(s);
}
}
return result;
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
if(superName != null) {
addDep(superName);
}
for (String iface : interfaces) {
addDep(iface);
}
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
addDep(Type.getType(desc));
return null;
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
for (Type param : Type.getArgumentTypes(desc)) {
addDep(param);
}
if (exceptions != null) {
for (String exception : exceptions) {
addDep(exception);
}
}
addDep(Type.getReturnType(desc));
return new EmptyVisitor() {
@Override
public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
addDep(Type.getType(desc));
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
addDep(Type.getObjectType(owner));
addDep(Type.getType(desc));
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
addDep(Type.getObjectType(owner));
}
@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
return Main.this.visitAnnotation(desc, visible);
}
};
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
addDep(Type.getType(desc));
return null;
}
}
|