KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > optional > depend > DependScanner


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.types.optional.depend;
19
20 import java.io.File JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.DirectoryScanner;
26 import org.apache.tools.ant.types.Path;
27 import org.apache.tools.ant.util.depend.DependencyAnalyzer;
28
29
30 /**
31  * DirectoryScanner for finding class dependencies.
32  */

33 public class DependScanner extends DirectoryScanner {
34     /**
35      * The name of the analyzer to use by default.
36      */

37     public static final String JavaDoc DEFAULT_ANALYZER_CLASS
38         = "org.apache.tools.ant.util.depend.bcel.FullAnalyzer";
39
40     /**
41      * The root classes to drive the search for dependent classes.
42      */

43     private Vector JavaDoc rootClasses;
44
45     /**
46      * The names of the classes to include in the fileset.
47      */

48     private Vector JavaDoc included;
49
50     /**
51      * The parent scanner which gives the basic set of files. Only files which
52      * are in this set and which can be reached from a root class will end
53      * up being included in the result set.
54      */

55     private DirectoryScanner parentScanner;
56
57     /**
58      * Create a DependScanner, using the given scanner to provide the basic
59      * set of files from which class files come.
60      *
61      * @param parentScanner the DirectoryScanner which returns the files from
62      * which class files must come.
63      */

64     public DependScanner(DirectoryScanner parentScanner) {
65         this.parentScanner = parentScanner;
66     }
67
68     /**
69      * Sets the root classes to be used to drive the scan.
70      *
71      * @param rootClasses the rootClasses to be used for this scan.
72      */

73     public synchronized void setRootClasses(Vector JavaDoc rootClasses) {
74         this.rootClasses = rootClasses;
75     }
76
77     /**
78      * Get the names of the class files on which baseClass depends.
79      *
80      * @return the names of the files.
81      */

82     public String JavaDoc[] getIncludedFiles() {
83         String JavaDoc[] files = new String JavaDoc[getIncludedFilesCount()];
84         for (int i = 0; i < files.length; i++) {
85             files[i] = (String JavaDoc) included.elementAt(i);
86         }
87         return files;
88     }
89
90     /** {@inheritDoc}. */
91     public synchronized int getIncludedFilesCount() {
92         if (included == null) {
93             throw new IllegalStateException JavaDoc();
94         }
95         return included.size();
96     }
97
98     /**
99      * Scans the base directory for files on which baseClass depends.
100      *
101      * @exception IllegalStateException when basedir was set incorrectly.
102      */

103     public synchronized void scan() throws IllegalStateException JavaDoc {
104         included = new Vector JavaDoc();
105         String JavaDoc analyzerClassName = DEFAULT_ANALYZER_CLASS;
106         DependencyAnalyzer analyzer = null;
107         try {
108             Class JavaDoc analyzerClass = Class.forName(analyzerClassName);
109             analyzer = (DependencyAnalyzer) analyzerClass.newInstance();
110         } catch (Exception JavaDoc e) {
111             throw new BuildException("Unable to load dependency analyzer: "
112                 + analyzerClassName, e);
113         }
114         analyzer.addClassPath(new Path(null, basedir.getPath()));
115
116         for (Enumeration JavaDoc e = rootClasses.elements(); e.hasMoreElements();) {
117             String JavaDoc rootClass = (String JavaDoc) e.nextElement();
118             analyzer.addRootClass(rootClass);
119         }
120         Enumeration JavaDoc e = analyzer.getClassDependencies();
121
122         String JavaDoc[] parentFiles = parentScanner.getIncludedFiles();
123         Hashtable JavaDoc parentSet = new Hashtable JavaDoc();
124         for (int i = 0; i < parentFiles.length; ++i) {
125             parentSet.put(parentFiles[i], parentFiles[i]);
126         }
127         while (e.hasMoreElements()) {
128             String JavaDoc classname = (String JavaDoc) e.nextElement();
129             String JavaDoc filename = classname.replace('.', File.separatorChar);
130             filename = filename + ".class";
131             File JavaDoc depFile = new File JavaDoc(basedir, filename);
132             if (depFile.exists() && parentSet.containsKey(filename)) {
133                 // This is included
134
included.addElement(filename);
135             }
136         }
137     }
138
139     /**
140      * @see DirectoryScanner#addDefaultExcludes
141      */

142     public void addDefaultExcludes() {
143     }
144
145     /**
146      * @see DirectoryScanner#getExcludedDirectories
147      */

148     /** {@inheritDoc}. */
149     public String JavaDoc[] getExcludedDirectories() {
150         return null;
151     }
152
153     /**
154      * @see DirectoryScanner#getExcludedFiles
155      */

156     /** {@inheritDoc}. */
157     public String JavaDoc[] getExcludedFiles() {
158         return null;
159     }
160
161     /**
162      * @see DirectoryScanner#getIncludedDirectories
163      */

164     /** {@inheritDoc}. */
165     public String JavaDoc[] getIncludedDirectories() {
166         return new String JavaDoc[0];
167     }
168
169     /**
170      * @see DirectoryScanner#getIncludedDirsCount
171      */

172     /** {@inheritDoc}. */
173     public int getIncludedDirsCount() {
174         return 0;
175     }
176
177     /**
178      * @see DirectoryScanner#getNotIncludedDirectories
179      */

180     /** {@inheritDoc}. */
181     public String JavaDoc[] getNotIncludedDirectories() {
182         return null;
183     }
184
185     /**
186      * @see DirectoryScanner#getNotIncludedFiles
187      */

188     /** {@inheritDoc}. */
189     public String JavaDoc[] getNotIncludedFiles() {
190         return null;
191     }
192
193     /**
194      * @see DirectoryScanner#setExcludes
195      */

196     /** {@inheritDoc}. */
197     public void setExcludes(String JavaDoc[] excludes) {
198     }
199
200     /**
201      * @see DirectoryScanner#setIncludes
202      */

203     /** {@inheritDoc}. */
204     public void setIncludes(String JavaDoc[] includes) {
205     }
206
207     /**
208      * @see DirectoryScanner#setCaseSensitive
209      */

210     /** {@inheritDoc}. */
211     public void setCaseSensitive(boolean isCaseSensitive) {
212     }
213 }
214
Popular Tags