Metaprogramming in Ruby and Java

Metaprogramming is modifying your programming language to make it fit your problem domain. Lisp started it, Perl’s source filters did something along those lines, but Ruby’s got it in spades (caution: Why The Lucky Stiff content behind that last link). In the last few weeks I’ve been pounding through Ruby like Rush Limbaugh through an Everest of Viagra, and I wish I’d seen Glenn Vanderburg’s session (PDF!) at OSCON last year about metaprogramming in Ruby. If you’re a Java programmer, you should check out Howard Lewis Ship‘s session at OSCON on metaprogramming in Java.

Howard uses HiveMind’s inversion of control container (Howard’s the creator of HiveMind) and the Javassist bytecode library. Javassist is, frankly, terrifyingly powerful. I don’t know what to be more amazed by: that Javassist exists at all, or that it’s a subproject of the relentlessly Enterprisey (and therefore sober and reliable and not at all meant to be terrifying) JBoss. That’s like the Betty Ford Clinic funding research into more efficient methods of producing crystal meth. Javassist reminds me of the alt.folklore.computers warstories about programmers who know too much about their execution environment.

If metaprogramming is too much like performing open brain surgery on yourself, you might be more comfortable with Howard’s sane contribution to OSCON: Building Java Web Applications with Tapestry (Howard also created Tapesty). No meth and no autocortical cauterization required. Viagra optional.

tags: ,

Get the O’Reilly Programming Newsletter

Weekly insight from industry insiders. Plus exclusive content and offers.

  • I would love to see something like Javassist for C#.

    Metaprogramming is just another feature in Ruby/Rails that makes coding in C#/ASP.NET a little bit more frustrating.

  • love that rush limbaugh line.

    “keep up” the good work nat ;)


  • rektide

    Pat Allan:
    Uh, take your pick, RAIL or CECIL. I’m still not sure whether CECIL is allcaps or not.

    Neither allow you to edit the source code, ala Javaassist, but that is to be expected. There are a plethora of languages which all compile down to CLR. I’d like to see someone even try and infer “source code” out of a Boo generator.

    My biggest beef is Mel’s biggest beef. None of these tools allow you to modify your code when and where it counts: at runtime. They’re simple instrumentation tools. Compiled (v. interpretted) dynamic languages would be amazing, but the tooling is not there.

    Starting to wish I could make it to OSCON.

  • rektide, thanks for that – I didn’t find either of those in my googling yesterday.