Browse Source

Add clone task.

PR: 32631


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277320 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 20 years ago
parent
commit
6ca8466008
9 changed files with 351 additions and 58 deletions
  1. +2
    -0
      WHATSNEW
  2. +56
    -0
      docs/manual/CoreTasks/clone.html
  3. +1
    -0
      docs/manual/coretasklist.html
  4. +65
    -0
      src/etc/testcases/taskdefs/clone.xml
  5. +64
    -50
      src/main/org/apache/tools/ant/RuntimeConfigurable.java
  6. +6
    -8
      src/main/org/apache/tools/ant/UnknownElement.java
  7. +97
    -0
      src/main/org/apache/tools/ant/taskdefs/Clone.java
  8. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/defaults.properties
  9. +59
    -0
      src/testcases/org/apache/tools/ant/taskdefs/CloneTest.java

+ 2
- 0
WHATSNEW View File

@@ -112,6 +112,8 @@ Other changes:


* Added length task to get strings' and files' lengths. * Added length task to get strings' and files' lengths.


* Added clone task.

Changes from Ant 1.6.2 to current Ant 1.6 CVS version Changes from Ant 1.6.2 to current Ant 1.6 CVS version
===================================================== =====================================================




+ 56
- 0
docs/manual/CoreTasks/clone.html View File

@@ -0,0 +1,56 @@
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Clone Task</title>
</head>

<body>

<h2>Clone</h2>
<h3>Description</h3>
<p>Clone a project reference (presumably for augmentation).</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">cloneref</td>
<td valign="top">What to clone, given as a
<a href="../using.html#references">reference</a> to an
object with a publicly accessible clone() implementation.
</td>
<td valign="top" align="center">Yes</td>
</tr>
</table>
<p>
Assuming the clone operation is successful, the clone invocation supports
any attributes and nested elements supported by the cloned type
(the obvious exception is the &quot;cloneref&quot; attribute).
<b>Please note that modifications to cloned objects may yield
unpredictable results depending on the internals of the cloned class.</b>
</p>
<h3>Examples</h3>
<p>
Given a fileset <i>foo</i>:
<pre> &lt;clone id="foo.txt" cloneref=&quot;foo&quot;&gt;
&lt;filename name="**/*.txt" />
&lt;/clone&gt;
&lt;clone id="foo.nontxt" cloneref=&quot;foo&quot;&gt;
&lt;filename name="**/*.txt" negate="true" />
&lt;/clone&gt;
</pre>
Creates filesets <i>foo.txt</i> and <i>foo.nontxt</i>, which could be
put to such uses as filtering some files and not others when copying.
</p>
<hr>

<p align="center">Copyright &copy; 2005 The Apache Software Foundation. All rights
Reserved.</p>

</body>
</html>


+ 1
- 0
docs/manual/coretasklist.html View File

@@ -27,6 +27,7 @@
<a href="CoreTasks/pack.html">BZip2</a><br> <a href="CoreTasks/pack.html">BZip2</a><br>
<a href="CoreTasks/checksum.html">Checksum</a><br> <a href="CoreTasks/checksum.html">Checksum</a><br>
<a href="CoreTasks/chmod.html">Chmod</a><br> <a href="CoreTasks/chmod.html">Chmod</a><br>
<a href="CoreTasks/clone.html">Clone</a><br>
<a href="CoreTasks/concat.html">Concat</a><br> <a href="CoreTasks/concat.html">Concat</a><br>
<a href="CoreTasks/condition.html">Condition</a><br> <a href="CoreTasks/condition.html">Condition</a><br>
&nbsp;&nbsp;<a href="CoreTasks/conditions.html">Supported conditions</a><br> &nbsp;&nbsp;<a href="CoreTasks/conditions.html">Supported conditions</a><br>


+ 65
- 0
src/etc/testcases/taskdefs/clone.xml View File

@@ -0,0 +1,65 @@
<project name="clone">

<target name="testClone1">
<fileset id="doppel" file="${ant.file}" />
<clone id="ganger" cloneref="doppel">
<not>
<size value="0" when="less" />
</not>
</clone>
<pathconvert property="doppel" pathsep="" refid="doppel" />
<pathconvert property="ganger" pathsep="" refid="ganger" />
<fail>
<condition>
<not>
<equals arg1="${doppel}" arg2="${ganger}" />
</not>
</condition>
</fail>
</target>

<target name="testClone2">
<fileset id="doppel" file="${ant.file}" />
<clone id="ganger" cloneref="doppel">
<size value="0" when="less" />
</clone>
<pathconvert property="ganger" pathsep="" refid="ganger" />
<fail>
<condition>
<not>
<equals arg1="" arg2="${ganger}" />
</not>
</condition>
</fail>
</target>

<target name="testClone3">
<fileset id="doppel" file="${ant.file}" />
<basename file="${ant.file}" property="ant.base" />
<clone id="ganger" cloneref="doppel" excludes="${ant.base}" />
<pathconvert property="ganger" pathsep="" refid="ganger" />
<fail>
<condition>
<not>
<equals arg1="" arg2="${ganger}" />
</not>
</condition>
</fail>
</target>

<target name="testNoClone">
<typedef name="object" classname="java.lang.Object" />
<object id="foo" />
<clone id="bar" cloneref="foo" />
</target>

<target name="testNoAttr">
<clone id="none" />
</target>

<target name="testNoRef">
<clone id="none"
cloneref="thisreferencehasnotbeensetinthecurrentproject" />
</target>

</project>

+ 64
- 50
src/main/org/apache/tools/ant/RuntimeConfigurable.java View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2000-2004 The Apache Software Foundation
* Copyright 2000-2005 The Apache Software Foundation
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -89,11 +89,10 @@ public class RuntimeConfigurable implements Serializable {
* *
* @param proxy The element to configure. Must not be <code>null</code>. * @param proxy The element to configure. Must not be <code>null</code>.
* @param elementTag The tag name generating this element. * @param elementTag The tag name generating this element.
* Should not be <code>null</code>.
*/ */
public RuntimeConfigurable(Object proxy, String elementTag) { public RuntimeConfigurable(Object proxy, String elementTag) {
setProxy(proxy); setProxy(proxy);
this.elementTag = elementTag;
setElementTag(elementTag);
// Most likely an UnknownElement // Most likely an UnknownElement
if (proxy instanceof Task) { if (proxy instanceof Task) {
((Task) proxy).setRuntimeConfigurableWrapper(this); ((Task) proxy).setRuntimeConfigurableWrapper(this);
@@ -105,7 +104,7 @@ public class RuntimeConfigurable implements Serializable {
* *
* @param proxy The element to configure. Must not be <code>null</code>. * @param proxy The element to configure. Must not be <code>null</code>.
*/ */
public void setProxy(Object proxy) {
public synchronized void setProxy(Object proxy) {
wrappedObject = proxy; wrappedObject = proxy;
proxyConfigured = false; proxyConfigured = false;
} }
@@ -116,7 +115,7 @@ public class RuntimeConfigurable implements Serializable {
* *
* @param creator the creator object. * @param creator the creator object.
*/ */
void setCreator(IntrospectionHelper.Creator creator) {
synchronized void setCreator(IntrospectionHelper.Creator creator) {
this.creator = creator; this.creator = creator;
} }


@@ -126,7 +125,7 @@ public class RuntimeConfigurable implements Serializable {
* *
* @return the object whose configure is held by this instance. * @return the object whose configure is held by this instance.
*/ */
public Object getProxy() {
public synchronized Object getProxy() {
return wrappedObject; return wrappedObject;
} }


@@ -134,7 +133,7 @@ public class RuntimeConfigurable implements Serializable {
* Get the polymorphic type for this element. * Get the polymorphic type for this element.
* @return the ant component type name, null if not set. * @return the ant component type name, null if not set.
*/ */
public String getPolyType() {
public synchronized String getPolyType() {
return polyType; return polyType;
} }


@@ -142,7 +141,7 @@ public class RuntimeConfigurable implements Serializable {
* Set the polymorphic type for this element. * Set the polymorphic type for this element.
* @param polyType the ant component type name, null if not set. * @param polyType the ant component type name, null if not set.
*/ */
public void setPolyType(String polyType) {
public synchronized void setPolyType(String polyType) {
this.polyType = polyType; this.polyType = polyType;
} }


@@ -153,7 +152,7 @@ public class RuntimeConfigurable implements Serializable {
* @param attributes List of attributes defined in the XML for this * @param attributes List of attributes defined in the XML for this
* element. May be <code>null</code>. * element. May be <code>null</code>.
*/ */
public void setAttributes(AttributeList attributes) {
public synchronized void setAttributes(AttributeList attributes) {
this.attributes = new AttributeListImpl(attributes); this.attributes = new AttributeListImpl(attributes);
for (int i = 0; i < attributes.getLength(); i++) { for (int i = 0; i < attributes.getLength(); i++) {
setAttribute(attributes.getName(i), attributes.getValue(i)); setAttribute(attributes.getName(i), attributes.getValue(i));
@@ -166,7 +165,7 @@ public class RuntimeConfigurable implements Serializable {
* @param name the name of the attribute. * @param name the name of the attribute.
* @param value the attribute's value. * @param value the attribute's value.
*/ */
public void setAttribute(String name, String value) {
public synchronized void setAttribute(String name, String value) {
if (name.equalsIgnoreCase(ProjectHelper.ANT_TYPE)) { if (name.equalsIgnoreCase(ProjectHelper.ANT_TYPE)) {
this.polyType = value; this.polyType = value;
} else { } else {
@@ -179,13 +178,22 @@ public class RuntimeConfigurable implements Serializable {
} }
} }


/**
* Delete an attribute. Not for the faint of heart.
* @param name the name of the attribute to be removed.
*/
public synchronized void removeAttribute(String name) {
attributeNames.remove(name);
attributeMap.remove(name);
}

/** /**
* Return the attribute map. * Return the attribute map.
* *
* @return Attribute name to attribute value map. * @return Attribute name to attribute value map.
* @since Ant 1.6 * @since Ant 1.6
*/ */
public Hashtable getAttributeMap() {
public synchronized Hashtable getAttributeMap() {
return (attributeMap == null) return (attributeMap == null)
? EMPTY_HASHTABLE : new Hashtable(attributeMap); ? EMPTY_HASHTABLE : new Hashtable(attributeMap);
} }
@@ -197,7 +205,7 @@ public class RuntimeConfigurable implements Serializable {
* @return An AttributeList representing the attributes defined in the * @return An AttributeList representing the attributes defined in the
* XML for this element. May be <code>null</code>. * XML for this element. May be <code>null</code>.
*/ */
public AttributeList getAttributes() {
public synchronized AttributeList getAttributes() {
return attributes; return attributes;
} }


@@ -207,7 +215,7 @@ public class RuntimeConfigurable implements Serializable {
* @param child The child element wrapper to add to this one. * @param child The child element wrapper to add to this one.
* Must not be <code>null</code>. * Must not be <code>null</code>.
*/ */
public void addChild(RuntimeConfigurable child) {
public synchronized void addChild(RuntimeConfigurable child) {
children = (children == null) ? new ArrayList() : children; children = (children == null) ? new ArrayList() : children;
children.add(child); children.add(child);
} }
@@ -220,7 +228,7 @@ public class RuntimeConfigurable implements Serializable {
* @return The child wrapper at position <code>index</code> within the * @return The child wrapper at position <code>index</code> within the
* list. * list.
*/ */
RuntimeConfigurable getChild(int index) {
synchronized RuntimeConfigurable getChild(int index) {
return (RuntimeConfigurable) children.get(index); return (RuntimeConfigurable) children.get(index);
} }


@@ -229,7 +237,7 @@ public class RuntimeConfigurable implements Serializable {
* @return an enumeration of the child wrappers. * @return an enumeration of the child wrappers.
* @since Ant 1.6 * @since Ant 1.6
*/ */
public Enumeration getChildren() {
public synchronized Enumeration getChildren() {
return (children == null) ? new CollectionUtils.EmptyEnumeration() return (children == null) ? new CollectionUtils.EmptyEnumeration()
: Collections.enumeration(children); : Collections.enumeration(children);
} }
@@ -240,7 +248,10 @@ public class RuntimeConfigurable implements Serializable {
* @param data Text to add to the wrapped element. * @param data Text to add to the wrapped element.
* Should not be <code>null</code>. * Should not be <code>null</code>.
*/ */
public void addText(String data) {
public synchronized void addText(String data) {
if (data.length() == 0) {
return;
}
characters = (characters == null) characters = (characters == null)
? new StringBuffer(data) : characters.append(data); ? new StringBuffer(data) : characters.append(data);
} }
@@ -254,14 +265,12 @@ public class RuntimeConfigurable implements Serializable {
* @param count The number of characters to read from the array. * @param count The number of characters to read from the array.
* *
*/ */
public void addText(char[] buf, int start, int count) {
public synchronized void addText(char[] buf, int start, int count) {
if (count == 0) { if (count == 0) {
return; return;
} }
if (characters == null) {
characters = new StringBuffer(count);
}
characters.append(buf, start, count);
characters = ((characters == null)
? new StringBuffer(count) : characters).append(buf, start, count);
} }


/** /**
@@ -272,12 +281,16 @@ public class RuntimeConfigurable implements Serializable {
* @return the text content of this element. * @return the text content of this element.
* @since Ant 1.6 * @since Ant 1.6
*/ */
public StringBuffer getText() {
if (characters != null) {
return characters;
} else {
return new StringBuffer(0);
}
public synchronized StringBuffer getText() {
return (characters == null) ? new StringBuffer(0) : characters;
}

/**
* Set the element tag.
* @param elementTag The tag name generating this element.
*/
public synchronized void setElementTag(String elementTag) {
this.elementTag = elementTag;
} }


/** /**
@@ -286,7 +299,7 @@ public class RuntimeConfigurable implements Serializable {
* @return The tag name of the wrapped element. This is unlikely * @return The tag name of the wrapped element. This is unlikely
* to be <code>null</code>, but may be. * to be <code>null</code>, but may be.
*/ */
public String getElementTag() {
public synchronized String getElementTag() {
return elementTag; return elementTag;
} }


@@ -330,7 +343,7 @@ public class RuntimeConfigurable implements Serializable {
* to invalid attributes or children, or text being added to * to invalid attributes or children, or text being added to
* an element which doesn't accept it. * an element which doesn't accept it.
*/ */
public void maybeConfigure(Project p, boolean configureChildren)
public synchronized void maybeConfigure(Project p, boolean configureChildren)
throws BuildException { throws BuildException {
String id = null; String id = null;


@@ -384,27 +397,28 @@ public class RuntimeConfigurable implements Serializable {
Enumeration e = getChildren(); Enumeration e = getChildren();
while (e.hasMoreElements()) { while (e.hasMoreElements()) {
RuntimeConfigurable child = (RuntimeConfigurable) e.nextElement(); RuntimeConfigurable child = (RuntimeConfigurable) e.nextElement();
if (child.wrappedObject instanceof Task) {
Task childTask = (Task) child.wrappedObject;
childTask.setRuntimeConfigurableWrapper(child);
}

if ((child.creator != null) && configureChildren) {
child.maybeConfigure(p);
child.creator.store();
continue;
}
/*
* backwards compatibility - element names of nested
* elements have been all lower-case in Ant, except for
* tasks in TaskContainers.
*
* For TaskContainers, we simply skip configuration here.
*/
String tag = child.getElementTag().toLowerCase(Locale.US);
if (configureChildren && ih.supportsNestedElement(tag)) {
child.maybeConfigure(p);
ProjectHelper.storeChild(p, target, child.wrappedObject, tag);
synchronized (child) {
if (child.wrappedObject instanceof Task) {
Task childTask = (Task) child.wrappedObject;
childTask.setRuntimeConfigurableWrapper(child);
}
if ((child.creator != null) && configureChildren) {
child.maybeConfigure(p);
child.creator.store();
continue;
}
/*
* backwards compatibility - element names of nested
* elements have been all lower-case in Ant, except for
* tasks in TaskContainers.
*
* For TaskContainers, we simply skip configuration here.
*/
String tag = child.getElementTag().toLowerCase(Locale.US);
if (configureChildren && ih.supportsNestedElement(tag)) {
child.maybeConfigure(p);
ProjectHelper.storeChild(p, target, child.wrappedObject, tag);
}
} }
} }




+ 6
- 8
src/main/org/apache/tools/ant/UnknownElement.java View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2000-2004 The Apache Software Foundation
* Copyright 2000-2005 The Apache Software Foundation
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -401,7 +401,6 @@ public class UnknownElement extends Task {
if (o == null) { if (o == null) {
throw getNotFoundException("task or type", name); throw getNotFoundException("task or type", name);
} }

if (o instanceof PreSetDef.PreSetDefinition) { if (o instanceof PreSetDef.PreSetDefinition) {
PreSetDef.PreSetDefinition def = (PreSetDef.PreSetDefinition) o; PreSetDef.PreSetDefinition def = (PreSetDef.PreSetDefinition) o;
o = def.createObject(ue.getProject()); o = def.createObject(ue.getProject());
@@ -412,7 +411,9 @@ public class UnknownElement extends Task {
task.setTaskName(ue.getTaskName()); task.setTaskName(ue.getTaskName());
} }
} }

if (o instanceof UnknownElement) {
o = ((UnknownElement) o).makeObject((UnknownElement) o, w);
}
if (o instanceof Task) { if (o instanceof Task) {
Task task = (Task) o; Task task = (Task) o;
task.setOwningTarget(getOwningTarget()); task.setOwningTarget(getOwningTarget());
@@ -639,10 +640,7 @@ public class UnknownElement extends Task {
return true; return true;
} }


private boolean equalsString(String a, String b) {
if (a == null) {
return b == null;
}
return a.equals(b);
private static boolean equalsString(String a, String b) {
return (a == null) ? (a == b) : a.equals(b);
} }
} }

+ 97
- 0
src/main/org/apache/tools/ant/taskdefs/Clone.java View File

@@ -0,0 +1,97 @@
/*
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.tools.ant.taskdefs;

import java.lang.reflect.Method;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.UnknownElement;
import org.apache.tools.ant.RuntimeConfigurable;

/**
* Clone an Object from a reference.
* @since Ant 1.7
*/
public class Clone extends UnknownElement {
/** Task name. */
public static final String TASK_NAME = "clone";

/** Clone reference attribute ID. */
public static final String CLONE_REF = "cloneref";

private static final Class[] NO_ARGS = new Class[] {};

/**
* Create a new instance of the Clone task.
*/
public Clone() {
super(TASK_NAME);
}

/**
* Creates a named task or data type. If the real object is a task,
* it is configured up to the init() stage.
*
* @param ue The UnknownElement to create the real object for.
* Not used in this implementation.
* @param w The RuntimeConfigurable containing the configuration
* information to pass to the cloned Object.
*
* @return the task or data type represented by the given unknown element.
*/
protected Object makeObject(UnknownElement ue, RuntimeConfigurable w) {
String cloneref = (String) (w.getAttributeMap().get(CLONE_REF));
if (cloneref == null) {
throw new BuildException("cloneref attribute not set");
}
Object ob = getProject().getReference(cloneref);
if (ob == null) {
throw new BuildException(
"reference \"" + cloneref + "\" not found");
}
try {
log("Attempting to clone " + ob.toString() + " \""
+ cloneref + "\"", Project.MSG_VERBOSE);
Method m = ob.getClass().getMethod("clone", NO_ARGS);
try {
Object bo = m.invoke(ob, NO_ARGS);
if (bo == null) {
throw new BuildException(m.toString() + " returned null");
}
w.removeAttribute(CLONE_REF);
w.setProxy(bo);
w.setElementTag(null);
setRuntimeConfigurableWrapper(w);
if (bo instanceof Task) {
((Task) bo).setOwningTarget(getOwningTarget());
((Task) bo).init();
}
return bo;
} catch (Exception e) {
throw new BuildException(e);
}
} catch (NoSuchMethodException e) {
throw new BuildException(
"Unable to locate public clone method for object \""
+ cloneref + "\"");
}
}

}

+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/defaults.properties View File

@@ -82,6 +82,7 @@ macrodef=org.apache.tools.ant.taskdefs.MacroDef
nice=org.apache.tools.ant.taskdefs.Nice nice=org.apache.tools.ant.taskdefs.Nice
libraries=org.apache.tools.ant.taskdefs.repository.Libraries libraries=org.apache.tools.ant.taskdefs.repository.Libraries
length=org.apache.tools.ant.taskdefs.Length length=org.apache.tools.ant.taskdefs.Length
clone=org.apache.tools.ant.taskdefs.Clone


# optional tasks # optional tasks
image=org.apache.tools.ant.taskdefs.optional.image.Image image=org.apache.tools.ant.taskdefs.optional.image.Image


+ 59
- 0
src/testcases/org/apache/tools/ant/taskdefs/CloneTest.java View File

@@ -0,0 +1,59 @@
/*
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.BuildFileTest;

public class CloneTest extends BuildFileTest {

public CloneTest(String name) {
super(name);
}

public void setUp() {
configureProject("src/etc/testcases/taskdefs/clone.xml");
}

public void testClone1() {
executeTarget("testClone1");
}

public void testClone2() {
executeTarget("testClone2");
}

public void testClone3() {
executeTarget("testClone3");
}

public void testNoClone() {
expectBuildExceptionContaining("testNoClone",
"should fail because Object cannot be cloned", "public clone method");
}

public void testNoAttr() {
expectSpecificBuildException("testNoAttr",
"cloneref attribute not set", "cloneref attribute not set");
}

public void testNoRef() {
expectSpecificBuildException("testNoRef", "reference does not exist",
"reference \"thisreferencehasnotbeensetinthecurrentproject\" not found");
}

}

Loading…
Cancel
Save