diff options
author | Kevin Jin <kjin@google.com> | 2013-07-23 14:26:02 -0700 |
---|---|---|
committer | Kevin Jin <kjin@google.com> | 2013-07-23 15:16:36 -0700 |
commit | df8ca0b5f5b6975d351a424db3bc1e8de88fe0fc (patch) | |
tree | 0a6f99db00aaedd670244fe9ab04ba53c15f22e5 /src/com | |
parent | 0b9344441daed36d371df59ca4735d1e0e008189 (diff) | |
download | droiddriver-df8ca0b5f5b6975d351a424db3bc1e8de88fe0fc.tar.gz |
break circular reference between UiElement and dom Element
add FileUtils.getAbsoluteFile
Change-Id: I278edbd60018ba230e966f5ce48cb582a98db44f
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/google/android/droiddriver/base/AbstractUiElement.java | 14 | ||||
-rw-r--r-- | src/com/google/android/droiddriver/util/FileUtils.java | 22 |
2 files changed, 27 insertions, 9 deletions
diff --git a/src/com/google/android/droiddriver/base/AbstractUiElement.java b/src/com/google/android/droiddriver/base/AbstractUiElement.java index 3b35844..9c26bb4 100644 --- a/src/com/google/android/droiddriver/base/AbstractUiElement.java +++ b/src/com/google/android/droiddriver/base/AbstractUiElement.java @@ -32,11 +32,13 @@ import com.google.common.base.Objects.ToStringHelper; import org.w3c.dom.Element; +import java.lang.ref.WeakReference; + /** * Abstract implementation with common methods already implemented. */ public abstract class AbstractUiElement implements UiElement { - private Element domNode; + private WeakReference<Element> domNode; @Override public <T> T get(Attribute attribute) { @@ -120,11 +122,15 @@ public abstract class AbstractUiElement implements UiElement { /** * Used internally in {@link ByXPath}. Returns the DOM node representing this * UiElement. The DOM is constructed from the UiElement tree. + * <p> + * TODO: move this to {@link ByXPath}. This requires a BiMap using + * WeakReference for both keys and values, which is error-prone. This will be + * deferred until we decide whether to clear cache upon getRootElement. */ public Element getDomNode() { - if (domNode == null) { - domNode = ByXPath.buildDomNode(this); + if (domNode == null || domNode.get() == null) { + domNode = new WeakReference<Element>(ByXPath.buildDomNode(this)); } - return domNode; + return domNode.get(); } } diff --git a/src/com/google/android/droiddriver/util/FileUtils.java b/src/com/google/android/droiddriver/util/FileUtils.java index a18d61a..5ffe681 100644 --- a/src/com/google/android/droiddriver/util/FileUtils.java +++ b/src/com/google/android/droiddriver/util/FileUtils.java @@ -34,11 +34,7 @@ public class FileUtils { * not exist, they will be created. The file will be readable to all. */ public static BufferedOutputStream open(String path) throws FileNotFoundException { - File file = new File(path); - if (!file.isAbsolute()) { - file = new File(System.getProperty("java.io.tmpdir"), path); - } - mkdirs(file.getParentFile()); + File file = getAbsoluteFile(path); Logs.log(Log.INFO, "opening file " + file.getAbsolutePath()); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file)); @@ -46,6 +42,22 @@ public class FileUtils { return stream; } + /** + * Returns a new file constructed using the absolute path of {@code path}. + * Unlike {@link File#getAbsoluteFile()}, default parent is "java.io.tmpdir" + * instead of "user.dir". + * <p> + * If any directories on {@code path} do not exist, they will be created. + */ + public static File getAbsoluteFile(String path) { + File file = new File(path); + if (!file.isAbsolute()) { + file = new File(System.getProperty("java.io.tmpdir"), path); + } + mkdirs(file.getParentFile()); + return file; + } + private static void mkdirs(File dir) { if (dir == null || dir.exists()) { return; |