Jatha

Bringing to Java some of the power of Lisp

Introduction

Lisp programmers have a very powerful macro system. When a Lisp programmer writes a macro it's not like a C programmer's macro; it's not just a simple textual substitution. No, when a Lisp programmer writes a macro, she has the full power of Lisp at her disposal.

Lisp macros are Lisp programs that write Lisp programs.

Jatha macros are Java programs that write Java programs.

Jatha is a preprocessor that runs arbitrary java code in order to generate the source code that the compiler sees. Using Jatha, I wrote a simple @PROP macro, that turns this:

class Foo {
    @PROP(String, name);
}

Into this:

class Foo {
    private String m_name;
    public String getName() { return m_name; }
    public void setName(String name) { m_name = name; }
}

Jatha "macros" are actually just regular java classes. The @PROP macro is merely a class named "PROP" that lives in the "macros" package. It has a method named expand() that can turn an argument array like {"String", "name"} into the source code for a property with get and set methods.

I have written other simple macros. The @ENUM_OBJ macro turns this one-liner:

@ENUM_OBJ(MyEnumeration, INT, "int", DOUBLE, "double")

Into this sophisticated object-based enumeration:

class MyEnumeration {
    public static final MyEnumeration INT = new MyEnumeration("int");
    public static final MyEnumeration DOUBLE = new MyEnumeration("double");

    public static int getNumElements() {
        return 2;
    }
    public static MyEnumeration getElement(int __index) {
        switch (__index) {
        case 0: return INT;
        case 1: return DOUBLE;
        default: return null;
        }
    }
    public static MyEnumeration getElement(String __name) {
        if (__name.equals("int")) return INT;
        if (__name.equals("double")) return DOUBLE;
        return null;
    }
    private MyEnumeration(String __description) {
        this.__description = __description;
    }
    private String __description;
    public String toString() {
        return __description;
    }
}

You can write macros like these too. Try it. You'll like it.

Download

jatha-1.1.tar.gz (75K)
jatha-1.1.zip (140K)

Documentation

You can browse the API documentation here.
Or you can look at the readme here.

License

Jatha is distributed under the terms of the GNU GPL. Please note that it is perfectly fine to use Jatha to develop commercial products. Only if you actually distribute Jatha itself will the GPL come into play.

Feedback

You can email me: kimbly -at- kimbly -dot- com.

Contributions

Thanks to Eric Anderson (Eric dot Anderson at keesler dot af dot mil) for the COLLECTION, MCATCH, and EXCEPTION_CLASS macros, and Ant support.

Related Work


Last updated September 25, 2003
Back to Kimberley's Code.
Back to Kimberley's Home Page.