diff --git a/src/main/org/apache/tools/ant/ComponentHelper.java b/src/main/org/apache/tools/ant/ComponentHelper.java index 163c20465..56aeb41af 100644 --- a/src/main/org/apache/tools/ant/ComponentHelper.java +++ b/src/main/org/apache/tools/ant/ComponentHelper.java @@ -407,6 +407,7 @@ public class ComponentHelper { try { Object o = c.newInstance(); + Project.setProjectOnObject(project, o); Task task = null; if (o instanceof Task) { task = (Task) o; @@ -415,9 +416,9 @@ public class ComponentHelper { // and an Adapter TaskAdapter taskA = new TaskAdapter(); taskA.setProxy(o); + Project.setProjectOnObject(project, taskA); task = taskA; } - task.setProject(project); task.setTaskType(taskType); // set default value, can be changed by the user @@ -520,9 +521,7 @@ public class ComponentHelper { } else { o = ctor.newInstance(new Object[] {this}); } - if (o instanceof ProjectComponent) { - ((ProjectComponent) o).setProject(project); - } + Project.setProjectOnObject(project, o); String msg = " +DataType: " + typeName; project.log(msg, Project.MSG_DEBUG); return o; diff --git a/src/main/org/apache/tools/ant/IntrospectionHelper.java b/src/main/org/apache/tools/ant/IntrospectionHelper.java index 5cd5ab606..8782e2611 100644 --- a/src/main/org/apache/tools/ant/IntrospectionHelper.java +++ b/src/main/org/apache/tools/ant/IntrospectionHelper.java @@ -538,9 +538,7 @@ public class IntrospectionHelper implements BuildListener { DynamicConfigurator dc = (DynamicConfigurator) parent; Object nestedElement = dc.createDynamicElement(elementName); if (nestedElement != null) { - if (nestedElement instanceof ProjectComponent) { - ((ProjectComponent) nestedElement).setProject(project); - } + Project.setProjectOnObject(project, nestedElement); return nestedElement; } } @@ -549,9 +547,7 @@ public class IntrospectionHelper implements BuildListener { } try { Object nestedElement = nc.create(parent); - if (nestedElement instanceof ProjectComponent) { - ((ProjectComponent) nestedElement).setProject(project); - } + Project.setProjectOnObject(project, nestedElement); return nestedElement; } catch (IllegalAccessException ie) { // impossible as getMethods should only return public methods @@ -842,9 +838,7 @@ public class IntrospectionHelper implements BuildListener { throws InvocationTargetException, IllegalAccessException, BuildException { try { Object attribute = c.newInstance(new String[] {value}); - if (attribute instanceof ProjectComponent) { - ((ProjectComponent) attribute).setProject(p); - } + Project.setProjectOnObject(p, attribute); m.invoke(parent, new Object[] {attribute}); } catch (InstantiationException ie) { throw new BuildException(ie); diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index 02a9666a2..540243c7a 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -649,6 +649,7 @@ public class Main { try { BuildListener listener = (BuildListener) Class.forName(className).newInstance(); + Project.setProjectOnObject(project, listener); project.addBuildListener(listener); } catch (Throwable exc) { throw new BuildException("Unable to instantiate listener " @@ -671,6 +672,7 @@ public class Main { try { handler = (InputHandler) (Class.forName(inputHandlerClassname).newInstance()); + Project.setProjectOnObject(project, handler); } catch (ClassCastException e) { String msg = "The specified input handler class " + inputHandlerClassname diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index c9483afbc..38bf967b8 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -58,6 +58,7 @@ import java.io.File; import java.io.IOException; import java.io.EOFException; import java.io.InputStream; +import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Enumeration; import java.util.Hashtable; @@ -1133,6 +1134,7 @@ public class Project { try { Object o = c.newInstance(); + setProjectOnObject(this, o); Task task = null; if (o instanceof Task) { task = (Task) o; @@ -1245,9 +1247,7 @@ public class Project { } else { o = ctor.newInstance(new Object[] {this}); } - if (o instanceof ProjectComponent) { - ((ProjectComponent) o).setProject(this); - } + setProjectOnObject(this, o); String msg = " +DataType: " + typeName; log (msg, MSG_DEBUG); return o; @@ -2305,4 +2305,33 @@ public class Project { return get(key) != null; } } + + /** + * set the project on a created object using object.setProject(project). + * Need to set the project before other set/add elements + * are called + * @param project the project object + * @param obj the object to invoke setProject(project) on + */ + public static void setProjectOnObject(Project project, Object obj) { + if (project == null) + return; + if (obj instanceof ProjectComponent) { + ((ProjectComponent) obj).setProject(project); + return; + } + try { + Method method = + obj.getClass().getMethod( + "setProject", new Class[] {Project.class}); + if (method != null) { + method.invoke(obj, new Object[] {project}); + } + } catch (Throwable e) { + // ignore this if the object does not have + // a set project method or the method + // is private/protected. + } + } + } diff --git a/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java b/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java index b2d8802e7..c2f8ea6b5 100644 --- a/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java +++ b/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java @@ -196,11 +196,7 @@ public final class ChainReaderHelper { final Reader[] rdr = {instream}; instream = (Reader) constructors[j].newInstance(rdr); - if (project != null && - instream instanceof BaseFilterReader) { - ((BaseFilterReader) - instream).setProject(project); - } + setProjectOnObject(instream); if (Parameterizable.class.isAssignableFrom(clazz)) { final Parameter[] params = filter.getParams(); ((Parameterizable) @@ -217,18 +213,31 @@ public final class ChainReaderHelper { throw new BuildException(ite); } } - } else if (o instanceof ChainableReader && - o instanceof Reader) { - if (project != null && o instanceof BaseFilterReader) { - ((BaseFilterReader) o).setProject(project); - } + } else if (o instanceof ChainableReader) { + setProjectOnObject(o); instream = ((ChainableReader) o).chain(instream); + setProjectOnObject(instream); } } } return instream; } + /** + * helper method to set the project on an object. + * the reflection setProject does not work for anonymous/protected/private + * classes, even if they have public methods. + */ + private void setProjectOnObject(Object obj) { + if (project == null) + return; + if (obj instanceof BaseFilterReader) { + ((BaseFilterReader) obj).setProject(project); + return; + } + Project.setProjectOnObject(project, obj); + } + /** * Read data from the reader and return the * contents as a string. diff --git a/src/main/org/apache/tools/ant/types/Mapper.java b/src/main/org/apache/tools/ant/types/Mapper.java index 94b5804e6..3069b6680 100644 --- a/src/main/org/apache/tools/ant/types/Mapper.java +++ b/src/main/org/apache/tools/ant/types/Mapper.java @@ -204,6 +204,7 @@ public class Mapper extends DataType implements Cloneable { } FileNameMapper m = (FileNameMapper) c.newInstance(); + Project.setProjectOnObject(getProject(), m); m.setFrom(from); m.setTo(to); return m; diff --git a/src/main/org/apache/tools/ant/types/selectors/ExtendSelector.java b/src/main/org/apache/tools/ant/types/selectors/ExtendSelector.java index 4bf69d3dc..c2715b9a3 100644 --- a/src/main/org/apache/tools/ant/types/selectors/ExtendSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/ExtendSelector.java @@ -56,6 +56,7 @@ package org.apache.tools.ant.types.selectors; import java.io.File; import java.util.Vector; +import org.apache.tools.ant.Project; import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Parameter; @@ -106,6 +107,7 @@ public class ExtendSelector extends BaseSelector { AntClassLoader.initializeClass(c); } dynselector = (FileSelector) c.newInstance(); + Project.setProjectOnObject(getProject(), dynselector); } catch (ClassNotFoundException cnfexcept) { setError("Selector " + classname +