Details
-
New Feature
-
Status: Open
-
Major
-
Resolution: Unresolved
-
1.0
-
None
-
None
Description
Janino is a pretty embedded compiler for run-time compilation purposes designed by Arno Unkrig under Apache License (<http://www.janino.net>).
It can be a very usefull tool to create java function, entry point (main), class and package on run time.
I would submit to jelly people a new jelly-tag library overview before getting in sandbox processus. This library implements janino compiler in jelly way.
Janino jelly-tag library implements compilation, class body and scriptlet like janino evaluator.
A - compilation
For exemple you can define classes like :
<compile var="janinoClassLoader">
<!-- java source code -->
public class Item {
private String name;
private double price;
public void setName(String str)
{ this.name = str; }public String getName()
{ return this.name; }
public void setPrice(double p) {
if ( p > 0 )
public double getPrice()
{return this.price; }}
</compile>
<j:new classLoader="${cl}" className="foo.test.Customer" var="customer"/>
<j:set target="${customer}" property="name" value="Charles"/>
B - ClassBody -
<classBody var="clazz">
import java.util.*;
static private int a = 1;
private int b = 2;
public int func(int c, int d)
{ return func2(c, d); }private static int func2(int e, int f)
{ return e * f; }
</classBody>
<j:useBean class="${clazz}" var="b"/>
<j:set var="result" value="${b.func(2,3)}"/>
C - scriptlet
<script var="script" result="s" returnType="java.util.ArrayList" execute="true">
<!-- define expected parameters -->
<parameter name="a" type="java.lang.String"/>
<parameter name="b" type="java.util.ArrayList"/>
<!-- pass argument reference -->
<j:arg value="foo"/>
<j:arg value="${l}"/>
<!-- java scrip itself -->
import java.util.ArrayList;
ArrayList list = new ArrayList();
list.add(a);
list.add(b);
return list;
</script>
<evaluate script="${script}" result="myList">
<j:arg value="item0"/>
<j:arg value="item1"/>
</evaluate>
<j:forEach var="item" items=="${mylist}">...
D - compilation support -
A light compilation log writer help you to get through compilation error
Exemple :
11 janv. 2005 18:21:51 org.apache.commons.jelly.janino.JaninoHelper throwDocumentedExcpetion
GRAVE: 0001:import java.util.fooArrayList;
0002:ArrayList list = new ArrayList();
---------------------^
Line 2, Column 17: Expression "ArrayList" is not a type
0003:list.add(a);
0004:list.add(b);
0005:return list;
E - Xml customizable bean definition -
A lot of jelly-tag library can do a lot for bean definition.
In janino tag library, high customizable run time bean definition is a included :
Exemples :
<compile var="cl" mapName="classes" packageName="foo.test">
<class name="Human">
<property name="name"/>
<property name="age" type="int"/>
</class>
<class name="Customer" extends="Human">
<property name="society"/>
</class>
</compile>
generate simple beans.
But you can go further :
<classBody var="Person">
<property name="name"/>
<property name="forname"/>
<property name="prefix"/>
<property name="sex" setter="true" declaration="true">
if ( string.equals("female") )
else
{ this.prefix = "Mr"; } </property>
<property name="sex" getter="true"/>
<property name="clothing" getter="true" override="true" declaration="true">
if ( this.prefix.equals("Ms") )
else
{ return "smoking"; }
</property>
</classBody>
<j:useBean class ="${Person}" var="jane" name="Doo" forname="Jane" sex="female"/>
<j:useBean class ="${Person}" var="john" name="Doo" forname="John" sex="male"/>
<j:file var="gossip">
${john.prefix} ${john.forname} ${john.name} dressed in ${john.clothing} and
${jane.prefix} ${jane.forname} ${jane.name} in ${jane.clothing} have enlighted this party !
</j:file>
F - of course you can use <compile> to load java class from a source file
<compile var="cl" uri="./mySources.txt"/>
As I have said, this library is for jelly people interest evaluation. It's rather an alpha library, but I think it can be a great feature for more supple jelly scripting.
Thank for your time.