Bug 53831 - calling antcall from <script> throws a java.lang.NullPointerException
calling antcall from <script> throws a java.lang.NullPointerException
Status: NEW
Product: Ant
Classification: Unclassified
Component: Optional Tasks
unspecified
All All
: P2 normal (vote)
: ---
Assigned To: Ant Notifications List
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2012-09-05 14:10 UTC by Antoine "hashar" Musso
Modified: 2012-09-05 14:14 UTC (History)
0 users



Attachments
working build file (629 bytes, text/xml)
2012-09-05 14:13 UTC, Antoine "hashar" Musso
Details
failing build file (358 bytes, text/xml)
2012-09-05 14:14 UTC, Antoine "hashar" Musso
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Antoine "hashar" Musso 2012-09-05 14:10:14 UTC
In the ant 1.8.2 build system, I am having a javascript that rely on an <antcall/>. That causes me a java.lang.NullPointerException which points to antcall.

<project default="main">
    <target name="main">
        <script language="javascript"> <![CDATA[
        task = project.createTask( 'macro' );
        task.execute();
        ]]></script>
    </target>

    <macrodef name="macro">
        <sequential>
            <antcall target="antcall" />
        </sequential>
    </macrodef>

    <target name="antcall">
        <echo>[antcall] succeed</echo>
    </target>

</project>


build.xml:11: java.lang.NullPointerException( #3 ... ). The stacktrace just talk about RhinoScriptEngine, not sure that is any helpful



at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:153)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:167)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.util.ReflectUtil.invoke(ReflectUtil.java:108)
at org.apache.tools.ant.util.ReflectWrapper.invoke(ReflectWrapper.java:81)
at org.apache.tools.ant.util.optional.JavaxScriptRunner.evaluateScript(JavaxScriptRunner.java:103)
at org.apache.tools.ant.util.optional.JavaxScriptRunner.executeScript(JavaxScriptRunner.java:67)
at org.apache.tools.ant.taskdefs.optional.Script.execute(Script.java:52)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Comment 1 Antoine "hashar" Musso 2012-09-05 14:10:43 UTC
I found a bug report in Groovy detailing a similar issue http://jira.codehaus.org/browse/GROOVY-1506 . The attached patch by Andreas Sahlbach fixed the groovy issue by assigning an owner to a newly created task.

So I have gone ahead and added:

task.setOwningTarget( self.getOwningTarget() );
That fixed the issue. I have put the original script and the fixed one in gist 3636007 so other people can play with.


Final script is below:

<project default="main">
    <target name="main">
        <script language="javascript"> <![CDATA[
        task = project.createTask( 'macro' );

        if( task.getOwningTarget() == null ) {
            task.log( "Assigning an owner ..." );
            task.setOwningTarget( self.getOwningTarget() );
            task.log( "Task:  " + task.getOwningTarget() );
        }

        try {
            task.execute();
        } catch(err) {
            task.log( "Execution error: " + err.message );
        }

        ]]></script>
    </target>

    <macrodef name="macro">
        <sequential>
            <antcall target="antcall" />
        </sequential>
    </macrodef>

    <target name="antcall">
        <echo>[antcall] succeed</echo>
    </target>

</project>
The resulting execution is:

$ ant
Buildfile: /Users/amusso/ant/bug/build.xml

main:
    [macro] Assigning an owner ...
    [macro] Task:  main

antcall:
     [echo] [antcall] succeed

BUILD SUCCESSFUL
Total time: 0 seconds
\O/
Comment 2 Antoine "hashar" Musso 2012-09-05 14:12:42 UTC
See also:
 http://stackoverflow.com/questions/12280295

Source code:
 https://gist.github.com/3636007

To clone the build script:
 git clone git://gist.github.com/3636007.git bug53831

 git checkout 9e686eb # failing build.xml
 git checkout 3bb9964 # working file
Comment 3 Antoine "hashar" Musso 2012-09-05 14:13:25 UTC
Created attachment 29334 [details]
working build file
Comment 4 Antoine "hashar" Musso 2012-09-05 14:14:07 UTC
Created attachment 29335 [details]
failing build file