From 9bc78e6c258fbc9ff142f959d67730b990f4834f Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Sat, 20 Jan 2001 11:10:25 +0000 Subject: [PATCH] Change to IntrospectionHelper to support creation of nested elements through a createElement(String elementName) factory method. This has two applications. It allows a task to define what nested elements are supported dynamically, perhaps through a configuration file. This is useful for tasks that support a plug-in architecture, as I am trying to achieve with As a byproduct, it also allows a task to support nested elements which cannot be mapped to valid Java method names, such as . The use of hyphenated compound names is relatively common style in XML DTDs git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268485 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/IntrospectionHelper.java | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/main/org/apache/tools/ant/IntrospectionHelper.java b/src/main/org/apache/tools/ant/IntrospectionHelper.java index e358e37e3..862e82531 100644 --- a/src/main/org/apache/tools/ant/IntrospectionHelper.java +++ b/src/main/org/apache/tools/ant/IntrospectionHelper.java @@ -95,6 +95,13 @@ public class IntrospectionHelper { */ private Method addText = null; + /** + * The method used to add nested elements which can't be added through + * nested creators. It allows a task to define a factory method for creating + * nested elements. + */ + private Method elementFactoryMethod = null; + /** * The Class that's been introspected. */ @@ -138,6 +145,14 @@ public class IntrospectionHelper { && java.lang.String.class.equals(args[0])) { addText = methods[i]; + + } else if ("createElement".equals(name) + && !returnType.isArray() + && !returnType.isPrimitive() + && args.length == 1 + && java.lang.String.class.equals(args[0])) { + + elementFactoryMethod = methods[i]; } else if (name.startsWith("set") && java.lang.Void.TYPE.equals(returnType) @@ -266,13 +281,23 @@ public class IntrospectionHelper { public Object createElement(Object element, String elementName) throws BuildException { NestedCreator nc = (NestedCreator) nestedCreators.get(elementName); - if (nc == null) { - String msg = "Class " + element.getClass().getName() + - " doesn't support the nested \"" + elementName + "\" element"; - throw new BuildException(msg); - } try { - return nc.create(element); + if (nc == null) { + Object nestedElement = null; + if (elementFactoryMethod != null) { + nestedElement + = elementFactoryMethod.invoke(element, new Object[] {elementName}); + } + if (nestedElement == null) { + String msg = "Class " + element.getClass().getName() + + " doesn't support the nested \"" + elementName + "\" element"; + throw new BuildException(msg); + } + return nestedElement; + } + else { + return nc.create(element); + } } catch (IllegalAccessException ie) { // impossible as getMethods should only return public methods throw new BuildException(ie);