Many words, strung together

3 AM
August 21, 2006

I received an unusual email today, and it’s given me an idea. The piece of email in question was, aside from a meaningless subject line, blank. Probably a programming or configuration error on behalf of the spammer, or maybe it was a virus that was quietly stripped by a vigilante MTA. Despite it’s suspect nature, the email had made it through both SpamAssassin’s and GMail’s filters.

That suggests a minor, but pleasant, misuse of email. First, you have to determine whether the recipient’s email reader sorts messages from earliest to latest or latest to earliest. Let’s say I’m sending to my wife – her reader (GMail) has the most recent message at the top.

Next, I open my mail client and send her four emails in quick succession:

-Alan

you.

love

I

The when my wife next checks here email, she’ll see a lovely message, without even having to click on it. I’m calling it, “Zero-click messaging”, and I’m posting it here before anyone patents it. Be free, idea. (Of course, if you invented it first, let me know, so I can give appropriate credit.)

Class Loading vs. Class Initialisation

2 AM
August 14, 2006

Did you know that it’s possible to reference a Java class without initialising it? I do. But I didn’t know it when I got into the office this morning.

There are several steps the JVM must go through before a program can use a class. These steps are detailed in section 12 of the Java Language Specification. In summary, before a class can be used it must be loaded, linked, and then initialised. I’d always been aware of these three steps, but I thought that the JVM would attempt all three steps as soon as the class was referred to.

It turns out that this is not the case. Section 12.4.1 lists the specific circumstances in which initialisation occurs. It is more than possible to have a class that is loaded and linked but not yet initialised, leaving several ways in which one can refer to an uninitialised class. For instance – and I’ve spent a good five or six hours tracking this one down – executing a statement such as ”Class c = MyClass.class;” won’t initialise MyClass.

Be wary of this behaviour when dynamically loading classes that are expected to register themselves with some registry, or create static final instances of themselves. My code uses commons-lang enums. Subclasses of Enum have static final instances which register themselves with the base class. The code also has a Hibernate UserType which converts the enum instances to and from strings, in a manner similar to this recipe. Everything works fine, unless the application tries to read from the database without having first executed particular – and seemingingly unrelated – pieces of functionality which just happen to cause the JVM to initialise the subclass.

For my code, the solution was to put an empty static method, named forceInit(), on each of the Enum subclasses. I then arranged for forceInit() to be called at the appropriate time from within the Hibernate User Type. Simple when you know what the problem is.