1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.depend;
|
---|
18 |
|
---|
19 | import java.io.BufferedReader;
|
---|
20 | import java.io.File;
|
---|
21 | import java.io.FileReader;
|
---|
22 | import java.io.FileWriter;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.io.PrintWriter;
|
---|
25 | import java.net.URL;
|
---|
26 | import java.util.Enumeration;
|
---|
27 | import java.util.Hashtable;
|
---|
28 | import java.util.Vector;
|
---|
29 | import org.apache.tools.ant.AntClassLoader;
|
---|
30 | import org.apache.tools.ant.BuildException;
|
---|
31 | import org.apache.tools.ant.DirectoryScanner;
|
---|
32 | import org.apache.tools.ant.Project;
|
---|
33 | import org.apache.tools.ant.taskdefs.MatchingTask;
|
---|
34 | import org.apache.tools.ant.types.Path;
|
---|
35 | import org.apache.tools.ant.types.Reference;
|
---|
36 | import org.apache.tools.ant.util.depend.DependencyAnalyzer;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Generates a dependency file for a given set of classes.
|
---|
40 | *
|
---|
41 | */
|
---|
42 | public class Depend extends MatchingTask {
|
---|
43 | /**
|
---|
44 | * A class (struct) user to manage information about a class
|
---|
45 | *
|
---|
46 | */
|
---|
47 | private static class ClassFileInfo {
|
---|
48 | /** The file where the class file is stored in the file system */
|
---|
49 | private File absoluteFile;
|
---|
50 |
|
---|
51 | /** The Java class name of this class */
|
---|
52 | private String className;
|
---|
53 |
|
---|
54 | /** The source File containing this class */
|
---|
55 | private File sourceFile;
|
---|
56 |
|
---|
57 | /** if user has been warned about this file not having a source file */
|
---|
58 | private boolean isUserWarned = false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** The path where source files exist */
|
---|
62 | private Path srcPath;
|
---|
63 |
|
---|
64 | /** The path where compiled class files exist. */
|
---|
65 | private Path destPath;
|
---|
66 |
|
---|
67 | /** The directory which contains the dependency cache. */
|
---|
68 | private File cache;
|
---|
69 |
|
---|
70 | /** The list of source paths derived from the srcPath field. */
|
---|
71 | private String[] srcPathList;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * A map which gives for every class a list of the class which it
|
---|
75 | * affects.
|
---|
76 | */
|
---|
77 | private Hashtable affectedClassMap;
|
---|
78 |
|
---|
79 | /** A map which gives information about a class */
|
---|
80 | private Hashtable classFileInfoMap;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * A map which gives the list of jars and classes from the classpath
|
---|
84 | * that a class depends upon
|
---|
85 | */
|
---|
86 | private Hashtable classpathDependencies;
|
---|
87 |
|
---|
88 | /** The list of classes which are out of date. */
|
---|
89 | private Hashtable outOfDateClasses;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * indicates that the dependency relationships should be extended beyond
|
---|
93 | * direct dependencies to include all classes. So if A directly affects
|
---|
94 | * B and B directly affects C, then A indirectly affects C.
|
---|
95 | */
|
---|
96 | private boolean closure = false;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Flag which controls whether the reversed dependencies should be
|
---|
100 | * dumped to the log
|
---|
101 | */
|
---|
102 | private boolean dump = false;
|
---|
103 |
|
---|
104 | /** The classpath to look for additional dependencies */
|
---|
105 | private Path dependClasspath;
|
---|
106 |
|
---|
107 | /** constants used with the cache file */
|
---|
108 | private static final String CACHE_FILE_NAME = "dependencies.txt";
|
---|
109 | /** String Used to separate classnames in the dependency file */
|
---|
110 | private static final String CLASSNAME_PREPEND = "||:";
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Set the classpath to be used for this dependency check.
|
---|
114 | *
|
---|
115 | * @param classpath the classpath to be used when checking for
|
---|
116 | * dependencies on elements in the classpath
|
---|
117 | */
|
---|
118 | public void setClasspath(Path classpath) {
|
---|
119 | if (dependClasspath == null) {
|
---|
120 | dependClasspath = classpath;
|
---|
121 | } else {
|
---|
122 | dependClasspath.append(classpath);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Gets the classpath to be used for this dependency check.
|
---|
128 | *
|
---|
129 | * @return the current dependency classpath
|
---|
130 | */
|
---|
131 | public Path getClasspath() {
|
---|
132 | return dependClasspath;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Adds a classpath to be used for this dependency check.
|
---|
137 | *
|
---|
138 | * @return A path object to be configured by Ant
|
---|
139 | */
|
---|
140 | public Path createClasspath() {
|
---|
141 | if (dependClasspath == null) {
|
---|
142 | dependClasspath = new Path(getProject());
|
---|
143 | }
|
---|
144 | return dependClasspath.createPath();
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Adds a reference to a classpath defined elsewhere.
|
---|
149 | *
|
---|
150 | * @param r a reference to a path object to be used as the depend
|
---|
151 | * classpath
|
---|
152 | */
|
---|
153 | public void setClasspathRef(Reference r) {
|
---|
154 | createClasspath().setRefid(r);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Read the dependencies from cache file
|
---|
159 | *
|
---|
160 | * @return a collection of class dependencies
|
---|
161 | * @exception IOException if the dependency file cannot be read
|
---|
162 | */
|
---|
163 | private Hashtable readCachedDependencies(File depFile) throws IOException {
|
---|
164 | Hashtable dependencyMap = new Hashtable();
|
---|
165 |
|
---|
166 | BufferedReader in = null;
|
---|
167 | try {
|
---|
168 | in = new BufferedReader(new FileReader(depFile));
|
---|
169 | String line = null;
|
---|
170 | Vector dependencyList = null;
|
---|
171 | String className = null;
|
---|
172 | int prependLength = CLASSNAME_PREPEND.length();
|
---|
173 | while ((line = in.readLine()) != null) {
|
---|
174 | if (line.startsWith(CLASSNAME_PREPEND)) {
|
---|
175 | dependencyList = new Vector();
|
---|
176 | className = line.substring(prependLength);
|
---|
177 | dependencyMap.put(className, dependencyList);
|
---|
178 | } else {
|
---|
179 | dependencyList.addElement(line);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | } finally {
|
---|
183 | if (in != null) {
|
---|
184 | in.close();
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | return dependencyMap;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Write the dependencies to cache file
|
---|
193 | *
|
---|
194 | * @param dependencyMap the map of dependencies to be written out.
|
---|
195 | * @exception IOException if the dependency file cannot be written out.
|
---|
196 | */
|
---|
197 | private void writeCachedDependencies(Hashtable dependencyMap)
|
---|
198 | throws IOException {
|
---|
199 | if (cache != null) {
|
---|
200 | PrintWriter pw = null;
|
---|
201 | try {
|
---|
202 | cache.mkdirs();
|
---|
203 | File depFile = new File(cache, CACHE_FILE_NAME);
|
---|
204 |
|
---|
205 | pw = new PrintWriter(new FileWriter(depFile));
|
---|
206 | Enumeration e = dependencyMap.keys();
|
---|
207 | while (e.hasMoreElements()) {
|
---|
208 | String className = (String) e.nextElement();
|
---|
209 |
|
---|
210 | pw.println(CLASSNAME_PREPEND + className);
|
---|
211 |
|
---|
212 | Vector dependencyList
|
---|
213 | = (Vector) dependencyMap.get(className);
|
---|
214 | int size = dependencyList.size();
|
---|
215 | for (int x = 0; x < size; x++) {
|
---|
216 | pw.println(dependencyList.elementAt(x));
|
---|
217 | }
|
---|
218 | }
|
---|
219 | } finally {
|
---|
220 | if (pw != null) {
|
---|
221 | pw.close();
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Get the classpath for dependency checking.
|
---|
229 | *
|
---|
230 | * This method removes the dest dirs if it is given from the dependency classpath
|
---|
231 | */
|
---|
232 | private Path getCheckClassPath() {
|
---|
233 | if (dependClasspath == null) {
|
---|
234 | return null;
|
---|
235 | }
|
---|
236 |
|
---|
237 | String[] destPathElements = destPath.list();
|
---|
238 | String[] classpathElements = dependClasspath.list();
|
---|
239 | String checkPath = "";
|
---|
240 | for (int i = 0; i < classpathElements.length; ++i) {
|
---|
241 | String element = classpathElements[i];
|
---|
242 | boolean inDestPath = false;
|
---|
243 | for (int j = 0; j < destPathElements.length && !inDestPath; ++j) {
|
---|
244 | inDestPath = destPathElements[j].equals(element);
|
---|
245 | }
|
---|
246 | if (!inDestPath) {
|
---|
247 | if (checkPath.length() == 0) {
|
---|
248 | checkPath = element;
|
---|
249 | } else {
|
---|
250 | checkPath += ":" + element;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (checkPath.length() == 0) {
|
---|
256 | return null;
|
---|
257 | }
|
---|
258 |
|
---|
259 | return new Path(getProject(), checkPath);
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Determine the dependencies between classes. Class dependencies are
|
---|
264 | * determined by examining the class references in a class file to other
|
---|
265 | * classes.
|
---|
266 | *
|
---|
267 | * This method sets up the following fields
|
---|
268 | * <ul>
|
---|
269 | * <li>affectedClassMap - the list of classes each class affects</li>
|
---|
270 | * <li>classFileInfoMap - information about each class</li>
|
---|
271 | * <li>classpathDependencies - the list of jars and classes from the
|
---|
272 | * classpath that each class depends upon.</li>
|
---|
273 | * </ul>
|
---|
274 | *
|
---|
275 | * If required, the dependencies are written to the cache.
|
---|
276 | *
|
---|
277 | * @exception IOException if either the dependencies cache or the class
|
---|
278 | * files cannot be read or written
|
---|
279 | */
|
---|
280 | private void determineDependencies() throws IOException {
|
---|
281 | affectedClassMap = new Hashtable();
|
---|
282 | classFileInfoMap = new Hashtable();
|
---|
283 | boolean cacheDirty = false;
|
---|
284 |
|
---|
285 | Hashtable dependencyMap = new Hashtable();
|
---|
286 | File cacheFile = null;
|
---|
287 | boolean cacheFileExists = true;
|
---|
288 | long cacheLastModified = Long.MAX_VALUE;
|
---|
289 |
|
---|
290 | // read the dependency cache from the disk
|
---|
291 | if (cache != null) {
|
---|
292 | cacheFile = new File(cache, CACHE_FILE_NAME);
|
---|
293 | cacheFileExists = cacheFile.exists();
|
---|
294 | cacheLastModified = cacheFile.lastModified();
|
---|
295 | if (cacheFileExists) {
|
---|
296 | dependencyMap = readCachedDependencies(cacheFile);
|
---|
297 | }
|
---|
298 | }
|
---|
299 | Enumeration classfileEnum = getClassFiles(destPath).elements();
|
---|
300 | while (classfileEnum.hasMoreElements()) {
|
---|
301 | ClassFileInfo info = (ClassFileInfo) classfileEnum.nextElement();
|
---|
302 | log("Adding class info for " + info.className, Project.MSG_DEBUG);
|
---|
303 | classFileInfoMap.put(info.className, info);
|
---|
304 |
|
---|
305 | Vector dependencyList = null;
|
---|
306 |
|
---|
307 | if (cache != null) {
|
---|
308 | // try to read the dependency info from the map if it is
|
---|
309 | // not out of date
|
---|
310 | if (cacheFileExists
|
---|
311 | && cacheLastModified > info.absoluteFile.lastModified()) {
|
---|
312 | // depFile exists and is newer than the class file
|
---|
313 | // need to get dependency list from the map.
|
---|
314 | dependencyList = (Vector) dependencyMap.get(info.className);
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | if (dependencyList == null) {
|
---|
319 | // not cached - so need to read directly from the class file
|
---|
320 | DependencyAnalyzer analyzer = new AntAnalyzer();
|
---|
321 | analyzer.addRootClass(info.className);
|
---|
322 | analyzer.addClassPath(destPath);
|
---|
323 | analyzer.setClosure(false);
|
---|
324 | dependencyList = new Vector();
|
---|
325 | Enumeration depEnum = analyzer.getClassDependencies();
|
---|
326 | while (depEnum.hasMoreElements()) {
|
---|
327 | dependencyList.addElement(depEnum.nextElement());
|
---|
328 | }
|
---|
329 | if (dependencyList != null) {
|
---|
330 | cacheDirty = true;
|
---|
331 | dependencyMap.put(info.className, dependencyList);
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | // This class depends on each class in the dependency list. For each
|
---|
336 | // one of those, add this class into their affected classes list
|
---|
337 | Enumeration depEnum = dependencyList.elements();
|
---|
338 | while (depEnum.hasMoreElements()) {
|
---|
339 | String dependentClass = (String) depEnum.nextElement();
|
---|
340 |
|
---|
341 | Hashtable affectedClasses
|
---|
342 | = (Hashtable) affectedClassMap.get(dependentClass);
|
---|
343 | if (affectedClasses == null) {
|
---|
344 | affectedClasses = new Hashtable();
|
---|
345 | affectedClassMap.put(dependentClass, affectedClasses);
|
---|
346 | }
|
---|
347 |
|
---|
348 | affectedClasses.put(info.className, info);
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | classpathDependencies = null;
|
---|
353 | Path checkPath = getCheckClassPath();
|
---|
354 | if (checkPath != null) {
|
---|
355 | // now determine which jars each class depends upon
|
---|
356 | classpathDependencies = new Hashtable();
|
---|
357 | AntClassLoader loader = getProject().createClassLoader(checkPath);
|
---|
358 |
|
---|
359 | Hashtable classpathFileCache = new Hashtable();
|
---|
360 | Object nullFileMarker = new Object();
|
---|
361 | for (Enumeration e = dependencyMap.keys(); e.hasMoreElements();) {
|
---|
362 | String className = (String) e.nextElement();
|
---|
363 | Vector dependencyList = (Vector) dependencyMap.get(className);
|
---|
364 | Hashtable dependencies = new Hashtable();
|
---|
365 | classpathDependencies.put(className, dependencies);
|
---|
366 | Enumeration e2 = dependencyList.elements();
|
---|
367 | while (e2.hasMoreElements()) {
|
---|
368 | String dependency = (String) e2.nextElement();
|
---|
369 | Object classpathFileObject
|
---|
370 | = classpathFileCache.get(dependency);
|
---|
371 | if (classpathFileObject == null) {
|
---|
372 | classpathFileObject = nullFileMarker;
|
---|
373 |
|
---|
374 | if (!dependency.startsWith("java.")
|
---|
375 | && !dependency.startsWith("javax.")) {
|
---|
376 | URL classURL
|
---|
377 | = loader.getResource(dependency.replace('.', '/') + ".class");
|
---|
378 | if (classURL != null) {
|
---|
379 | if (classURL.getProtocol().equals("jar")) {
|
---|
380 | String jarFilePath = classURL.getFile();
|
---|
381 | if (jarFilePath.startsWith("file:")) {
|
---|
382 | int classMarker = jarFilePath.indexOf('!');
|
---|
383 | jarFilePath = jarFilePath.substring(5, classMarker);
|
---|
384 | }
|
---|
385 | classpathFileObject = new File(jarFilePath);
|
---|
386 | } else if (classURL.getProtocol().equals("file")) {
|
---|
387 | String classFilePath = classURL.getFile();
|
---|
388 | classpathFileObject = new File(classFilePath);
|
---|
389 | }
|
---|
390 | log("Class " + className
|
---|
391 | + " depends on " + classpathFileObject
|
---|
392 | + " due to " + dependency, Project.MSG_DEBUG);
|
---|
393 | }
|
---|
394 | }
|
---|
395 | classpathFileCache.put(dependency, classpathFileObject);
|
---|
396 | }
|
---|
397 | if (classpathFileObject != null && classpathFileObject != nullFileMarker) {
|
---|
398 | // we need to add this jar to the list for this class.
|
---|
399 | File jarFile = (File) classpathFileObject;
|
---|
400 | dependencies.put(jarFile, jarFile);
|
---|
401 | }
|
---|
402 | }
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | // write the dependency cache to the disk
|
---|
407 | if (cache != null && cacheDirty) {
|
---|
408 | writeCachedDependencies(dependencyMap);
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 | /**
|
---|
413 | * Delete all the class files which are out of date, by way of their
|
---|
414 | * dependency on a class which is out of date
|
---|
415 | *
|
---|
416 | * @return the number of files deleted.
|
---|
417 | */
|
---|
418 | private int deleteAllAffectedFiles() {
|
---|
419 | int count = 0;
|
---|
420 | for (Enumeration e = outOfDateClasses.elements(); e.hasMoreElements();) {
|
---|
421 | String className = (String) e.nextElement();
|
---|
422 | count += deleteAffectedFiles(className);
|
---|
423 | ClassFileInfo classInfo
|
---|
424 | = (ClassFileInfo) classFileInfoMap.get(className);
|
---|
425 | if (classInfo != null && classInfo.absoluteFile.exists()) {
|
---|
426 | classInfo.absoluteFile.delete();
|
---|
427 | count++;
|
---|
428 | }
|
---|
429 | }
|
---|
430 | return count;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Delete all the class files of classes which depend on the given class
|
---|
435 | *
|
---|
436 | * @param className the name of the class whose dependent classes will be
|
---|
437 | * deleted
|
---|
438 | * @return the number of class files removed
|
---|
439 | */
|
---|
440 | private int deleteAffectedFiles(String className) {
|
---|
441 | int count = 0;
|
---|
442 |
|
---|
443 | Hashtable affectedClasses = (Hashtable) affectedClassMap.get(className);
|
---|
444 | if (affectedClasses == null) {
|
---|
445 | return count;
|
---|
446 | }
|
---|
447 | for (Enumeration e = affectedClasses.keys(); e.hasMoreElements();) {
|
---|
448 | String affectedClass = (String) e.nextElement();
|
---|
449 | ClassFileInfo affectedClassInfo
|
---|
450 | = (ClassFileInfo) affectedClasses.get(affectedClass);
|
---|
451 |
|
---|
452 | if (!affectedClassInfo.absoluteFile.exists()) {
|
---|
453 | continue;
|
---|
454 | }
|
---|
455 |
|
---|
456 | if (affectedClassInfo.sourceFile == null) {
|
---|
457 | if (!affectedClassInfo.isUserWarned) {
|
---|
458 | log("The class " + affectedClass + " in file "
|
---|
459 | + affectedClassInfo.absoluteFile.getPath()
|
---|
460 | + " is out of date due to " + className
|
---|
461 | + " but has not been deleted because its source file"
|
---|
462 | + " could not be determined", Project.MSG_WARN);
|
---|
463 | affectedClassInfo.isUserWarned = true;
|
---|
464 | }
|
---|
465 | continue;
|
---|
466 | }
|
---|
467 |
|
---|
468 | log("Deleting file " + affectedClassInfo.absoluteFile.getPath()
|
---|
469 | + " since " + className + " out of date", Project.MSG_VERBOSE);
|
---|
470 |
|
---|
471 | affectedClassInfo.absoluteFile.delete();
|
---|
472 | count++;
|
---|
473 | if (closure) {
|
---|
474 | count += deleteAffectedFiles(affectedClass);
|
---|
475 | } else {
|
---|
476 | // without closure we may delete an inner class but not the
|
---|
477 | // top level class which would not trigger a recompile.
|
---|
478 |
|
---|
479 | if (affectedClass.indexOf("$") == -1) {
|
---|
480 | continue;
|
---|
481 | }
|
---|
482 | // need to delete the main class
|
---|
483 | String topLevelClassName
|
---|
484 | = affectedClass.substring(0, affectedClass.indexOf("$"));
|
---|
485 | log("Top level class = " + topLevelClassName,
|
---|
486 | Project.MSG_VERBOSE);
|
---|
487 | ClassFileInfo topLevelClassInfo
|
---|
488 | = (ClassFileInfo) classFileInfoMap.get(topLevelClassName);
|
---|
489 | if (topLevelClassInfo != null
|
---|
490 | && topLevelClassInfo.absoluteFile.exists()) {
|
---|
491 | log("Deleting file "
|
---|
492 | + topLevelClassInfo.absoluteFile.getPath()
|
---|
493 | + " since one of its inner classes was removed",
|
---|
494 | Project.MSG_VERBOSE);
|
---|
495 | topLevelClassInfo.absoluteFile.delete();
|
---|
496 | count++;
|
---|
497 | if (closure) {
|
---|
498 | count += deleteAffectedFiles(topLevelClassName);
|
---|
499 | }
|
---|
500 | }
|
---|
501 | }
|
---|
502 | }
|
---|
503 | return count;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * Dump the dependency information loaded from the classes to the Ant log
|
---|
508 | */
|
---|
509 | private void dumpDependencies() {
|
---|
510 | log("Reverse Dependency Dump for " + affectedClassMap.size()
|
---|
511 | + " classes:", Project.MSG_DEBUG);
|
---|
512 |
|
---|
513 | Enumeration classEnum = affectedClassMap.keys();
|
---|
514 | while (classEnum.hasMoreElements()) {
|
---|
515 | String className = (String) classEnum.nextElement();
|
---|
516 | log(" Class " + className + " affects:", Project.MSG_DEBUG);
|
---|
517 | Hashtable affectedClasses
|
---|
518 | = (Hashtable) affectedClassMap.get(className);
|
---|
519 | Enumeration affectedClassEnum = affectedClasses.keys();
|
---|
520 | while (affectedClassEnum.hasMoreElements()) {
|
---|
521 | String affectedClass = (String) affectedClassEnum.nextElement();
|
---|
522 | ClassFileInfo info
|
---|
523 | = (ClassFileInfo) affectedClasses.get(affectedClass);
|
---|
524 | log(" " + affectedClass + " in "
|
---|
525 | + info.absoluteFile.getPath(), Project.MSG_DEBUG);
|
---|
526 | }
|
---|
527 | }
|
---|
528 |
|
---|
529 | if (classpathDependencies != null) {
|
---|
530 | log("Classpath file dependencies (Forward):", Project.MSG_DEBUG);
|
---|
531 |
|
---|
532 | Enumeration classpathEnum = classpathDependencies.keys();
|
---|
533 | while (classpathEnum.hasMoreElements()) {
|
---|
534 | String className = (String) classpathEnum.nextElement();
|
---|
535 | log(" Class " + className + " depends on:", Project.MSG_DEBUG);
|
---|
536 | Hashtable dependencies
|
---|
537 | = (Hashtable) classpathDependencies.get(className);
|
---|
538 |
|
---|
539 | Enumeration classpathFileEnum = dependencies.elements();
|
---|
540 | while (classpathFileEnum.hasMoreElements()) {
|
---|
541 | File classpathFile = (File) classpathFileEnum.nextElement();
|
---|
542 | log(" " + classpathFile.getPath(), Project.MSG_DEBUG);
|
---|
543 | }
|
---|
544 | }
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | private void determineOutOfDateClasses() {
|
---|
549 | outOfDateClasses = new Hashtable();
|
---|
550 | for (int i = 0; i < srcPathList.length; i++) {
|
---|
551 | File srcDir = (File) getProject().resolveFile(srcPathList[i]);
|
---|
552 | if (srcDir.exists()) {
|
---|
553 | DirectoryScanner ds = this.getDirectoryScanner(srcDir);
|
---|
554 | String[] files = ds.getIncludedFiles();
|
---|
555 | scanDir(srcDir, files);
|
---|
556 | }
|
---|
557 | }
|
---|
558 |
|
---|
559 | // now check classpath file dependencies
|
---|
560 | if (classpathDependencies == null) {
|
---|
561 | return;
|
---|
562 | }
|
---|
563 |
|
---|
564 | Enumeration classpathDepsEnum = classpathDependencies.keys();
|
---|
565 | while (classpathDepsEnum.hasMoreElements()) {
|
---|
566 | String className = (String) classpathDepsEnum.nextElement();
|
---|
567 | if (outOfDateClasses.containsKey(className)) {
|
---|
568 | continue;
|
---|
569 | }
|
---|
570 | ClassFileInfo info
|
---|
571 | = (ClassFileInfo) classFileInfoMap.get(className);
|
---|
572 |
|
---|
573 | // if we have no info about the class - it may have been deleted already and we
|
---|
574 | // are using cached info.
|
---|
575 | if (info != null) {
|
---|
576 | Hashtable dependencies
|
---|
577 | = (Hashtable) classpathDependencies.get(className);
|
---|
578 | for (Enumeration e2 = dependencies.elements(); e2.hasMoreElements();) {
|
---|
579 | File classpathFile = (File) e2.nextElement();
|
---|
580 | if (classpathFile.lastModified()
|
---|
581 | > info.absoluteFile.lastModified()) {
|
---|
582 | log("Class " + className
|
---|
583 | + " is out of date with respect to "
|
---|
584 | + classpathFile, Project.MSG_DEBUG);
|
---|
585 | outOfDateClasses.put(className, className);
|
---|
586 | break;
|
---|
587 | }
|
---|
588 | }
|
---|
589 | }
|
---|
590 | }
|
---|
591 | }
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Does the work.
|
---|
595 | *
|
---|
596 | * @exception BuildException Thrown in case of an unrecoverable error.
|
---|
597 | */
|
---|
598 | public void execute() throws BuildException {
|
---|
599 | try {
|
---|
600 | long start = System.currentTimeMillis();
|
---|
601 | if (srcPath == null) {
|
---|
602 | throw new BuildException("srcdir attribute must be set",
|
---|
603 | getLocation());
|
---|
604 | }
|
---|
605 |
|
---|
606 | srcPathList = srcPath.list();
|
---|
607 | if (srcPathList.length == 0) {
|
---|
608 | throw new BuildException("srcdir attribute must be non-empty",
|
---|
609 | getLocation());
|
---|
610 | }
|
---|
611 |
|
---|
612 | if (destPath == null) {
|
---|
613 | destPath = srcPath;
|
---|
614 | }
|
---|
615 |
|
---|
616 | if (cache != null && cache.exists() && !cache.isDirectory()) {
|
---|
617 | throw new BuildException("The cache, if specified, must "
|
---|
618 | + "point to a directory");
|
---|
619 | }
|
---|
620 |
|
---|
621 | if (cache != null && !cache.exists()) {
|
---|
622 | cache.mkdirs();
|
---|
623 | }
|
---|
624 |
|
---|
625 | determineDependencies();
|
---|
626 | if (dump) {
|
---|
627 | dumpDependencies();
|
---|
628 | }
|
---|
629 | determineOutOfDateClasses();
|
---|
630 | int count = deleteAllAffectedFiles();
|
---|
631 |
|
---|
632 | long duration = (System.currentTimeMillis() - start) / 1000;
|
---|
633 |
|
---|
634 | final int summaryLogLevel;
|
---|
635 | if(count>0) {
|
---|
636 | summaryLogLevel = Project.MSG_INFO;
|
---|
637 | } else {
|
---|
638 | summaryLogLevel = Project.MSG_DEBUG;
|
---|
639 | }
|
---|
640 |
|
---|
641 | log("Deleted " + count + " out of date files in "
|
---|
642 | + duration + " seconds", summaryLogLevel);
|
---|
643 | } catch (Exception e) {
|
---|
644 | throw new BuildException(e);
|
---|
645 | }
|
---|
646 | }
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Scans the directory looking for source files that are newer than
|
---|
650 | * their class files. The results are returned in the class variable
|
---|
651 | * compileList
|
---|
652 | *
|
---|
653 | * @param srcDir the source directory
|
---|
654 | * @param files the names of the files in the source dir which are to be
|
---|
655 | * checked.
|
---|
656 | */
|
---|
657 | protected void scanDir(File srcDir, String files[]) {
|
---|
658 |
|
---|
659 | for (int i = 0; i < files.length; i++) {
|
---|
660 | File srcFile = new File(srcDir, files[i]);
|
---|
661 | if (files[i].endsWith(".java")) {
|
---|
662 | String filePath = srcFile.getPath();
|
---|
663 | String className
|
---|
664 | = filePath.substring(srcDir.getPath().length() + 1,
|
---|
665 | filePath.length() - ".java".length());
|
---|
666 | className = ClassFileUtils.convertSlashName(className);
|
---|
667 | ClassFileInfo info
|
---|
668 | = (ClassFileInfo) classFileInfoMap.get(className);
|
---|
669 | if (info == null) {
|
---|
670 | // there was no class file. add this class to the list
|
---|
671 | outOfDateClasses.put(className, className);
|
---|
672 | } else {
|
---|
673 | if (srcFile.lastModified()
|
---|
674 | > info.absoluteFile.lastModified()) {
|
---|
675 | outOfDateClasses.put(className, className);
|
---|
676 | }
|
---|
677 | }
|
---|
678 | }
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * Get the list of class files we are going to analyse.
|
---|
685 | *
|
---|
686 | * @param classLocations a path structure containing all the directories
|
---|
687 | * where classes can be found.
|
---|
688 | * @return a vector containing the classes to analyse.
|
---|
689 | */
|
---|
690 | private Vector getClassFiles(Path classLocations) {
|
---|
691 | // break the classLocations into its components.
|
---|
692 | String[] classLocationsList = classLocations.list();
|
---|
693 |
|
---|
694 | Vector classFileList = new Vector();
|
---|
695 |
|
---|
696 | for (int i = 0; i < classLocationsList.length; ++i) {
|
---|
697 | File dir = new File(classLocationsList[i]);
|
---|
698 | if (dir.isDirectory()) {
|
---|
699 | addClassFiles(classFileList, dir, dir);
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 | return classFileList;
|
---|
704 | }
|
---|
705 |
|
---|
706 | /**
|
---|
707 | * Find the source file for a given class
|
---|
708 | *
|
---|
709 | * @param classname the classname in slash format.
|
---|
710 | */
|
---|
711 | private File findSourceFile(String classname) {
|
---|
712 | String sourceFilename = classname + ".java";
|
---|
713 | int innerIndex = classname.indexOf("$");
|
---|
714 | if (innerIndex != -1) {
|
---|
715 | sourceFilename = classname.substring(0, innerIndex) + ".java";
|
---|
716 | }
|
---|
717 |
|
---|
718 | // search the various source path entries
|
---|
719 | for (int i = 0; i < srcPathList.length; ++i) {
|
---|
720 | File sourceFile = new File(srcPathList[i], sourceFilename);
|
---|
721 | if (sourceFile.exists()) {
|
---|
722 | return sourceFile;
|
---|
723 | }
|
---|
724 | }
|
---|
725 | return null;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /**
|
---|
729 | * Add the list of class files from the given directory to the class
|
---|
730 | * file vector, including any subdirectories.
|
---|
731 | *
|
---|
732 | * @param classFileList a list of ClassFileInfo objects for all the
|
---|
733 | * files in the directory tree
|
---|
734 | * @param dir the directory tree to be searched, recursively, for class
|
---|
735 | * files
|
---|
736 | * @param root the root of the source tree. This is used to determine
|
---|
737 | * the absolute class name from the relative position in the
|
---|
738 | * source tree
|
---|
739 | */
|
---|
740 | private void addClassFiles(Vector classFileList, File dir, File root) {
|
---|
741 | String[] filesInDir = dir.list();
|
---|
742 |
|
---|
743 | if (filesInDir == null) {
|
---|
744 | return;
|
---|
745 | }
|
---|
746 | int length = filesInDir.length;
|
---|
747 |
|
---|
748 | int rootLength = root.getPath().length();
|
---|
749 | for (int i = 0; i < length; ++i) {
|
---|
750 | File file = new File(dir, filesInDir[i]);
|
---|
751 | if (file.isDirectory()) {
|
---|
752 | addClassFiles(classFileList, file, root);
|
---|
753 | } else if (file.getName().endsWith(".class")) {
|
---|
754 | ClassFileInfo info = new ClassFileInfo();
|
---|
755 | info.absoluteFile = file;
|
---|
756 | String relativeName = file.getPath().substring(rootLength + 1,
|
---|
757 | file.getPath().length() - 6);
|
---|
758 | info.className
|
---|
759 | = ClassFileUtils.convertSlashName(relativeName);
|
---|
760 | info.sourceFile = findSourceFile(relativeName);
|
---|
761 | classFileList.addElement(info);
|
---|
762 | }
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * Set the directories path to find the Java source files.
|
---|
769 | *
|
---|
770 | * @param srcPath the source path
|
---|
771 | */
|
---|
772 | public void setSrcdir(Path srcPath) {
|
---|
773 | this.srcPath = srcPath;
|
---|
774 | }
|
---|
775 |
|
---|
776 | /**
|
---|
777 | * Set the destination directory where the compiled Java files exist.
|
---|
778 | *
|
---|
779 | * @param destPath the destination areas where build files are written
|
---|
780 | */
|
---|
781 | public void setDestDir(Path destPath) {
|
---|
782 | this.destPath = destPath;
|
---|
783 | }
|
---|
784 |
|
---|
785 | /**
|
---|
786 | * Sets the dependency cache file.
|
---|
787 | *
|
---|
788 | * @param cache the dependency cache file
|
---|
789 | */
|
---|
790 | public void setCache(File cache) {
|
---|
791 | this.cache = cache;
|
---|
792 | }
|
---|
793 |
|
---|
794 | /**
|
---|
795 | * If true, transitive dependencies are followed until the
|
---|
796 | * closure of the dependency set if reached.
|
---|
797 | * When not set, the depend task will only follow
|
---|
798 | * direct dependencies between classes.
|
---|
799 | *
|
---|
800 | * @param closure indicate if dependency closure is required.
|
---|
801 | */
|
---|
802 | public void setClosure(boolean closure) {
|
---|
803 | this.closure = closure;
|
---|
804 | }
|
---|
805 |
|
---|
806 | /**
|
---|
807 | * If true, the dependency information will be written
|
---|
808 | * to the debug level log.
|
---|
809 | *
|
---|
810 | * @param dump set to true to dump dependency information to the log
|
---|
811 | */
|
---|
812 | public void setDump(boolean dump) {
|
---|
813 | this.dump = dump;
|
---|
814 | }
|
---|
815 | }
|
---|
816 |
|
---|