|
37 | 37 | import org.apache.bcel.classfile.ClassParser;
|
38 | 38 |
|
39 | 39 | /**
|
40 |
| - * Constructs a callgraph out of a JAR archive. Can combine multiple archives |
41 |
| - * into a single call graph. |
| 40 | + * Constructs a callgraph out of a set of directories, classes and JAR archives. |
| 41 | + * Can combine multiple archives into a single call graph. |
42 | 42 | *
|
43 | 43 | * @author Georgios Gousios <gousiosg@gmail.com>
|
44 | 44 | *
|
45 | 45 | */
|
46 | 46 | public class JCallGraph {
|
47 | 47 |
|
48 |
| - public static void main(String[] args) { |
49 |
| - ClassParser cp; |
50 |
| - try { |
51 |
| - for (String arg : args) { |
| 48 | + public static void processClass(String class_name) throws IOException { |
| 49 | + ClassParser cp = new ClassParser(class_name); |
| 50 | + ClassVisitor visitor = new ClassVisitor(cp.parse()); |
| 51 | + visitor.start(); |
| 52 | + } |
| 53 | + |
| 54 | + public static void processClass(String jar_name, String class_name) throws IOException { |
| 55 | + ClassParser cp = new ClassParser(jar_name,class_name); |
| 56 | + ClassVisitor visitor = new ClassVisitor(cp.parse()); |
| 57 | + visitor.start(); |
| 58 | + } |
| 59 | + |
| 60 | + public static void processJar(JarFile jar) throws IOException { |
| 61 | + Enumeration<JarEntry> entries = jar.entries(); |
| 62 | + while (entries.hasMoreElements()) { |
| 63 | + JarEntry entry = entries.nextElement(); |
| 64 | + if (entry.isDirectory()) |
| 65 | + continue; |
52 | 66 |
|
53 |
| - File f = new File(arg); |
54 |
| - |
55 |
| - if (!f.exists()) { |
56 |
| - System.err.println("Jar file " + arg + " does not exist"); |
57 |
| - } |
58 |
| - |
59 |
| - JarFile jar = new JarFile(f); |
| 67 | + if (!entry.getName().endsWith(".class")) |
| 68 | + continue; |
60 | 69 |
|
61 |
| - Enumeration<JarEntry> entries = jar.entries(); |
62 |
| - while (entries.hasMoreElements()) { |
63 |
| - JarEntry entry = entries.nextElement(); |
64 |
| - if (entry.isDirectory()) |
65 |
| - continue; |
| 70 | + processClass(jar.getName(),entry.getName()); |
| 71 | + } |
| 72 | + } |
66 | 73 |
|
67 |
| - if (!entry.getName().endsWith(".class")) |
68 |
| - continue; |
| 74 | + public static void processFile(File file) { |
| 75 | + try { |
| 76 | + if (!file.exists()) |
| 77 | + System.err.println("File " + file.getName() + " does not exist"); |
69 | 78 |
|
70 |
| - cp = new ClassParser(arg,entry.getName()); |
71 |
| - ClassVisitor visitor = new ClassVisitor(cp.parse()); |
72 |
| - visitor.start(); |
73 |
| - } |
| 79 | + else if (file.isDirectory()) { |
| 80 | + for (File dfile : file.listFiles()) |
| 81 | + processFile(dfile); |
74 | 82 | }
|
| 83 | + |
| 84 | + else if (file.getName().endsWith(".jar")) |
| 85 | + processJar(new JarFile(file)); |
| 86 | + |
| 87 | + else if (file.getName().endsWith(".class")) |
| 88 | + processClass(file.getAbsolutePath()); |
| 89 | + |
75 | 90 | } catch (IOException e) {
|
76 |
| - System.err.println("Error while processing jar: " + e.getMessage()); |
| 91 | + System.err.println("Error while processing file: " + e.getMessage()); |
77 | 92 | e.printStackTrace();
|
78 | 93 | }
|
79 | 94 | }
|
| 95 | + |
| 96 | + public static void main(String[] args) { |
| 97 | + for (String arg : args) { |
| 98 | + processFile(new File(arg)); |
| 99 | + } |
| 100 | + } |
80 | 101 | }
|
0 commit comments