@@ -6,6 +6,7 @@
* the LICENSE file.
* the LICENSE file.
*/
*/
package org.apache.tools.ant.taskdefs.optional.depend;
package org.apache.tools.ant.taskdefs.optional.depend;
import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileInputStream;
@@ -17,15 +18,14 @@ import java.net.URL;
import java.util.Enumeration;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Vector;
import org.apache.myrmidon.api.TaskException;
import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.types.Reference;
/**
/**
* Generate a dependency file for a given set of classes
* Generate a dependency file for a given set of classes
*
*
@@ -152,7 +152,6 @@ public class Depend extends MatchingTask
this.dump = dump;
this.dump = dump;
}
}
/**
/**
* Set the source dirs to find the source Java files.
* Set the source dirs to find the source Java files.
*
*
@@ -190,10 +189,10 @@ public class Depend extends MatchingTask
/**
/**
* Does the work.
* Does the work.
*
*
* @exception Build Exception Thrown in unrecovrable error.
* @exception Task Exception Thrown in unrecovrable error.
*/
*/
public void execute()
public void execute()
throws Build Exception
throws Task Exception
{
{
try
try
{
{
@@ -201,7 +200,7 @@ public class Depend extends MatchingTask
String[] srcPathList = srcPath.list();
String[] srcPathList = srcPath.list();
if( srcPathList.length == 0 )
if( srcPathList.length == 0 )
{
{
throw new Build Exception( "srcdir attribute must be set!" );
throw new Task Exception( "srcdir attribute must be set!" );
}
}
if( destPath == null )
if( destPath == null )
@@ -211,7 +210,7 @@ public class Depend extends MatchingTask
if( cache != null && cache.exists() && !cache.isDirectory() )
if( cache != null && cache.exists() && !cache.isDirectory() )
{
{
throw new Build Exception( "The cache, if specified, must point to a directory" );
throw new Task Exception( "The cache, if specified, must point to a directory" );
}
}
if( cache != null && !cache.exists() )
if( cache != null && !cache.exists() )
@@ -224,16 +223,16 @@ public class Depend extends MatchingTask
if( dump )
if( dump )
{
{
log( "Reverse Dependency Dump for " + affectedClassMap.size() +
log( "Reverse Dependency Dump for " + affectedClassMap.size() +
" classes:", Project.MSG_DEBUG );
for( Enumeration e = affectedClassMap.keys(); e.hasMoreElements(); )
" classes:", Project.MSG_DEBUG );
for( Enumeration e = affectedClassMap.keys(); e.hasMoreElements(); )
{
{
String className = ( String )e.nextElement();
String className = (String)e.nextElement();
log( " Class " + className + " affects:", Project.MSG_DEBUG );
log( " Class " + className + " affects:", Project.MSG_DEBUG );
Hashtable affectedClasses = ( Hashtable )affectedClassMap.get( className );
for( Enumeration e2 = affectedClasses.keys(); e2.hasMoreElements(); )
Hashtable affectedClasses = (Hashtable)affectedClassMap.get( className );
for( Enumeration e2 = affectedClasses.keys(); e2.hasMoreElements(); )
{
{
String affectedClass = ( String )e2.nextElement();
ClassFileInfo info = ( ClassFileInfo )affectedClasses.get( affectedClass );
String affectedClass = (String)e2.nextElement();
ClassFileInfo info = (ClassFileInfo)affectedClasses.get( affectedClass );
log( " " + affectedClass + " in " + info.absoluteFile.getPath(), Project.MSG_DEBUG );
log( " " + affectedClass + " in " + info.absoluteFile.getPath(), Project.MSG_DEBUG );
}
}
}
}
@@ -241,14 +240,14 @@ public class Depend extends MatchingTask
if( classpathDependencies != null )
if( classpathDependencies != null )
{
{
log( "Classpath file dependencies (Forward):", Project.MSG_DEBUG );
log( "Classpath file dependencies (Forward):", Project.MSG_DEBUG );
for( Enumeration e = classpathDependencies.keys(); e.hasMoreElements(); )
for( Enumeration e = classpathDependencies.keys(); e.hasMoreElements(); )
{
{
String className = ( String )e.nextElement();
String className = (String)e.nextElement();
log( " Class " + className + " depends on:", Project.MSG_DEBUG );
log( " Class " + className + " depends on:", Project.MSG_DEBUG );
Hashtable dependencies = ( Hashtable )classpathDependencies.get( className );
for( Enumeration e2 = dependencies.elements(); e2.hasMoreElements(); )
Hashtable dependencies = (Hashtable)classpathDependencies.get( className );
for( Enumeration e2 = dependencies.elements(); e2.hasMoreElements(); )
{
{
File classpathFile = ( File )e2.nextElement();
File classpathFile = (File)e2.nextElement();
log( " " + classpathFile.getPath(), Project.MSG_DEBUG );
log( " " + classpathFile.getPath(), Project.MSG_DEBUG );
}
}
}
}
@@ -261,7 +260,7 @@ public class Depend extends MatchingTask
outOfDateClasses = new Hashtable();
outOfDateClasses = new Hashtable();
for( int i = 0; i < srcPathList.length; i++ )
for( int i = 0; i < srcPathList.length; i++ )
{
{
File srcDir = ( File )resolveFile( srcPathList[i] );
File srcDir = (File)resolveFile( srcPathList[ i ] );
if( srcDir.exists() )
if( srcDir.exists() )
{
{
DirectoryScanner ds = this.getDirectoryScanner( srcDir );
DirectoryScanner ds = this.getDirectoryScanner( srcDir );
@@ -273,25 +272,25 @@ public class Depend extends MatchingTask
// now check classpath file dependencies
// now check classpath file dependencies
if( classpathDependencies != null )
if( classpathDependencies != null )
{
{
for( Enumeration e = classpathDependencies.keys(); e.hasMoreElements(); )
for( Enumeration e = classpathDependencies.keys(); e.hasMoreElements(); )
{
{
String className = ( String )e.nextElement();
String className = (String)e.nextElement();
if( !outOfDateClasses.containsKey( className ) )
if( !outOfDateClasses.containsKey( className ) )
{
{
ClassFileInfo info = ( ClassFileInfo )classFileInfoMap.get( className );
ClassFileInfo info = (ClassFileInfo)classFileInfoMap.get( className );
// if we have no info about the class - it may have been deleted already and we
// if we have no info about the class - it may have been deleted already and we
// are using cached info.
// are using cached info.
if( info != null )
if( info != null )
{
{
Hashtable dependencies = ( Hashtable )classpathDependencies.get( className );
for( Enumeration e2 = dependencies.elements(); e2.hasMoreElements(); )
Hashtable dependencies = (Hashtable)classpathDependencies.get( className );
for( Enumeration e2 = dependencies.elements(); e2.hasMoreElements(); )
{
{
File classpathFile = ( File )e2.nextElement();
File classpathFile = (File)e2.nextElement();
if( classpathFile.lastModified() > info.absoluteFile.lastModified() )
if( classpathFile.lastModified() > info.absoluteFile.lastModified() )
{
{
log( "Class " + className +
log( "Class " + className +
" is out of date with respect to " + classpathFile, Project.MSG_DEBUG );
" is out of date with respect to " + classpathFile, Project.MSG_DEBUG );
outOfDateClasses.put( className, className );
outOfDateClasses.put( className, className );
break;
break;
}
}
@@ -310,7 +309,7 @@ public class Depend extends MatchingTask
}
}
catch( Exception e )
catch( Exception e )
{
{
throw new Build Exception( "Error", e );
throw new Task Exception( "Error", e );
}
}
}
}
@@ -328,14 +327,14 @@ public class Depend extends MatchingTask
for( int i = 0; i < files.length; i++ )
for( int i = 0; i < files.length; i++ )
{
{
File srcFile = new File( srcDir, files[i] );
if( files[i].endsWith( ".java" ) )
File srcFile = new File( srcDir, files[ i ] );
if( files[ i ].endsWith( ".java" ) )
{
{
String filePath = srcFile.getPath();
String filePath = srcFile.getPath();
String className = filePath.substring( srcDir.getPath().length() + 1,
String className = filePath.substring( srcDir.getPath().length() + 1,
filePath.length() - ".java".length() );
filePath.length() - ".java".length() );
className = ClassFileUtils.convertSlashName( className );
className = ClassFileUtils.convertSlashName( className );
ClassFileInfo info = ( ClassFileInfo )classFileInfoMap.get( className );
ClassFileInfo info = (ClassFileInfo)classFileInfoMap.get( className );
if( info == null )
if( info == null )
{
{
// there was no class file. add this class to the list
// there was no class file. add this class to the list
@@ -352,7 +351,6 @@ public class Depend extends MatchingTask
}
}
}
}
/**
/**
* Get the list of class files we are going to analyse.
* Get the list of class files we are going to analyse.
*
*
@@ -369,7 +367,7 @@ public class Depend extends MatchingTask
for( int i = 0; i < classLocationsList.length; ++i )
for( int i = 0; i < classLocationsList.length; ++i )
{
{
File dir = new File( classLocationsList[i] );
File dir = new File( classLocationsList[ i ] );
if( dir.isDirectory() )
if( dir.isDirectory() )
{
{
addClassFiles( classFileList, dir, dir );
addClassFiles( classFileList, dir, dir );
@@ -397,7 +395,7 @@ public class Depend extends MatchingTask
for( int i = 0; i < length; ++i )
for( int i = 0; i < length; ++i )
{
{
File file = new File( dir, filesInDir[i] );
File file = new File( dir, filesInDir[ i ] );
if( file.isDirectory() )
if( file.isDirectory() )
{
{
addClassFiles( classFileList, file, root );
addClassFiles( classFileList, file, root );
@@ -407,7 +405,7 @@ public class Depend extends MatchingTask
ClassFileInfo info = new ClassFileInfo();
ClassFileInfo info = new ClassFileInfo();
info.absoluteFile = file;
info.absoluteFile = file;
info.relativeName = file.getPath().substring( root.getPath().length() + 1,
info.relativeName = file.getPath().substring( root.getPath().length() + 1,
file.getPath().length() - 6 );
file.getPath().length() - 6 );
info.className = ClassFileUtils.convertSlashName( info.relativeName );
info.className = ClassFileUtils.convertSlashName( info.relativeName );
classFileList.addElement( info );
classFileList.addElement( info );
}
}
@@ -419,17 +417,17 @@ public class Depend extends MatchingTask
{
{
int count = 0;
int count = 0;
Hashtable affectedClasses = ( Hashtable )affectedClassMap.get( className );
Hashtable affectedClasses = (Hashtable)affectedClassMap.get( className );
if( affectedClasses != null )
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 );
String affectedClassName = (String)e.nextElement();
ClassFileInfo affectedClassInfo = (ClassFileInfo)affectedClasses.get( affectedClassName );
if( affectedClassInfo.absoluteFile.exists() )
if( affectedClassInfo.absoluteFile.exists() )
{
{
log( "Deleting file " + affectedClassInfo.absoluteFile.getPath() + " since " +
log( "Deleting file " + affectedClassInfo.absoluteFile.getPath() + " since " +
className + " out of date", Project.MSG_VERBOSE );
className + " out of date", Project.MSG_VERBOSE );
affectedClassInfo.absoluteFile.delete();
affectedClassInfo.absoluteFile.delete();
count++;
count++;
if( closure )
if( closure )
@@ -445,15 +443,15 @@ public class Depend extends MatchingTask
{
{
// need to delete the main class
// need to delete the main class
String topLevelClassName
String topLevelClassName
= affectedClassName.substring( 0, affectedClassName.indexOf( "$" ) );
= affectedClassName.substring( 0, affectedClassName.indexOf( "$" ) );
log( "Top level class = " + topLevelClassName, Project.MSG_VERBOSE );
log( "Top level class = " + topLevelClassName, Project.MSG_VERBOSE );
ClassFileInfo topLevelClassInfo
ClassFileInfo topLevelClassInfo
= ( ClassFileInfo )classFileInfoMap.get( topLevelClassName );
= (ClassFileInfo)classFileInfoMap.get( topLevelClassName );
if( topLevelClassInfo != null &&
if( topLevelClassInfo != null &&
topLevelClassInfo.absoluteFile.exists() )
topLevelClassInfo.absoluteFile.exists() )
{
{
log( "Deleting file " + topLevelClassInfo.absoluteFile.getPath() + " since " +
log( "Deleting file " + topLevelClassInfo.absoluteFile.getPath() + " since " +
"one of its inner classes was removed", Project.MSG_VERBOSE );
"one of its inner classes was removed", Project.MSG_VERBOSE );
topLevelClassInfo.absoluteFile.delete();
topLevelClassInfo.absoluteFile.delete();
count++;
count++;
if( closure )
if( closure )
@@ -472,11 +470,11 @@ public class Depend extends MatchingTask
private int deleteAllAffectedFiles()
private int deleteAllAffectedFiles()
{
{
int count = 0;
int count = 0;
for( Enumeration e = outOfDateClasses.elements(); e.hasMoreElements(); )
for( Enumeration e = outOfDateClasses.elements(); e.hasMoreElements(); )
{
{
String className = ( String )e.nextElement();
String className = (String)e.nextElement();
count += deleteAffectedFiles( className );
count += deleteAffectedFiles( className );
ClassFileInfo classInfo = ( ClassFileInfo )classFileInfoMap.get( className );
ClassFileInfo classInfo = (ClassFileInfo)classFileInfoMap.get( className );
if( classInfo != null && classInfo.absoluteFile.exists() )
if( classInfo != null && classInfo.absoluteFile.exists() )
{
{
classInfo.absoluteFile.delete();
classInfo.absoluteFile.delete();
@@ -486,7 +484,6 @@ public class Depend extends MatchingTask
return count;
return count;
}
}
/**
/**
* Determine the dependencies between classes. Class dependencies are
* Determine the dependencies between classes. Class dependencies are
* determined by examining the class references in a class file to other
* determined by examining the class references in a class file to other
@@ -495,7 +492,7 @@ public class Depend extends MatchingTask
* @exception IOException Description of Exception
* @exception IOException Description of Exception
*/
*/
private void determineDependencies()
private void determineDependencies()
throws IOException
throws IOException, TaskException
{
{
affectedClassMap = new Hashtable();
affectedClassMap = new Hashtable();
classFileInfoMap = new Hashtable();
classFileInfoMap = new Hashtable();
@@ -514,9 +511,9 @@ public class Depend extends MatchingTask
depCacheFileExists = depCacheFile.exists();
depCacheFileExists = depCacheFile.exists();
depCacheFileLastModified = depCacheFile.lastModified();
depCacheFileLastModified = depCacheFile.lastModified();
}
}
for( Enumeration e = getClassFiles( destPath ).elements(); e.hasMoreElements(); )
for( Enumeration e = getClassFiles( destPath ).elements(); e.hasMoreElements(); )
{
{
ClassFileInfo info = ( ClassFileInfo )e.nextElement();
ClassFileInfo info = (ClassFileInfo)e.nextElement();
log( "Adding class info for " + info.className, Project.MSG_DEBUG );
log( "Adding class info for " + info.className, Project.MSG_DEBUG );
classFileInfoMap.put( info.className, info );
classFileInfoMap.put( info.className, info );
@@ -529,7 +526,7 @@ public class Depend extends MatchingTask
{
{
// depFile exists and is newer than the class file
// depFile exists and is newer than the class file
// need to get dependency list from the map.
// need to get dependency list from the map.
dependencyList = ( Vector )dependencyMap.get( info.className );
dependencyList = (Vector)dependencyMap.get( info.className );
}
}
}
}
@@ -562,11 +559,11 @@ public class Depend extends MatchingTask
// This class depends on each class in the dependency list. For each
// This class depends on each class in the dependency list. For each
// one of those, add this class into their affected classes list
// one of those, add this class into their affected classes list
for( Enumeration depEnum = dependencyList.elements(); depEnum.hasMoreElements(); )
for( Enumeration depEnum = dependencyList.elements(); depEnum.hasMoreElements(); )
{
{
String dependentClass = ( String )depEnum.nextElement();
String dependentClass = (String)depEnum.nextElement();
Hashtable affectedClasses = ( Hashtable )affectedClassMap.get( dependentClass );
Hashtable affectedClasses = (Hashtable)affectedClassMap.get( dependentClass );
if( affectedClasses == null )
if( affectedClasses == null )
{
{
affectedClasses = new Hashtable();
affectedClasses = new Hashtable();
@@ -586,15 +583,15 @@ public class Depend extends MatchingTask
Hashtable classpathFileCache = new Hashtable();
Hashtable classpathFileCache = new Hashtable();
Object nullFileMarker = new Object();
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 );
String className = (String)e.nextElement();
Vector dependencyList = (Vector)dependencyMap.get( className );
Hashtable dependencies = new Hashtable();
Hashtable dependencies = new Hashtable();
classpathDependencies.put( className, dependencies );
classpathDependencies.put( className, dependencies );
for( Enumeration e2 = dependencyList.elements(); e2.hasMoreElements(); )
for( Enumeration e2 = dependencyList.elements(); e2.hasMoreElements(); )
{
{
String dependency = ( String )e2.nextElement();
String dependency = (String)e2.nextElement();
Object classpathFileObject = classpathFileCache.get( dependency );
Object classpathFileObject = classpathFileCache.get( dependency );
if( classpathFileObject == null )
if( classpathFileObject == null )
{
{
@@ -621,8 +618,8 @@ public class Depend extends MatchingTask
classpathFileObject = new File( classFilePath );
classpathFileObject = new File( classFilePath );
}
}
log( "Class " + className +
log( "Class " + className +
" depends on " + classpathFileObject +
" due to " + dependency, Project.MSG_DEBUG );
" depends on " + classpathFileObject +
" due to " + dependency, Project.MSG_DEBUG );
}
}
}
}
classpathFileCache.put( dependency, classpathFileObject );
classpathFileCache.put( dependency, classpathFileObject );
@@ -630,7 +627,7 @@ public class Depend extends MatchingTask
if( classpathFileObject != null && classpathFileObject != nullFileMarker )
if( classpathFileObject != null && classpathFileObject != nullFileMarker )
{
{
// we need to add this jar to the list for this class.
// we need to add this jar to the list for this class.
File jarFile = ( File )classpathFileObject;
File jarFile = (File)classpathFileObject;
dependencies.put( jarFile, jarFile );
dependencies.put( jarFile, jarFile );
}
}
}
}
@@ -713,13 +710,13 @@ public class Depend extends MatchingTask
File depFile = new File( cache, CACHE_FILE_NAME );
File depFile = new File( cache, CACHE_FILE_NAME );
pw = new PrintWriter( new FileWriter( depFile ) );
pw = new PrintWriter( new FileWriter( depFile ) );
for( Enumeration deps = dependencyMap.keys(); deps.hasMoreElements(); )
for( Enumeration deps = dependencyMap.keys(); deps.hasMoreElements(); )
{
{
String className = ( String )deps.nextElement();
String className = (String)deps.nextElement();
pw.println( CLASSNAME_PREPEND + className );
pw.println( CLASSNAME_PREPEND + className );
Vector dependencyList = ( Vector )dependencyMap.get( className );
Vector dependencyList = (Vector)dependencyMap.get( className );
int size = dependencyList.size();
int size = dependencyList.size();
for( int x = 0; x < size; x++ )
for( int x = 0; x < size; x++ )
{
{