You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by scretti <sc...@yahoo.com> on 2007/05/02 19:37:41 UTC

new element suggestion - java rather than script

Hi,
I'm new to contributing on an open source project but have used ant for many
years so 
forgive me if this topic has been discussed previously.  I searched the dev
list but 
did not see anything quite like what I am about to suggest.  I have a
working task 
implemented as described in the Usage Examples below and could post it to a
contributor
right away if it gets blessed.  However, it could probably use a bit more
'real-world'
testing before it is ready for prime-time.  

The reason that I suggest it for the core is that I would need to either
include it 
in each project that it is used in or ensure that all developers install the
additional
task in their ant environment.  Not that either of these options are
terrible, but I've
also wished for this functionality for many years and only recently
implemented it.

Thanks in advance for your input.
..Shawn

Background :

Ant does not have "normal" programming language constructs such as
loops, conditions(it has them but they can be difficult), math, etc.

The ability exists to add on optional scripting languages, but that is not
part of a "default" ant install.  There is talk of incorporating a default
scripting
language in 1.7.1 which would be great, but this solution may make that
unnecessary.

There is also the ability to extend ant and create your own tasks.  That
works
fine, but overkill for simple things especially if the functionality already
exists in the java language(such as Math.XXX(...)).  Again, not part of a
default ant
install.  This is limiting in an environment where developers have freedom
to 
configure their environment as they wish.  It is much easier to require a
minimum
version of ant rather than a minimum version with the addition of X, Y, Z
specialized
tasks.


Suggestion:

The suggested task would be used to execute any java class method or even to
compile
and execute a java class defined in-line of the XML element on the fly. 
This would 
give the power of the full Java API to ant build files.  
Suggested element would be something like : 
    <jexecutor>

This suggestion leverages known features of the ant runtime : 
		Have access to jdk and compiler (should have a javac if ant is used to
compile java)
		Already running in Java VM
		Dynamic classloading
		Reflection

Usage examples :

To invoke the method, an instance of the class is first instantiated using
the default constructor.
This instance is passed to the invoke method of the Method class.  
If an instance of the class cannot be created, invoke is called passing null
for the instance 
and attempting to invoke the method as if it were static.
 	... 
	<taskdef name="jexecutor" classname="xxx.xxx.JavaExecutor"
classpath="${basedir}/classes" />
	
	Example 1 - calling static no-arg method of existing class -->

	    <jexecutor classname="java.lang.Math" methodname="random"
outputproperty="output1" />
	    <echo>Result of call : ${output1}</echo>


	Example 2 - calling paramed method where runtime type differs from method
signiture type.
	 		Note : signature is :
javax.swing.JOptionPane.showMessageDialog(Component, Object)
	 		Since we're passing in a String as the second argument, we want it
constructed
	 		and used as a string...hence runtype="string".  However, since the
method signiture
	 		is defined with a Component and Object, searching for it with reflection
using
	 		Component and String will not find it.  Therefore we also need
sigtype="java.lang.Object"
	 		so that the correct method can be found.

	<jexecutor classname="javax.swing.JOptionPane"
methodname="showMessageDialog" >
		<metharg value="" sigtype="java.awt.Component" />
		<metharg value="a messgage" sigtype="java.lang.Object" runtype="string" />
	</jexecutor>
	

	Example 3 - Defining class inline and executing a method 
	        Note : some characters may require the inline class definition
below to
	               be defined in a CDATA block but for this simple case it
works fine
	
	<jexecutor methodname="testMethod" outputproperty="testout" >
		public class TestClass
		{
			public String testMethod()
			{
				return "non-static Blah";
			}
		}
	</jexecutor>


-- 
View this message in context: http://www.nabble.com/new-element-suggestion---java-rather-than-script-tf3682054.html#a10290813
Sent from the Ant - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org