diff options
author | wisberg <wisberg> | 2005-06-08 23:33:01 +0000 |
---|---|---|
committer | wisberg <wisberg> | 2005-06-08 23:33:01 +0000 |
commit | d851660694dc1ee5d44d0ffb3bf2381559e8ceb4 (patch) | |
tree | 1eaff217ffca1bd9a9d07c7662d7ece3bada610b /util | |
parent | ec99a1f3fed8237f2df73a5063d7039d762afa8d (diff) | |
download | aspectj-d851660694dc1ee5d44d0ffb3bf2381559e8ceb4.tar.gz aspectj-d851660694dc1ee5d44d0ffb3bf2381559e8ceb4.zip |
getBestFile(String[])
Diffstat (limited to 'util')
-rw-r--r-- | util/src/org/aspectj/util/FileUtil.java | 58 | ||||
-rw-r--r-- | util/testsrc/org/aspectj/util/FileUtilTest.java | 23 |
2 files changed, 81 insertions, 0 deletions
diff --git a/util/src/org/aspectj/util/FileUtil.java b/util/src/org/aspectj/util/FileUtil.java index ddaeedb43..a0f0a9021 100644 --- a/util/src/org/aspectj/util/FileUtil.java +++ b/util/src/org/aspectj/util/FileUtil.java @@ -339,6 +339,64 @@ public class FileUtil { } /** + * Get best File for the first-readable path in input paths, + * treating entries prefixed "sp:" as system property keys. + * Safe to call in static initializers. + * @param paths the String[] of paths to check. + * @return null if not found, or valid File otherwise + */ + public static File getBestFile(String[] paths) { + if (null == paths) { + return null; + } + File result = null; + for (int i = 0; (null == result) && (i < paths.length); i++) { + String path = paths[i]; + if (null == path) { + continue; + } + if (path.startsWith("sp:")) { + try { + path = System.getProperty(path.substring(3)); + } catch (Throwable t) { + path = null; + } + if (null == path) { + continue; + } + } + try { + File f = new File(path); + if (f.exists() && f.canRead()) { + result = FileUtil.getBestFile(f); + } + } catch (Throwable t) { + // swallow + } + } + return result; + } + + /** + * Render as best file, canonical or absolute. + * @param file the File to get the best File for (not null) + * @return File of the best-available path + * @throws IllegalArgumentException if file is null + */ + public static File getBestFile(File file) { + LangUtil.throwIaxIfNull(file, "file"); + if (file.exists()) { + try { + return file.getCanonicalFile(); + } catch (IOException e) { + return file.getAbsoluteFile(); + } + } else { + return file; + } + } + + /** * Render as best path, canonical or absolute. * @param file the File to get the path for (not null) * @return String of the best-available path diff --git a/util/testsrc/org/aspectj/util/FileUtilTest.java b/util/testsrc/org/aspectj/util/FileUtilTest.java index da2757009..c54080936 100644 --- a/util/testsrc/org/aspectj/util/FileUtilTest.java +++ b/util/testsrc/org/aspectj/util/FileUtilTest.java @@ -212,6 +212,29 @@ public class FileUtilTest extends TestCase { assertTrue(!noSuchFile.isDirectory()); } + public void testGetBestFile() { + assertNull(FileUtil.getBestFile((String[]) null)); + assertNull(FileUtil.getBestFile(new String[0])); + assertNull(FileUtil.getBestFile(new String[] {"!"})); + File f = FileUtil.getBestFile(new String[] {"."}); + assertNotNull(f); + f = FileUtil.getBestFile(new String[] {"!", "."}); + assertNotNull(f); + assertTrue(f.canRead()); + boolean setProperty = false; + try { + System.setProperty("bestfile", "."); + setProperty = true; + } catch (Throwable t) { + // ignore Security, etc. + } + if (setProperty) { + f = FileUtil.getBestFile(new String[] {"sp:bestfile"}); + assertNotNull(f); + assertTrue(f.canRead()); + } + } + public void testCopyFiles() { // bad input Class iaxClass = IllegalArgumentException.class; |