From 31e1bc0694eb6804e681cc7add03a960eb14a544 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 17 Nov 2008 09:39:23 +0000 Subject: [PATCH] use adapter for Touchable instead of instanceof checks git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@718210 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Touch.java | 5 +- .../ant/types/resources/MappedResource.java | 66 ++++--------------- .../apache/tools/ant/util/ResourceUtils.java | 7 +- 3 files changed, 19 insertions(+), 59 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Touch.java b/src/main/org/apache/tools/ant/taskdefs/Touch.java index 852ad11e3..8c965979b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Touch.java +++ b/src/main/org/apache/tools/ant/taskdefs/Touch.java @@ -299,7 +299,8 @@ public class Touch extends Task { Iterator iter = resources.iterator(); while (iter.hasNext()) { Resource r = (Resource) iter.next(); - if (!(r instanceof Touchable)) { + Touchable t = (Touchable) r.as(Touchable.class); + if (t == null) { throw new BuildException("Can't touch " + r); } touch(r, defaultTimestamp); @@ -343,7 +344,7 @@ public class Touch extends Task { // use this to create file and deal with non-writable files touch(((FileProvider) r).getFile(), defaultTimestamp); } else { - ((Touchable) r).touch(defaultTimestamp); + ((Touchable) r.as(Touchable.class)).touch(defaultTimestamp); } } else { String[] mapped = fileNameMapper.mapFileName(r.getName()); diff --git a/src/main/org/apache/tools/ant/types/resources/MappedResource.java b/src/main/org/apache/tools/ant/types/resources/MappedResource.java index e1aaf0074..c480e2661 100644 --- a/src/main/org/apache/tools/ant/types/resources/MappedResource.java +++ b/src/main/org/apache/tools/ant/types/resources/MappedResource.java @@ -38,6 +38,7 @@ import org.apache.tools.ant.types.Resource; public class MappedResource extends Resource { private final Resource wrapped; private final boolean isAppendable; + private final boolean isTouchable; // should only be instantiated via factory, this also means we // don't have to think about being a reference to a different @@ -49,6 +50,7 @@ public class MappedResource extends Resource { protected MappedResource(Resource r) { wrapped = r; isAppendable = wrapped.as(Appendable.class) != null; + isTouchable = wrapped.as(Touchable.class) != null; } /** @@ -162,21 +164,19 @@ public class MappedResource extends Resource { } }; } + if (clazz == Touchable.class && isTouchable) { + return new Touchable() { + public void touch(long modTime) { + ((Touchable) wrapped.as(Touchable.class)).touch(modTime); + } + }; + } return super.as(clazz); } public static MappedResource map(Resource r) { - if (r instanceof FileProvider) { - if (r instanceof Touchable) { - return new TouchableFileProviderMR(r); - } - return new FileProviderMR(r); - } - if (r instanceof Touchable) { - return new TouchableMR(r); - } - // no special interface - return new MappedResource(r); + return r instanceof FileProvider + ? new FileProviderMR(r) : new MappedResource(r); } private static class FileProviderMR extends MappedResource @@ -201,48 +201,4 @@ public class MappedResource extends Resource { } } - private static class TouchableMR extends MappedResource - implements Touchable { - private final Touchable t; - - protected TouchableMR(Resource r) { - super(r); - if (!(r instanceof Touchable)) { - throw new IllegalArgumentException("trying to wrap something " - + "that is not a " - + " Touchable"); - } - t = (Touchable) r; - } - - /** - * delegated to the wrapped resource. - */ - public void touch(long m) { - t.touch(m); - } - } - - private static class TouchableFileProviderMR extends FileProviderMR - implements Touchable { - private final Touchable t; - - protected TouchableFileProviderMR(Resource r) { - super(r); - if (!(r instanceof Touchable)) { - throw new IllegalArgumentException("trying to wrap something " - + "that is not a " - + " Touchable"); - } - t = (Touchable) r; - } - - /** - * delegated to the wrapped resource. - */ - public void touch(long m) { - t.touch(m); - } - } - } \ No newline at end of file diff --git a/src/main/org/apache/tools/ant/util/ResourceUtils.java b/src/main/org/apache/tools/ant/util/ResourceUtils.java index dd23fe37b..78aedc110 100644 --- a/src/main/org/apache/tools/ant/util/ResourceUtils.java +++ b/src/main/org/apache/tools/ant/util/ResourceUtils.java @@ -421,8 +421,11 @@ public class ResourceUtils { FileUtils.close(in); } } - if (preserveLastModified && dest instanceof Touchable) { - setLastModified((Touchable) dest, source.getLastModified()); + if (preserveLastModified) { + Touchable t = (Touchable) dest.as(Touchable.class); + if (t != null) { + setLastModified(t, source.getLastModified()); + } } } // CheckStyle:ParameterNumberCheck ON