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();

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

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


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


Will generate the output:

package org.skife.example;

public class Wombat implements
     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