diff options
author | Hugo Hudson <hugohudson@google.com> | 2012-01-16 17:56:36 +0000 |
---|---|---|
committer | Hugo Hudson <hugohudson@google.com> | 2012-01-16 17:56:36 +0000 |
commit | a790bf33cb3643efbbcc731408870d0194bc2b5a (patch) | |
tree | bac6d5b590a66f5b041cb10b2bd7b2372211774a | |
parent | ba73a086722c0eb6dd33e4de0764ae1f45d8d895 (diff) | |
download | littlemock-a790bf33cb3643efbbcc731408870d0194bc2b5a.tar.gz |
Update to r10 of LittleMock.
Change-Id: Ieb45372e45881e46037cb75e62b9355b5670e266
-rw-r--r-- | src/com/google/testing/littlemock/AppDataDirGuesser.java | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/src/com/google/testing/littlemock/AppDataDirGuesser.java b/src/com/google/testing/littlemock/AppDataDirGuesser.java index 0fdd475..8dd556e 100644 --- a/src/com/google/testing/littlemock/AppDataDirGuesser.java +++ b/src/com/google/testing/littlemock/AppDataDirGuesser.java @@ -17,6 +17,7 @@ package com.google.testing.littlemock; import java.io.File; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -43,7 +44,8 @@ public class AppDataDirGuesser { Class<?> clazz = Class.forName("dalvik.system.PathClassLoader"); clazz.cast(classLoader); // Use the toString() method to calculate the data directory. - String pathFromThisClassLoader = getPathFromThisClassLoader(classLoader); + String pathFromThisClassLoader = + getPathFromPathClassLoader(classLoader, clazz); File[] results = guessPath(pathFromThisClassLoader); if (results.length > 0) { return results[0]; @@ -56,7 +58,22 @@ public class AppDataDirGuesser { return null; } - private String getPathFromThisClassLoader(ClassLoader classLoader) { + private String getPathFromPathClassLoader( + ClassLoader classLoader, Class<?> pathClassLoaderClass) { + // Prior to ICS, we can simply read the "path" field of the + // PathClassLoader. + try { + Field pathField = pathClassLoaderClass.getDeclaredField("path"); + pathField.setAccessible(true); + return (String) pathField.get(classLoader); + } catch (NoSuchFieldException e) { + // Ignore and fall back on parsing the output of toString below + } catch (IllegalAccessException e) { + // Ignore and fall back on parsing the output of toString below + } catch (ClassCastException e) { + // Ignore and fall back on parsing the output of toString below + } + // Parsing toString() method: yuck. But no other way to get the path. // Strip out the bit between angle brackets, that's our path. String result = classLoader.toString(); @@ -82,9 +99,15 @@ public class AppDataDirGuesser { if (dash != -1) { end = dash; } - File file = new File("/data/data/" + potential.substring(start, end) + "/cache"); - if (isWriteableDirectory(file)) { - results.add(file); + String packageName = potential.substring(start, end); + File dataDir = new File("/data/data/" + packageName); + if (isWriteableDirectory(dataDir)) { + File cacheDir = new File(dataDir, "cache"); + if ((cacheDir.exists()) || (cacheDir.mkdir())) { + if (isWriteableDirectory(cacheDir)) { + results.add(cacheDir); + } + } } } return results.toArray(new File[results.size()]); |