Archive

EJB 3.0 Annotations Cheat Sheet - 1.2

Update: The Annotations Cheat Sheet got it’s own page here

After a longer hibernation I found the time to update the EJB 3.0 Annotations Cheat Sheet. I hope I catched up with all the recent changes to the spec up to the proposed final draft. I’ll appreciate any error or suggestion that is reported to me.

Just download it from here as pdf or OpenOffice Document.

Preview of (old) cheat sheet.

Refactor-Safe passing of method names for callbacks

Reflection is a very helpful tool for decoupling and avoiding marker interfaces. A common pattern for registering callbacks at the obersever pattern makes use of passing the callback method name as a String to a Observable:

The Observable class uses reflection to extract the method “onEvent” at runtime and will invoke it to notify Observable that an event occurred. This approach shows several advantages over the use of marker interfaces like java.util.Observer. First, no marker interface is needed, which decouples the code and depending on the effort put into the observer, the callback method must not be defined as strictly. E.g. the observer could even handle a signature like “void onEvent()” and simple do not pass any argument to the method.

Drawback of this approach is that the method name is passed to the Observable as String which makes refactoring your code harder and leads to nasty runtime errors. There is not check if the methods signature matches the signature which Observable expects. These errors won’t occur until the Observable will try to make a callback to the Observer.

Inspired by EasyMock 2.0 I found a way to extract a method name at runtime without invoking the method. Create a proxy with the Cglib bytecode manipulation library which records the method called on the proxy. Later on, the method can be used to register an Observer. I made the following, experimental code with Proxytoys and Cglib.

All small test shows how it should work:

As you can see, this one is refactor-safe because the name of the method isn’t used anymore.

The full power of this with the oberserver pattern:

I must admit that there is another solution to the problem of callback interfaces using anonymous inner classes, too:

This implementation which abstains of making Bar implement any Observer interface, suffers from the famous inner-class-breaks-security-problem (see entry at c2 wiki). The reflection based solution above uses an inner class only the extract the method name.

More stuff to read:

[updated and fixed broken comment form]

I am a Light Cycle.

What Video Game Character Are You? I am a Light Cycle.I am a Light Cycle.

I drive fast, I turn fast, I do everything fast. I even breakfast. I tend to confuse people with my sudden changes of heart. Sometimes I even confuse myself, which tends to cause problems. What Video Game Character Are You?

5-Minuten Schokokuchen aus der Mikrowelle

Wer traut sich, das auzuprobieren?

5-Minuten Schokokuchen aus der Mikrowelle

Gott auch zum Sofortkauf

Gott auch zum Sofortkauf

Gott auch zum Sofortkauf,
originally uploaded by llucifer.

Feine Sache, dieses eBay. Jetzt gibt’s sogar Gott zum Sofortkauf, zumindest laut google… Und die wissen bekanntlich alles.