Skip to main content

Enhanced DTs with CGLIB

Posted by evanx on December 5, 2006 at 5:03 AM PST

src="" width="200" height="133"

src="" width="250" height="167"
vspace=8 hspace=16 align=left />

We try some magic tricks to avoid boilerplate EDT code. First we use dynamic proxies.
This makes the code is more difficult to navigate using our IDE. Nevertheless it is
potentially useful for general utility classes whose methods should all run in the
EDT, eg. to enable EDT-agnostic programming in background threads.

Finally, we investigate using byte code manipulation using CGLib.
We intercept invocations, inspect for annotations, and then
run the method in the background or in the EDT, according to the annotation.
This seems to be a better option than dynamic proxies, since we don't need
to introduce an interface, and navigation in our IDE is unaffected.

style="text-decoration: none;">

Code sample

We continue with our fantasy SupportAssistantGui application from the Boiler Room prequel.

src="" width="700" height="228"
vspace=0 hspace=0 />

public class SupportAssistantGui implements ActionListener {
    JButton checkForNewMessagesButton = new JButton("Check for new messages");
    protected void checkForNewMessages() {
        swingHelper.setEnabled(checkForNewMessagesButton, false);
        try {
            updateGuiWithNewMessages((CheckForNewMessagesResponse) response);
        } catch (IOException e) {
        } finally {
            swingHelper.setEnabled(checkForNewMessagesButton, true);
    protected void updateGuiWithNewMessages(CheckForNewMessagesResponse response) {

where the method annotations determine whether that method is invoked in the EDT using
invokeAndWait() or in a background SwingWorker thread, courtesy of CGLIB's


The following screenshot shows the method interceptor invocating methods in
succession, switching into a background thread for a long task (so as not to block to the EDT),
and then back into the EDT, thanks to the annotations on the
methods been invoked, ie. to update the GUI intermittently during the
series of long tasks.


(SupportAssistant, 200k/650k, unsandboxed, Java5)

src="" width="700" height="284"
vspace=0 hspace=0 />

where the first true or false indicates the EDT.

src="" width="700" height="235"

style="text-decoration: none;">

style="text-decoration: none;">

style='text-decoration: none;'>
Related Topics >>