1 | /** | | 1 | /** |
2 | * Determine the dependencies of the configured root classes. | | 2 | * Determine the dependencies of the configured root classes. |
3 | * | | 3 | * |
4 | * @param files a vector to be populated with the files which contain | | 4 | * @param files a vector to be populated with the files which contain |
5 | * the dependency classes | | 5 | * the dependency classes |
6 | * @param classes a vector to be populated with the names of the | | 6 | * @param classes a vector to be populated with the names of the |
7 | * dependency classes. | | 7 | * depencency classes. |
8 | */ | | 8 | */ |
9 | protected void determineDependencies(Vector files, Vector classes) { | | 9 | protected void determineDependencies(Vector files, Vector classes) { |
10 | // we get the root classes and build up a set of | | 10 | // we get the root classes and build up a set of |
11 | // classes upon which they depend | | 11 | // classes upon which they depend |
12 | Hashtable dependencies = new Hashtable(); | | 12 | Hashtable dependencies = new Hashtable(); |
13 | Hashtable containers = new Hashtable(); | | 13 | Hashtable containers = new Hashtable(); |
14 | Hashtable toAnalyze = new Hashtable(); | | 14 | Hashtable toAnalyze = new Hashtable(); |
15 | for (Enumeration e = getRootClasses(); e.hasMoreElements();) { | | 15 | Hashtable nextAnalyze = new Hashtable(); |
16 | String classname = (String) e.nextElement(); | | 16 | for (Enumeration e = getRootClasses(); e.hasMoreElements();) { |
17 | toAnalyze.put(classname, classname); | | 17 | String classname = (String) e.nextElement(); |
18 | } | | 18 | toAnalyze.put(classname, classname); |
19 | | | 19 | } |
20 | int count = 0; | | 20 | |
21 | int maxCount = isClosureRequired() ? MAX_LOOPS : 1; | | 21 | int count = 0; |
22 | Hashtable analyzedDeps = null; | | 22 | int maxCount = isClosureRequired() ? MAX_LOOPS : 2; |
23 | while (toAnalyze.size() != 0 && count++ < maxCount) { | | 23 | while (toAnalyze.size() != 0 && count++ < maxCount) { |
24 | analyzedDeps = new Hashtable(); | | 24 | nextAnalyze.clear(); |
25 | for (Enumeration e = toAnalyze.keys(); e.hasMoreElements();) { | | 25 | for (Enumeration e = toAnalyze.keys(); e.hasMoreElements();) { |
26 | String classname = (String) e.nextElement(); | | 26 | String classname = (String) e.nextElement(); |
27 | dependencies.put(classname, classname); | | 27 | dependencies.put(classname, classname); |
28 | try { | | 28 | try { |
29 | File container = getClassContainer(classname); | | 29 | File container = getClassContainer(classname); |
30 | if (container == null) { | | 30 | if (container == null) { |
31 | continue; | | 31 | continue; |
32 | } | | 32 | } |
33 | containers.put(container, container); | | 33 | containers.put(container, container); |
34 | | | 34 | |
35 | ZipFile zipFile = null; | | 35 | ClassParser parser = null; |
36 | InputStream inStream = null; | | 36 | if (container.getName().endsWith(".class")) { |
37 | try { | | 37 | parser = new ClassParser(container.getPath()); |
38 | if (container.getName().endsWith(".class")) { | | 38 | } else { |
39 | inStream = new FileInputStream(container.getPath()); | | 39 | parser = new ClassParser(container.getPath(), |
40 | } else { | | 40 | classname.replace('.', '/') + ".class"); |
41 | zipFile = new ZipFile(container.getPath()); | | 41 | } |
42 | String entryName | | 42 | |
43 | = classname.replace('.', '/') + ".class"; | | 43 | JavaClass javaClass = parser.parse(); |
44 | ZipEntry entry = new ZipEntry(entryName); | | 44 | String[] interfaces = javaClass.getInterfaceNames(); |
45 | inStream | | 45 | for (int i = 0; i < interfaces.length; ++i) { |
46 | = zipFile.getInputStream(entry); | | 46 | String interfaceName = interfaces[i]; |
47 | } | | 47 | if (!dependencies.containsKey(interfaceName)) { |
48 | ClassFile classFile = new ClassFile(); | | 48 | nextAnalyze.put(interfaceName, interfaceName); |
49 | classFile.read(inStream); | | 49 | } |
50 | Vector dependencyList = classFile.getClassRefs(); | | 50 | } |
51 | Enumeration depEnum = dependencyList.elements(); | | 51 | |
52 | while (depEnum.hasMoreElements()) { | | 52 | if (javaClass.isClass()) { |
53 | String dependency = (String) depEnum.nextElement(); | | 53 | String superClass = javaClass.getSuperclassName(); |
54 | analyzedDeps.put(dependency, dependency); | | 54 | if (!dependencies.containsKey(superClass)) { |
55 | } | | 55 | nextAnalyze.put(superClass, superClass); |
56 | } finally { | | 56 | } |
57 | if (inStream != null) { | | 57 | } |
58 | inStream.close(); | | 58 | } catch (IOException ioe) { |
59 | } | | 59 | // ignore |
60 | if (zipFile != null) { | | 60 | } |
61 | zipFile.close(); | | 61 | } |
62 | } | | 62 | |
63 | } | | 63 | Hashtable temp = toAnalyze; |
64 | } catch (IOException ioe) { | | 64 | toAnalyze = nextAnalyze; |
65 | // ignore | | 65 | nextAnalyze = temp; |
66 | } | | 66 | } |
67 | } | | 67 | |
68 | | | 68 | files.removeAllElements(); |
69 | toAnalyze.clear(); | | 69 | for (Enumeration e = containers.keys(); e.hasMoreElements();) { |
70 | | | 70 | files.addElement((File) e.nextElement()); |
71 | // now recover all the dependencies collected and add to the list. | | 71 | } |
72 | Enumeration depsEnum = analyzedDeps.elements(); | | 72 | |
73 | while (depsEnum.hasMoreElements()) { | | 73 | classes.removeAllElements(); |
74 | String className = (String) depsEnum.nextElement(); | | 74 | for (Enumeration e = dependencies.keys(); e.hasMoreElements();) { |
75 | if (!dependencies.containsKey(className)) { | | 75 | classes.addElement((String) e.nextElement()); |
76 | toAnalyze.put(className, className); | | 76 | } |
77 | } | | 77 | } |
78 | } | | | |
79 | } | | | |
80 | | | | |
81 | // pick up the last round of dependencies that were determined | | | |
82 | Enumeration depsEnum = analyzedDeps.elements(); | | | |
83 | while (depsEnum.hasMoreElements()) { | | | |
84 | String className = (String) depsEnum.nextElement(); | | | |
85 | dependencies.put(className, className); | | | |
86 | } | | | |
87 | | | | |
88 | files.removeAllElements(); | | | |
89 | for (Enumeration e = containers.keys(); e.hasMoreElements();) { | | | |
90 | files.addElement((File) e.nextElement()); | | | |
91 | } | | | |
92 | | | | |
93 | classes.removeAllElements(); | | | |
94 | for (Enumeration e = dependencies.keys(); e.hasMoreElements();) { | | | |
95 | classes.addElement((String) e.nextElement()); | | | |
96 | } | | | |
97 | } | | | |