Browse Source

make tasks with add(Foo) method play nice with TaskContainer. PR 41647.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@707398 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
8f488f0872
4 changed files with 137 additions and 16 deletions
  1. +4
    -0
      WHATSNEW
  2. +77
    -15
      src/main/org/apache/tools/ant/IntrospectionHelper.java
  3. +2
    -1
      src/main/org/apache/tools/ant/UnknownElement.java
  4. +54
    -0
      src/tests/antunit/core/nested-elements-test.xml

+ 4
- 0
WHATSNEW View File

@@ -266,6 +266,10 @@ Fixed bugs:
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:
--------------



+ 77
- 15
src/main/org/apache/tools/ant/IntrospectionHelper.java View File

@@ -518,22 +518,15 @@ public final class IntrospectionHelper {
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) {
@@ -548,6 +541,27 @@ public final class IntrospectionHelper {
return nc;
}

/**
* 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
@@ -654,6 +668,12 @@ public final class IntrospectionHelper {
* 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 {@link #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
*
@@ -666,6 +686,48 @@ public final class IntrospectionHelper {
return supportsReflectElement(parentUri, elementName);
}

/**
* 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.
*


+ 2
- 1
src/main/org/apache/tools/ant/UnknownElement.java View File

@@ -542,7 +542,8 @@ public class UnknownElement extends Task {
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);


+ 54
- 0
src/tests/antunit/core/nested-elements-test.xml View File

@@ -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>

Loading…
Cancel
Save