PR: 18312 Submitted by: peter.reilly@corvil.com (peter reilly) git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274448 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -407,6 +407,7 @@ public class ComponentHelper { | |||||
| try { | try { | ||||
| Object o = c.newInstance(); | Object o = c.newInstance(); | ||||
| Project.setProjectOnObject(project, o); | |||||
| Task task = null; | Task task = null; | ||||
| if (o instanceof Task) { | if (o instanceof Task) { | ||||
| task = (Task) o; | task = (Task) o; | ||||
| @@ -415,9 +416,9 @@ public class ComponentHelper { | |||||
| // and an Adapter | // and an Adapter | ||||
| TaskAdapter taskA = new TaskAdapter(); | TaskAdapter taskA = new TaskAdapter(); | ||||
| taskA.setProxy(o); | taskA.setProxy(o); | ||||
| Project.setProjectOnObject(project, taskA); | |||||
| task = taskA; | task = taskA; | ||||
| } | } | ||||
| task.setProject(project); | |||||
| task.setTaskType(taskType); | task.setTaskType(taskType); | ||||
| // set default value, can be changed by the user | // set default value, can be changed by the user | ||||
| @@ -520,9 +521,7 @@ public class ComponentHelper { | |||||
| } else { | } else { | ||||
| o = ctor.newInstance(new Object[] {this}); | o = ctor.newInstance(new Object[] {this}); | ||||
| } | } | ||||
| if (o instanceof ProjectComponent) { | |||||
| ((ProjectComponent) o).setProject(project); | |||||
| } | |||||
| Project.setProjectOnObject(project, o); | |||||
| String msg = " +DataType: " + typeName; | String msg = " +DataType: " + typeName; | ||||
| project.log(msg, Project.MSG_DEBUG); | project.log(msg, Project.MSG_DEBUG); | ||||
| return o; | return o; | ||||
| @@ -538,9 +538,7 @@ public class IntrospectionHelper implements BuildListener { | |||||
| DynamicConfigurator dc = (DynamicConfigurator) parent; | DynamicConfigurator dc = (DynamicConfigurator) parent; | ||||
| Object nestedElement = dc.createDynamicElement(elementName); | Object nestedElement = dc.createDynamicElement(elementName); | ||||
| if (nestedElement != null) { | if (nestedElement != null) { | ||||
| if (nestedElement instanceof ProjectComponent) { | |||||
| ((ProjectComponent) nestedElement).setProject(project); | |||||
| } | |||||
| Project.setProjectOnObject(project, nestedElement); | |||||
| return nestedElement; | return nestedElement; | ||||
| } | } | ||||
| } | } | ||||
| @@ -549,9 +547,7 @@ public class IntrospectionHelper implements BuildListener { | |||||
| } | } | ||||
| try { | try { | ||||
| Object nestedElement = nc.create(parent); | Object nestedElement = nc.create(parent); | ||||
| if (nestedElement instanceof ProjectComponent) { | |||||
| ((ProjectComponent) nestedElement).setProject(project); | |||||
| } | |||||
| Project.setProjectOnObject(project, nestedElement); | |||||
| return nestedElement; | return nestedElement; | ||||
| } catch (IllegalAccessException ie) { | } catch (IllegalAccessException ie) { | ||||
| // impossible as getMethods should only return public methods | // impossible as getMethods should only return public methods | ||||
| @@ -842,9 +838,7 @@ public class IntrospectionHelper implements BuildListener { | |||||
| throws InvocationTargetException, IllegalAccessException, BuildException { | throws InvocationTargetException, IllegalAccessException, BuildException { | ||||
| try { | try { | ||||
| Object attribute = c.newInstance(new String[] {value}); | 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}); | m.invoke(parent, new Object[] {attribute}); | ||||
| } catch (InstantiationException ie) { | } catch (InstantiationException ie) { | ||||
| throw new BuildException(ie); | throw new BuildException(ie); | ||||
| @@ -649,6 +649,7 @@ public class Main { | |||||
| try { | try { | ||||
| BuildListener listener = | BuildListener listener = | ||||
| (BuildListener) Class.forName(className).newInstance(); | (BuildListener) Class.forName(className).newInstance(); | ||||
| Project.setProjectOnObject(project, listener); | |||||
| project.addBuildListener(listener); | project.addBuildListener(listener); | ||||
| } catch (Throwable exc) { | } catch (Throwable exc) { | ||||
| throw new BuildException("Unable to instantiate listener " | throw new BuildException("Unable to instantiate listener " | ||||
| @@ -671,6 +672,7 @@ public class Main { | |||||
| try { | try { | ||||
| handler = (InputHandler) | handler = (InputHandler) | ||||
| (Class.forName(inputHandlerClassname).newInstance()); | (Class.forName(inputHandlerClassname).newInstance()); | ||||
| Project.setProjectOnObject(project, handler); | |||||
| } catch (ClassCastException e) { | } catch (ClassCastException e) { | ||||
| String msg = "The specified input handler class " | String msg = "The specified input handler class " | ||||
| + inputHandlerClassname | + inputHandlerClassname | ||||
| @@ -58,6 +58,7 @@ import java.io.File; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.EOFException; | import java.io.EOFException; | ||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import java.lang.reflect.Method; | |||||
| import java.lang.reflect.Modifier; | import java.lang.reflect.Modifier; | ||||
| import java.util.Enumeration; | import java.util.Enumeration; | ||||
| import java.util.Hashtable; | import java.util.Hashtable; | ||||
| @@ -1133,6 +1134,7 @@ public class Project { | |||||
| try { | try { | ||||
| Object o = c.newInstance(); | Object o = c.newInstance(); | ||||
| setProjectOnObject(this, o); | |||||
| Task task = null; | Task task = null; | ||||
| if (o instanceof Task) { | if (o instanceof Task) { | ||||
| task = (Task) o; | task = (Task) o; | ||||
| @@ -1245,9 +1247,7 @@ public class Project { | |||||
| } else { | } else { | ||||
| o = ctor.newInstance(new Object[] {this}); | o = ctor.newInstance(new Object[] {this}); | ||||
| } | } | ||||
| if (o instanceof ProjectComponent) { | |||||
| ((ProjectComponent) o).setProject(this); | |||||
| } | |||||
| setProjectOnObject(this, o); | |||||
| String msg = " +DataType: " + typeName; | String msg = " +DataType: " + typeName; | ||||
| log (msg, MSG_DEBUG); | log (msg, MSG_DEBUG); | ||||
| return o; | return o; | ||||
| @@ -2305,4 +2305,33 @@ public class Project { | |||||
| return get(key) != null; | 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. | |||||
| } | |||||
| } | |||||
| } | } | ||||
| @@ -196,11 +196,7 @@ public final class ChainReaderHelper { | |||||
| final Reader[] rdr = {instream}; | final Reader[] rdr = {instream}; | ||||
| instream = | instream = | ||||
| (Reader) constructors[j].newInstance(rdr); | (Reader) constructors[j].newInstance(rdr); | ||||
| if (project != null && | |||||
| instream instanceof BaseFilterReader) { | |||||
| ((BaseFilterReader) | |||||
| instream).setProject(project); | |||||
| } | |||||
| setProjectOnObject(instream); | |||||
| if (Parameterizable.class.isAssignableFrom(clazz)) { | if (Parameterizable.class.isAssignableFrom(clazz)) { | ||||
| final Parameter[] params = filter.getParams(); | final Parameter[] params = filter.getParams(); | ||||
| ((Parameterizable) | ((Parameterizable) | ||||
| @@ -217,18 +213,31 @@ public final class ChainReaderHelper { | |||||
| throw new BuildException(ite); | 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); | instream = ((ChainableReader) o).chain(instream); | ||||
| setProjectOnObject(instream); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| return 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 | * Read data from the reader and return the | ||||
| * contents as a string. | * contents as a string. | ||||
| @@ -204,6 +204,7 @@ public class Mapper extends DataType implements Cloneable { | |||||
| } | } | ||||
| FileNameMapper m = (FileNameMapper) c.newInstance(); | FileNameMapper m = (FileNameMapper) c.newInstance(); | ||||
| Project.setProjectOnObject(getProject(), m); | |||||
| m.setFrom(from); | m.setFrom(from); | ||||
| m.setTo(to); | m.setTo(to); | ||||
| return m; | return m; | ||||
| @@ -56,6 +56,7 @@ package org.apache.tools.ant.types.selectors; | |||||
| import java.io.File; | import java.io.File; | ||||
| import java.util.Vector; | import java.util.Vector; | ||||
| import org.apache.tools.ant.Project; | |||||
| import org.apache.tools.ant.AntClassLoader; | import org.apache.tools.ant.AntClassLoader; | ||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| import org.apache.tools.ant.types.Parameter; | import org.apache.tools.ant.types.Parameter; | ||||
| @@ -106,6 +107,7 @@ public class ExtendSelector extends BaseSelector { | |||||
| AntClassLoader.initializeClass(c); | AntClassLoader.initializeClass(c); | ||||
| } | } | ||||
| dynselector = (FileSelector) c.newInstance(); | dynselector = (FileSelector) c.newInstance(); | ||||
| Project.setProjectOnObject(getProject(), dynselector); | |||||
| } | } | ||||
| catch (ClassNotFoundException cnfexcept) { | catch (ClassNotFoundException cnfexcept) { | ||||
| setError("Selector " + classname + | setError("Selector " + classname + | ||||