KickJava   Java API By Example, From Geeks To Geeks.

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


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.DescendingVisitor;
26 import org.apache.bcel.classfile.JavaClass;
27 import org.apache.tools.ant.util.depend.AbstractAnalyzer;
28
29 /**
30  * An analyzer capable fo traversing all class - class relationships.
31  *
32  */

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

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

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

127     protected boolean supportsFileDependencies() {
128         return true;
129     }
130 }
131
132
Popular Tags