<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Infernus &#187; Aspects</title>
	<atom:link href="http://infernus.org/tag/aspects/feed/" rel="self" type="application/rss+xml" />
	<link>http://infernus.org</link>
	<description>Don&#039;t feel you have to take any notice of me, please.</description>
	<lastBuildDate>Sat, 17 Sep 2011 19:57:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Checking the EDT with aspects</title>
		<link>http://infernus.org/2009/02/checking-the-edt-with-aspects/</link>
		<comments>http://infernus.org/2009/02/checking-the-edt-with-aspects/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 17:27:14 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Aspects]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://infernus.org/wordpress/?p=5</guid>
		<description><![CDATA[I am unreasonably fond of Swing. While it has plenty of foibles, and brings a new horror to UIs with Metal, it&#8217;s nevertheless quite a nice framework to use &#8211; once you&#8217;re familiar with it. The problem is that getting familiar is a path strewn with brambles and holes full on punji sticks. One of [...]]]></description>
			<content:encoded><![CDATA[<p>I am unreasonably fond of Swing. While it has plenty of foibles, and brings a new horror to UIs with Metal, it&#8217;s nevertheless quite a nice framework to use &#8211; once you&#8217;re familiar with it.</p>
<p>The problem is that getting familiar is a path strewn with brambles and holes full on punji sticks. One of the bigger holes is the event dispatch thread (EDT) &#8211; everything Swing related should take place on the EDT (even initialisation, under the latest Sun guidelines). When you&#8217;re trying to keep the UI fluid it&#8217;s all too easy to break the rule &#8211; hence, aspects to the rescue!</p>
<p>This topic has been covered by many before, including <a href="http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html">Alexander Potochkin</a> and <a href="http://thejavacodemonkey.blogspot.com/2007/08/using-aspectj-to-detect-violations-of.html">Anders Prisak</a> &#8211; however, I found their solution needed a little tweaking to be used in our environment. In particular, they had missed two cases we cover &#8211; SwingUtilities and SwingWorker.</p>
<p>So, here&#8217;s the tweaked aspect. safeMethods now includes a few extras.</p>
<pre>package org.infernus.swing.aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.awt.*;

@Aspect
public class EDTCheck {

    @Pointcut("call (* javax.swing..*+.*(..)) || "
            + "call (javax.swing..*+.new(..))")
    public void swingMethods() {
    }

    @Pointcut("call (* javax.swing..*+.add*Listener(..)) || "
            + "call (* javax.swing..*+.remove*Listener(..)) || "
            + "call (* javax.swing..*+.getListeners(..)) || "
            + "call (* javax.swing..*+.revalidate()) || "
            + "call (* javax.swing..*+.invalidate()) || "
            + "call (* javax.swing..*+.repaint()) || "
            + "target (javax.swing.SwingWorker+) || "
            + "call (* javax.swing.SwingUtilities+.invoke*(..)) || "
            + "call (* javax.swing.SwingUtilities+.isEventDispatchThread()) || "
            + "call (void javax.swing.JComponent+.setText(java.lang.String))")
    public void safeMethods() {
    }

    @Before("swingMethods() &amp;&amp; !safeMethods() &amp;&amp; !within(EDTCheck)")
    public void checkCallingThread(final JoinPoint.StaticPart thisJoinPointStatic) {
        if (!EventQueue.isDispatchThread()) {
            System.err.println("Swing EDT violation: " + thisJoinPointStatic.getSignature()
                    + " (" + thisJoinPointStatic.getSourceLocation() + ")");
            Thread.dumpStack();
        }
    }
}</pre>
<p>Once it&#8217;s built, we just need to weave it &#8211; I&#8217;ve already got compile-time weaving configured for <a href="http://infernus.org/node/269">Spring @Configurable support</a>, so just add the JAR containing the aspect as a weaveDependency and then the magic happens.</p>
<p>Now, if a Swing call is made off of the EDT, you&#8217;ll get complaints:</p>
<pre>Swing EDT violation: String javax.swing.JTextArea.getText() (YourSwingClass.java:98)
java.lang.Exception: Stack trace
        at java.lang.Thread.dumpStack(Thread.java:1224)
        at org.infernus.swing.aspects.EDTCheck.checkCallingThread(EDTCheck.java:55)
     ... and so on</pre>
]]></content:encoded>
			<wfw:commentRss>http://infernus.org/2009/02/checking-the-edt-with-aspects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: infernus.org @ 2012-02-10 02:15:44 -->
