Author: bodewig
Date: Thu Oct 23 08:47:35 2008
New Revision: 707398
URL: http://svn.apache.org/viewvc?rev=707398&view=rev
Log:
make tasks with add(Foo) method play nice with TaskContainer. PR 41647.
Added:
ant/core/trunk/src/tests/antunit/core/nested-elements-test.xml (with
props)
Modified:
ant/core/trunk/WHATSNEW
ant/core/trunk/src/main/org/apache/tools/ant/IntrospectionHelper.java
ant/core/trunk/src/main/org/apache/tools/ant/UnknownElement.java
Modified: ant/core/trunk/WHATSNEW
URL:
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=707398&r1=707397&r2=707398&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Thu Oct 23 08:47:35 2008
@@ -266,6 +266,10 @@
multiple modules have been specified.
Bugzilla Report 35301.
+ * Tasks with a "public void add(SomeType)" method failed to work as
+ TaskContainers at the same time.
+ Bugzilla Report 41647.
+
Other changes:
--------------
Modified: ant/core/trunk/src/main/org/apache/tools/ant/IntrospectionHelper.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/IntrospectionHelper.java?rev=707398&r1=707397&r2=707398&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/IntrospectionHelper.java
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/IntrospectionHelper.java Thu
Oct 23 08:47:35 2008
@@ -518,22 +518,15 @@
if (nc == null) {
nc = createAddTypeCreator(project, parent, elementName);
}
- if (nc == null && parent instanceof DynamicElementNS) {
- DynamicElementNS dc = (DynamicElementNS) parent;
+ if (nc == null &&
+ (parent instanceof DynamicElementNS
+ || parent instanceof DynamicElement)
+ ) {
String qName = child == null ? name : child.getQName();
- final Object nestedElement = dc.createDynamicElement(
- child == null ? "" : child.getNamespace(), name, qName);
- if (nestedElement != null) {
- nc = new NestedCreator(null) {
- Object create(Project project, Object parent, Object
ignore) {
- return nestedElement;
- }
- };
- }
- }
- if (nc == null && parent instanceof DynamicElement) {
- DynamicElement dc = (DynamicElement) parent;
- final Object nestedElement =
dc.createDynamicElement(name.toLowerCase(Locale.US));
+ final Object nestedElement =
+ createDynamicElement(parent,
+ child == null ? "" : child.getNamespace(),
+ name, qName);
if (nestedElement != null) {
nc = new NestedCreator(null) {
Object create(Project project, Object parent, Object
ignore) {
@@ -549,6 +542,27 @@
}
/**
+ * Invokes the "correct" createDynamicElement method on parent in
+ * order to obtain a child element by name.
+ *
+ * @since Ant 1.8.0.
+ */
+ private Object createDynamicElement(Object parent, String ns,
+ String localName, String qName) {
+ Object nestedElement = null;
+ if (parent instanceof DynamicElementNS) {
+ DynamicElementNS dc = (DynamicElementNS) parent;
+ nestedElement = dc.createDynamicElement(ns, localName, qName);
+ }
+ if (nestedElement == null && parent instanceof DynamicElement) {
+ DynamicElement dc = (DynamicElement) parent;
+ nestedElement =
+ dc.createDynamicElement(localName.toLowerCase(Locale.US));
+ }
+ return nestedElement;
+ }
+
+ /**
* Creates a named nested element. Depending on the results of the
* initial introspection, either a method in the given parent instance
* or a simple no-arg constructor is used to create an instance of the
@@ -654,6 +668,12 @@
* Indicate if this element supports a nested element of the
* given name.
*
+ * <p>Note that this method will always return true if the
+ * introspected class is [EMAIL PROTECTED] #isDynamic dynamic} or contains
a
+ * method named "add" with void return type and a single argument.
+ * To ge a more thorough answer, use the four-arg version of this
+ * method instead.</p>
+ *
* @param parentUri the uri of the parent
* @param elementName the name of the nested element being checked
*
@@ -667,6 +687,48 @@
}
/**
+ * Indicate if this element supports a nested element of the
+ * given name.
+ *
+ * @param parentUri the uri of the parent
+ * @param elementName the name of the nested element being checked
+ * @param project currently executing project instance
+ * @param parent the parent element (an instance of the introspected
class)
+ * @param childQName QName of the child element, may be null
+ *
+ * @return true if the given nested element is supported
+ * @since Ant 1.8.0.
+ */
+ public boolean supportsNestedElement(String parentUri, String elementName,
+ Project project, Object parent,
+ String childQName) {
+ if (addTypeMethods.size() > 0
+ && createAddTypeCreator(project, parent, elementName) != null) {
+ return true;
+ }
+ if (isDynamic()) {
+ /*
+ breaks several tests, in particular EchoXML because it
+ creates additional empty child elements in XMLFragment
+
+ String localName =
+ ProjectHelper.extractNameFromComponentName(elementName);
+ String uri =
ProjectHelper.extractUriFromComponentName(elementName);
+ if (uri.equals(ProjectHelper.ANT_CORE_URI)) {
+ uri = "";
+ }
+ if (createDynamicElement(parent, uri, localName,
+ childQName == null ? localName :
childQName)
+ != null) {
+ return true;
+ }
+ */
+ return true;
+ }
+ return supportsReflectElement(parentUri, elementName);
+ }
+
+ /**
* Check if this element supports a nested element from reflection.
*
* @param parentUri the uri of the parent
Modified: ant/core/trunk/src/main/org/apache/tools/ant/UnknownElement.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/UnknownElement.java?rev=707398&r1=707397&r2=707398&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/UnknownElement.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/UnknownElement.java Thu Oct 23
08:47:35 2008
@@ -542,7 +542,8 @@
RuntimeConfigurable childWrapper) {
String childName = ProjectHelper.genComponentName(
child.getNamespace(), child.getTag());
- if (ih.supportsNestedElement(parentUri, childName)) {
+ if (ih.supportsNestedElement(parentUri, childName,
+ getProject(), parent, child.getQName())) {
IntrospectionHelper.Creator creator =
ih.getElementCreator(
getProject(), parentUri, parent, childName, child);
Added: ant/core/trunk/src/tests/antunit/core/nested-elements-test.xml
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/core/nested-elements-test.xml?rev=707398&view=auto
==============================================================================
--- ant/core/trunk/src/tests/antunit/core/nested-elements-test.xml (added)
+++ ant/core/trunk/src/tests/antunit/core/nested-elements-test.xml Thu Oct 23
08:47:35 2008
@@ -0,0 +1,54 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You 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.
+-->
+<project xmlns:au="antlib:org.apache.ant.antunit" default="antunit">
+
+ <import file="../antunit-base.xml"/>
+
+ <target name="testConditionBaseAndTasContainer">
+ <mkdir dir="${input}"/>
+ <mkdir dir="${output}"/>
+ <echo file="${input}/ConditionRun.java">
+import org.apache.tools.ant.*;
+import org.apache.tools.ant.taskdefs.condition.*;
+import java.util.*;
+
+public class ConditionRun extends ConditionBase implements TaskContainer {
+ private List tasks = new ArrayList();
+
+ public void addTask(Task task) {
+ tasks.add(task);
+ }
+
+ public void execute() {
+ for (Iterator iter = tasks.iterator(); iter.hasNext(); ) {
+ Task t = (Task) iter.next();
+ t.perform();
+ }
+ }
+}
+ </echo>
+ <javac destdir="${output}"
+ srcdir="${input}"/>
+ <taskdef name="conditionrun" classpath="${output}"
+ classname="ConditionRun"/>
+ <conditionrun>
+ <echo>Hello</echo>
+ </conditionrun>
+ <au:assertLogContains text="Hello"/>
+ </target>
+</project>
Propchange: ant/core/trunk/src/tests/antunit/core/nested-elements-test.xml
------------------------------------------------------------------------------
svn:eol-style = native