Thursday, August 8, 2013

Want to monitor your progress while rendering a BIRT report at a finer level then just page level and actually know how many elements you've done and how many are left?

First, record all the line numbers of every element in your report

            final TreeSet lineNos = new TreeSet();
            for (DesignElement de : rpd.getAllElements())
                lineNos.add(rpd.getLineNo(de));



Then you add a progress monitor to your task in the usual way, and at the start of each query you get the line number of the element being processed.


EventBus Annotation Processor

I was playing with EventBus to allow communication between classes. One thing that bothered me was that to use the Annotations system (eg,  @EventSubscriber(eventClass=StatusEvent.class) ), you have to have the annotation processed by calling:

org.bushe.swing.event.annotation.AnnotationProcessor.process(this)

Where this is the object that wants to register all it's annotations. However, you have to add a call to that in every class that will have that annotation.
Well, screw that. I got eclipse and I want it to be automatic.

Long story short, you gotta write your own annotation processor. Its not particularly complicated,but I'll just go and give you teh source and codez: Event Bus Annotation Processor
You plug it into Eclipse using the following instructions: Getting started with APT
Now, once that's working you'll get a file called eventbus.classes appearing in the .apt_generated directory.
That file contains a list of all classes that use the EventSubscriber annotation, but we still need a way to automatically get those classes to register with EventBus. You could play with classloading, but i chose Javassist.

Simply put, some code reads the eventbus.classes file, and uses Javassist to embed a call to register the instance at the end of every constructor in the class:

CtClass ctclass = ClassPool.getDefault().get(classname);
CtConstructor[] ctconstructor = ctclass.getConstructors();
for (CtConstructor c : ctconstructor)
{
 c.insertAfter("org.bushe.swing.event.annotation.AnnotationProcessor.process(this);");
}
ctclass.toClass(null, null);



And that is that. Everytime a class of that kind is instantiated, you'll get it auto registering to EventBus without you having to do it yourself.

All you need is a call to begin the reading of eventbus.classes.

Note: If you use some kind of serializing library that doesn't call constructors (eg, XStream), you'll need to register it yourself. You can either do that when it's loading, or if you are feeling adventurous you could extend the Javassist code above to include a call to the annotation processor in the readResolve() method XStream uses.