KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > depend > bcel > AncestorAnalyzer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util.depend.bcel;
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.bcel.classfile.ClassParser;
25 import org.apache.bcel.classfile.JavaClass;
26 import org.apache.tools.ant.util.depend.AbstractAnalyzer;
27
28 /**
29  * A dependency analyzer which returns superclass and superinterface
30  * dependencies.
31  *
32  */

33 public class AncestorAnalyzer extends AbstractAnalyzer {
34
35     /**
36      * Default constructor
37      *
38      * Causes the BCEL classes to load to ensure BCEL dependencies can
39      * be satisfied
40      */

41     public AncestorAnalyzer() {
42         // force BCEL classes to load now
43
try {
44             new ClassParser("force");
45         } catch (IOException JavaDoc e) {
46             // ignore
47
}
48     }
49
50     /**
51      * Determine the dependencies of the configured root classes.
52      *
53      * @param files a vector to be populated with the files which contain
54      * the dependency classes
55      * @param classes a vector to be populated with the names of the
56      * depencency classes.
57      */

58     protected void determineDependencies(Vector JavaDoc files, Vector JavaDoc classes) {
59         // we get the root classes and build up a set of
60
// classes upon which they depend
61
Hashtable JavaDoc dependencies = new Hashtable JavaDoc();
62         Hashtable JavaDoc containers = new Hashtable JavaDoc();
63         Hashtable JavaDoc toAnalyze = new Hashtable JavaDoc();
64         Hashtable JavaDoc nextAnalyze = new Hashtable JavaDoc();
65         for (Enumeration JavaDoc e = getRootClasses(); e.hasMoreElements();) {
66             String JavaDoc classname = (String JavaDoc) e.nextElement();
67             toAnalyze.put(classname, classname);
68         }
69
70         int count = 0;
71         int maxCount = isClosureRequired() ? MAX_LOOPS : 2;
72         while (toAnalyze.size() != 0 && count++ < maxCount) {
73             nextAnalyze.clear();
74             for (Enumeration JavaDoc e = toAnalyze.keys(); e.hasMoreElements();) {
75                 String JavaDoc classname = (String JavaDoc) e.nextElement();
76                 dependencies.put(classname, classname);
77                 try {
78                     File JavaDoc container = getClassContainer(classname);
79                     if (container == null) {
80                         continue;
81                     }
82                     containers.put(container, container);
83
84                     ClassParser parser = null;
85                     if (container.getName().endsWith(".class")) {
86                         parser = new ClassParser(container.getPath());
87                     } else {
88                         parser = new ClassParser(container.getPath(),
89                             classname.replace('.', '/') + ".class");
90                     }
91
92                     JavaClass javaClass = parser.parse();
93                     String JavaDoc[] interfaces = javaClass.getInterfaceNames();
94                     for (int i = 0; i < interfaces.length; ++i) {
95                         String JavaDoc interfaceName = interfaces[i];
96                         if (!dependencies.containsKey(interfaceName)) {
97                             nextAnalyze.put(interfaceName, interfaceName);
98                         }
99                     }
100
101                     if (javaClass.isClass()) {
102                         String JavaDoc superClass = javaClass.getSuperclassName();
103                         if (!dependencies.containsKey(superClass)) {
104                             nextAnalyze.put(superClass, superClass);
105                         }
106                     }
107                 } catch (IOException JavaDoc ioe) {
108                     // ignore
109
}
110             }
111
112             Hashtable JavaDoc temp = toAnalyze;
113             toAnalyze = nextAnalyze;
114             nextAnalyze = temp;
115         }
116
117         files.removeAllElements();
118         for (Enumeration JavaDoc e = containers.keys(); e.hasMoreElements();) {
119             files.addElement((File JavaDoc) e.nextElement());
120         }
121
122         classes.removeAllElements();
123         for (Enumeration JavaDoc e = dependencies.keys(); e.hasMoreElements();) {
124             classes.addElement((String JavaDoc) e.nextElement());
125         }
126     }
127
128     /**
129      * Indicate if this analyzer can determine dependent files.
130      *
131      * @return true if the analyzer provides dependency file information.
132      */

133     protected boolean supportsFileDependencies() {
134         return true;
135     }
136
137 }
138
139
Popular Tags