ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jesse Glick <jgl...@cloudbees.com>
Subject Re: Request - Native Java "Script" Support
Date Thu, 27 Sep 2012 21:08:51 GMT
On 09/21/2012 12:06 AM, Stefan Bodewig wrote:
> Adding support for Java as "scripting language" wouldn't be incredibly difficult

There is already a ScriptEngine for *.java out there somewhere. Not sure if it is maintained.

> write a temporary file, compile it, add the resulting class to a classloader, load it

If you drop Java 5 support you can use JSR 199 and dispense with the temporary file.


The main difficulty with *.java in <script> is that your natural inclination is to just
compile

public class _ implements Callable<Void> {
   @Override public Void call() throws Exception {
     /* insert body of script here */
     return null;
   }
}

so that you can ((Callable<?>) loader.loadClass("_").newInstance()).call() to run it.
Unfortunately this makes it very awkward to define helper functions in the script 
before Java 8’s lambdas; the best you can do is something like

<script>
final AtomicReference<Integer> param1 = new AtomicReference<>();
Callable<String> function = new Callable<String>() {
   @Override public String call() throws Exception {
     return "The answer is " + param1.get() + ".";
   }
};
param1.set(42);
String result = function.call();
</script>

By contrast, Groovy makes it possible to freely mix function definitions and statements, while
otherwise looking like Java code, which is what you want here.


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


Mime
View raw message