Recycled Beans

10 PM August 30, 2006

Via email, from my dainty little sister, entitled “Read before lunch”:

A young cowboy walks into a seedy cafe in Prescott, AZ. He sits at the counter and notices an old cowboy with his arms folded staring blankly at a full bowl of chili.

After fifteen minutes of just sitting there staring at it, the young cowboy bravely asks the old cowpoke, “If you ain’t gonna eat that, mind if I do?”

The older cowboy slowly turns his head toward the young wrangler and in his best cowboy manner says, “Nah, you go ahead.”

Eagerly, the young cowboy reaches over and slides the bowl over to his place and starts spooning it in with delight. He gets nearly down to the bottom and notices a dead mouse in the chili.

Shocked and disgusted, he barfs the chili back into the bowl.

The old cowboy quietly says, “Yep, that’s as far as I got, too.”

By alang | # | Comments (0)
(Posted to Tall Tales)

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:

  1. -Alan
  2. you.
  3. love
  4. 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.)

Oh, and if you are a spammer, please disregard this entire blog entry.

By alang | # | Comments (2)
(Posted to Software Development and javablogs)

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.

Finally, I’d just like to note that Class.forName(“mypackage.MyClass”) initialises MyClass after it loads it, while the other Class.forName() variant has a parameter to control whether or not the newly loaded class should be initialised. None of the Classloader methods initialise a class.

By alang | # | Comments (2)
(Posted to javablogs, Software Development and Java)

Friday Cheer-up

3 AM August 11, 2006

Find Friday afternoons hard? Well cheer-up: Monday is only two days away! Hurrah!

PS: Here’s a five minute (mostly SFW) video that might help, too.

By alang | # | Comments (0)
(Posted to Tall Tales and Stuff)

Programmers Answer

10 PM August 1, 2006

I’ve never heard of Stiff, but his Stiff asks, great programmers answer blog post is well worth a read, with answers from Linus, Guido, DHH, Tim Bray, James Gosling, Steve Yegge, Dave Thomas and Peter Norvig. (via Tim Bray)

(PS: And now, I will stop writing blog posts inspired by Tim Bray. Three in a row is enough, for a while.)

By alang | # | Comments (0)
(Posted to Software Development and javablogs)
© 2003-2006 Alan Green