Brian's Waste of Time

Fri, 11 Feb 2005

Code Monkey

I'm happy to share CodeMonkey.

CodeMonkey is a programmatic code generation tool. It is not a template based code generator like XDoclet or its ilk, it is entirely programmatic, for instance:

    public void testSample() throws Exception
    {
        Klass wombat = new Klass();
        wombat.setName("Wombat");
        wombat.setPackage("org.skife.example");

        Method belch = new Method();
        belch.setFinal(true);
        belch.setName("belch");
        belch.addParameter(new Parameter("int", "volume"));
        belch.setBody("System.out.print(\"BURP at level \" + volume);");

        wombat.addMethod(belch);
        BeanTool.addProperty(wombat, String.class, "name");
        BeanTool.addProperty(wombat, "int", "age");

        wombat.addInterface(Serializable.class);

        Generator generator = new Generator();
        String source = generator.generate(wombat);

        System.out.println(source);
    }

Will generate the output:

package org.skife.example;

public class Wombat implements java.io.Serializable
{
     private java.lang.String name0;
     private int age1;

    public void belch(int volume)
    {
        System.out.print("BURP at level " + volume);
    }

    public final void setName(java.lang.String name)
    {
        name0 = name;
    }

    public java.lang.String getName()
    {
        return name0;
    }

    public void setAge(int age)
    {
        age1 = age;
    }

    public int getAge()
    {
        return age1;
    }

 }

The library just has low level constructs right now (java language features), but more is sure to come, as I built it to solve other problems ;-)

CodeMonkey is *not* a replacement for XDoclet, Middlegen, etc. It *is* a useful tool for building semi-dynamic code (ie, from JDBC metadata or whatnot) at compile time.

Have fun!

0 writebacks [/src/java] permanent link