From 2c3767a537410d491b803796180e94b91cf52fd7 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Mon, 8 Apr 2002 13:57:06 +0000 Subject: [PATCH] Add capability to classfileset to specify the root classes using a fileset git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272292 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/OptionalTypes/classfileset.html | 17 ++++++ .../ant/taskdefs/optional/depend/Depend.java | 56 +++++++++++-------- .../types/optional/depend/ClassfileSet.java | 42 ++++++++++++-- .../types/optional/depend/DependScanner.java | 9 ++- 4 files changed, 95 insertions(+), 29 deletions(-) diff --git a/docs/manual/OptionalTypes/classfileset.html b/docs/manual/OptionalTypes/classfileset.html index 1384b4337..ecd1facee 100644 --- a/docs/manual/OptionalTypes/classfileset.html +++ b/docs/manual/OptionalTypes/classfileset.html @@ -60,6 +60,14 @@ may be used +

RootFileSet

+

+A root fileset is used to add a set of root classes from a fileset. In this case the entries in +the fileset are expected to be Java class files. The name of the Java class is determined by the +relative location of the classfile in the fileset. So, the file +org/apache/tools/ant/Project.class corresponds to the Java class +org.apache.tools.ant.Project.

+

Examples

 <classfileset id="reqdClasses" dir="${classes.dir}">
@@ -78,6 +86,15 @@ then be used to create a jar.
 </jar>
 
+
+<classfileset id="reqdClasses" dir="${classes.dir}">
+  <rootfileset dir="${classes.dir}" includes="org/apache/tools/ant/Project*.class" />
+</classfileset>
+
+ +

This example constructs the classfileset using all the class with names starting with Project +in the org.apache.tools.ant package

+

Copyright © 2002 Apache Software Foundation. All rights Reserved.

diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java b/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java index 4dd6c69cd..b70e10637 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/Depend.java @@ -86,16 +86,16 @@ public class Depend extends MatchingTask { */ private static class ClassFileInfo { /** The file where the class file is stored in the file system */ - public File absoluteFile; + private File absoluteFile; /** * The location of the file relative to its base directory - the * root of the package namespace */ - public String relativeName; + private String relativeName; /** The Java class name of this class */ - public String className; + private String className; } /** The path where source files exist */ @@ -142,9 +142,9 @@ public class Depend extends MatchingTask { private Path dependClasspath; /** constants used with the cache file */ - private final static String CACHE_FILE_NAME = "dependencies.txt"; + private static final String CACHE_FILE_NAME = "dependencies.txt"; /** String Used to separate classnames in the dependency file */ - private final static String CLASSNAME_PREPEND = "||:"; + private static final String CLASSNAME_PREPEND = "||:"; /** * Set the classpath to be used for this dependency check. @@ -292,7 +292,7 @@ public class Depend extends MatchingTask { depCacheFileExists = depCacheFile.exists(); depCacheFileLastModified = depCacheFile.lastModified(); } - for (Enumeration e = getClassFiles(destPath).elements(); e.hasMoreElements(); ) { + for (Enumeration e = getClassFiles(destPath).elements(); e.hasMoreElements();) { ClassFileInfo info = (ClassFileInfo)e.nextElement(); log("Adding class info for " + info.className, Project.MSG_DEBUG); classFileInfoMap.put(info.className, info); @@ -300,8 +300,10 @@ public class Depend extends MatchingTask { Vector dependencyList = null; if (cache != null) { - // try to read the dependency info from the map if it is not out of date - if (depCacheFileExists && depCacheFileLastModified > info.absoluteFile.lastModified()) { + // try to read the dependency info from the map if it is + // not out of date + if (depCacheFileExists + && depCacheFileLastModified > info.absoluteFile.lastModified()) { // depFile exists and is newer than the class file // need to get dependency list from the map. dependencyList = (Vector)dependencyMap.get(info.className); @@ -333,7 +335,8 @@ public class Depend extends MatchingTask { for (Enumeration depEnum = dependencyList.elements(); depEnum.hasMoreElements(); ) { String dependentClass = (String)depEnum.nextElement(); - Hashtable affectedClasses = (Hashtable)affectedClassMap.get(dependentClass); + Hashtable affectedClasses + = (Hashtable)affectedClassMap.get(dependentClass); if (affectedClasses == null) { affectedClasses = new Hashtable(); affectedClassMap.put(dependentClass, affectedClasses); @@ -347,22 +350,25 @@ public class Depend extends MatchingTask { if (dependClasspath != null) { // now determine which jars each class depends upon classpathDependencies = new Hashtable(); - AntClassLoader loader = new AntClassLoader(getProject(), dependClasspath); + AntClassLoader loader + = new AntClassLoader(getProject(), dependClasspath); Hashtable classpathFileCache = new Hashtable(); Object nullFileMarker = new Object(); - for (Enumeration e = dependencyMap.keys(); e.hasMoreElements(); ) { + for (Enumeration e = dependencyMap.keys(); e.hasMoreElements();) { String className = (String)e.nextElement(); Vector dependencyList = (Vector)dependencyMap.get(className); Hashtable dependencies = new Hashtable(); classpathDependencies.put(className, dependencies); - for (Enumeration e2 = dependencyList.elements(); e2.hasMoreElements(); ) { + for (Enumeration e2 = dependencyList.elements(); e2.hasMoreElements();) { String dependency = (String)e2.nextElement(); - Object classpathFileObject = classpathFileCache.get(dependency); + Object classpathFileObject + = classpathFileCache.get(dependency); if (classpathFileObject == null) { classpathFileObject = nullFileMarker; - if (!dependency.startsWith("java.") && !dependency.startsWith("javax.")) { + if (!dependency.startsWith("java.") + && !dependency.startsWith("javax.")) { URL classURL = loader.getResource(dependency.replace('.', '/') + ".class"); if (classURL != null) { if (classURL.getProtocol().equals("jar")) { @@ -406,10 +412,11 @@ public class Depend extends MatchingTask { */ private int deleteAllAffectedFiles() { int count = 0; - for (Enumeration e = outOfDateClasses.elements(); e.hasMoreElements(); ) { + for (Enumeration e = outOfDateClasses.elements(); e.hasMoreElements();) { String className = (String)e.nextElement(); count += deleteAffectedFiles(className); - ClassFileInfo classInfo = (ClassFileInfo)classFileInfoMap.get(className); + ClassFileInfo classInfo + = (ClassFileInfo)classFileInfoMap.get(className); if (classInfo != null && classInfo.absoluteFile.exists()) { classInfo.absoluteFile.delete(); count++; @@ -430,7 +437,7 @@ public class Depend extends MatchingTask { Hashtable affectedClasses = (Hashtable)affectedClassMap.get(className); if (affectedClasses != null) { - for (Enumeration e = affectedClasses.keys(); e.hasMoreElements(); ) { + for (Enumeration e = affectedClasses.keys(); e.hasMoreElements();) { String affectedClassName = (String)e.nextElement(); ClassFileInfo affectedClassInfo = (ClassFileInfo)affectedClasses.get(affectedClassName); if (affectedClassInfo.absoluteFile.exists()) { @@ -479,7 +486,8 @@ public class Depend extends MatchingTask { long start = System.currentTimeMillis(); String[] srcPathList = srcPath.list(); if (srcPathList.length == 0) { - throw new BuildException("srcdir attribute must be set!", location); + throw new BuildException("srcdir attribute must be set!", + location); } if (destPath == null) { @@ -487,7 +495,8 @@ public class Depend extends MatchingTask { } if (cache != null && cache.exists() && !cache.isDirectory()) { - throw new BuildException("The cache, if specified, must point to a directory"); + throw new BuildException("The cache, if specified, must " + + "point to a directory"); } if (cache != null && !cache.exists()) { @@ -499,11 +508,11 @@ public class Depend extends MatchingTask { if (dump) { log("Reverse Dependency Dump for " + affectedClassMap.size() + " classes:", Project.MSG_DEBUG); - for (Enumeration e = affectedClassMap.keys(); e.hasMoreElements(); ) { + for (Enumeration e = affectedClassMap.keys(); e.hasMoreElements();) { String className = (String)e.nextElement(); log(" Class " + className + " affects:", Project.MSG_DEBUG); Hashtable affectedClasses = (Hashtable)affectedClassMap.get(className); - for (Enumeration e2 = affectedClasses.keys(); e2.hasMoreElements(); ) { + for (Enumeration e2 = affectedClasses.keys(); e2.hasMoreElements();) { String affectedClass = (String)e2.nextElement(); ClassFileInfo info = (ClassFileInfo)affectedClasses.get(affectedClass); log(" " + affectedClass + " in " + info.absoluteFile.getPath(), Project.MSG_DEBUG); @@ -525,7 +534,8 @@ public class Depend extends MatchingTask { } // we now need to scan for out of date files. When we have the list - // we go through and delete all class files which are affected by these files. + // we go through and delete all class files which are affected by + // these files. outOfDateClasses = new Hashtable(); for (int i = 0; i < srcPathList.length; i++) { File srcDir = (File)project.resolveFile(srcPathList[i]); @@ -538,7 +548,7 @@ public class Depend extends MatchingTask { // now check classpath file dependencies if (classpathDependencies != null) { - for (Enumeration e = classpathDependencies.keys(); e.hasMoreElements(); ) { + for (Enumeration e = classpathDependencies.keys(); e.hasMoreElements();) { String className = (String)e.nextElement(); if (!outOfDateClasses.containsKey(className)) { ClassFileInfo info = (ClassFileInfo)classFileInfoMap.get(className); diff --git a/src/main/org/apache/tools/ant/types/optional/depend/ClassfileSet.java b/src/main/org/apache/tools/ant/types/optional/depend/ClassfileSet.java index d92bf2855..eb7982e0e 100644 --- a/src/main/org/apache/tools/ant/types/optional/depend/ClassfileSet.java +++ b/src/main/org/apache/tools/ant/types/optional/depend/ClassfileSet.java @@ -53,14 +53,12 @@ */ package org.apache.tools.ant.types.optional.depend; - - import java.util.Vector; +import java.util.Enumeration; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.FileSet; - /** * A ClassfileSet is a FileSet, that enlists all classes that depend on a * certain set of root classes. @@ -79,6 +77,11 @@ public class ClassfileSet extends FileSet { */ private Vector rootClasses = new Vector(); + /** + * The list of filesets which contain root classes + */ + private Vector rootFileSets = new Vector(); + /** * Inner class used to contain info about root classes */ @@ -104,13 +107,24 @@ public class ClassfileSet extends FileSet { return rootClass; } } - + /** * Default constructor */ public ClassfileSet() { } + /** + * Add a fileset to which contains a collection of root classes used to + * drive the search from classes + * + * @param rootFileSet a root file set to be used to search for dependent + * classes + */ + public void addRootFileset(FileSet rootFileSet) { + rootFileSets.addElement(rootFileSet); + } + /** * Create a ClassfileSet from another ClassfileSet * @@ -142,10 +156,28 @@ public class ClassfileSet extends FileSet { return getRef(p).getDirectoryScanner(p); } + Vector allRootClasses = (Vector)rootClasses.clone(); + for (Enumeration e = rootFileSets.elements(); e.hasMoreElements();) { + FileSet additionalRootSet = (FileSet)e.nextElement(); + DirectoryScanner additionalScanner + = additionalRootSet.getDirectoryScanner(p); + String[] files = additionalScanner.getIncludedFiles(); + for (int i = 0; i < files.length; ++i) { + if (files[i].endsWith(".class")) { + String classFilePath + = files[i].substring(0, files[i].length() - 6); + String className + = classFilePath.replace('/', '.').replace('\\', '.'); + allRootClasses.addElement(className); + } + } + } + + DirectoryScanner parentScanner = super.getDirectoryScanner(p); DependScanner scanner = new DependScanner(parentScanner); scanner.setBasedir(getDir(p)); - scanner.setRootClasses(rootClasses); + scanner.setRootClasses(allRootClasses); scanner.scan(); return scanner; } diff --git a/src/main/org/apache/tools/ant/types/optional/depend/DependScanner.java b/src/main/org/apache/tools/ant/types/optional/depend/DependScanner.java index d60cd98af..3800c831e 100644 --- a/src/main/org/apache/tools/ant/types/optional/depend/DependScanner.java +++ b/src/main/org/apache/tools/ant/types/optional/depend/DependScanner.java @@ -99,6 +99,13 @@ public class DependScanner extends DirectoryScanner { */ private DirectoryScanner parentScanner; + /** + * Create a DependScanner, using the given scanner to provide the basic + * set of files from which class files come. + * + * @param parentScanner the DirectoryScanner which returns the files from + * which class files must come. + */ public DependScanner(DirectoryScanner parentScanner) { this.parentScanner = parentScanner; } @@ -161,7 +168,7 @@ public class DependScanner extends DirectoryScanner { } analyzer.addClassPath(new Path(null, basedir.getPath())); - for (Enumeration e = rootClasses.elements(); e.hasMoreElements(); ) { + for (Enumeration e = rootClasses.elements(); e.hasMoreElements();) { String rootClass = (String)e.nextElement(); analyzer.addRootClass(rootClass); }