Browse Source

Make FileUtils.resolveFile more robust - modelled after File(File, String).

This avoids the explicit checks for null directories in
SourceFileScanner as well.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269755 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
361eba4355
3 changed files with 27 additions and 13 deletions
  1. +10
    -1
      src/main/org/apache/tools/ant/util/FileUtils.java
  2. +2
    -12
      src/main/org/apache/tools/ant/util/SourceFileScanner.java
  3. +15
    -0
      src/testcases/org/apache/tools/ant/util/FileUtilsTest.java

+ 10
- 1
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -299,7 +299,9 @@ public class FileUtils {
* @param file the "reference" file for relative paths. This
* instance must be an absolute file and must not contain
* "./" or "../" sequences (same for \ instead
* of /).
* of /). If it is null, this call is equivalent to
* <code>new java.io.File(filename)</code>.
*
* @param filename a file name
*
* @return an absolute file that doesn't contain &quot;./&quot; or
@@ -327,6 +329,10 @@ public class FileUtils {
return normalize(filename);
}

if (file == null) {
return new File(filename);
}

File helpFile = new File(file.getAbsolutePath());
StringTokenizer tok = new StringTokenizer(filename, File.separator);
while (tok.hasMoreTokens()) {
@@ -361,6 +367,9 @@ public class FileUtils {
* <li>DOS style paths that start with a drive letter will have
* \ as the separator.</li>
* </ul>
*
* @throws java.lang.NullPointerException if the file path is
* equal to null.
*/
public File normalize(String path) {
String orig = path;


+ 2
- 12
src/main/org/apache/tools/ant/util/SourceFileScanner.java View File

@@ -121,12 +121,7 @@ public class SourceFileScanner {
continue;
}

File src = null;
if (srcDir == null) {
src = new File(files[i]);
} else {
src = fileUtils.resolveFile(srcDir, files[i]);
}
File src = fileUtils.resolveFile(srcDir, files[i]);

if (src.lastModified() > now) {
task.log("Warning: "+files[i]+" modified in the future.",
@@ -136,12 +131,7 @@ public class SourceFileScanner {
boolean added = false;
targetList.setLength(0);
for (int j=0; !added && j<targets.length; j++) {
File dest = null;
if (destDir == null) {
dest = new File(targets[j]);
} else {
dest = fileUtils.resolveFile(destDir, targets[j]);
}
File dest = fileUtils.resolveFile(destDir, targets[j]);
if (!dest.exists()) {
task.log(files[i]+" added as "+dest.getAbsolutePath()+" doesn\'t exist.",


+ 15
- 0
src/testcases/org/apache/tools/ant/util/FileUtilsTest.java View File

@@ -258,6 +258,21 @@ public class FileUtilsTest extends TestCase {
}
}

/**
* Test handling of null arguments.
*/
public void testNullArgs() {
try {
fu.normalize(null);
fail("successfully normalized a null-file");
} catch (NullPointerException npe) {
// Expected exception caught
}
File f = fu.resolveFile(null, "a");
assertEquals(f, new File("a"));
}

/**
* adapt file separators to local conventions
*/


Loading…
Cancel
Save