<?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/category/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>Wed, 28 Jul 2010 18:35:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.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>
		<item>
		<title>Weaving Maven</title>
		<link>http://infernus.org/2008/09/weaving-maven/</link>
		<comments>http://infernus.org/2008/09/weaving-maven/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 17:23:01 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Aspects]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://infernus.org/wordpress/?p=12</guid>
		<description><![CDATA[We&#8217;re trying to clean up our services at present, and as such are keen on investigating the @Configurable annotation for Spring 2.5. For those who aren&#8217;t in the know, this uses aspects to allow Spring to configure new instances of an annotated class, allowing dependencies to magically appear in your new object, no factories required. [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re trying to clean up our services at present, and as such are keen on investigating the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-using-aspectj">@Configurable</a> annotation for Spring 2.5. For those who aren&#8217;t in the know, this uses aspects to allow Spring to configure new instances of an annotated class, allowing dependencies to magically appear in your new object, no factories required.</p>
<p>Black magic? Perhaps. Time will tell. But first we had to get it set up anyway.</p>
<p>In theory, this is simple. You have two choices: runtime or compile-time weaving. Compile-time means changing your build scripts. Runtime means either using a compliant classloader (WLS, for instance) or changing your JVM parameters (nasty for deployment). Given our lack of desire for deployment pain compile-time seemed the obvious choice.</p>
<p>As always, there was <a href="http://mojo.codehaus.org/aspectj-maven-plugin/">a Maven plug-in</a>. As always, it didn&#8217;t work. At least not with Spring 2.5.4. It turns out that Spring 2.5.4 broke compatibility with AspectJ 1.5.4. Worse, Spring&#8217;s POMs were broken and it still depended on this incompatible version. So, task one: <strong>upgrade to Spring 2.5.5</strong>.</p>
<p>Secondly, the Maven plug-in still broke. So, check it out, change the version number and the AspectJ version, compile &amp; upload to our internal repository. <strong>Task two: hack the Maven plug-in. </strong>I have a growing suspicion that this is a standard part of the Maven workflow.</p>
<p>Now, the easy bit &#8211; <strong>configure your POM</strong>. Except the examples need a bit of tweaking, not least because Java 5 is now old hat. Try the following:</p>
<pre>&lt;dependency&gt;
    &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
    &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
    &lt;version&gt;1.6.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework&lt;/groupId&gt;
    &lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
    &lt;version&gt;2.5.5&lt;/version&gt;
&lt;/dependency&gt;</pre>
<p>And:</p>
<pre>&lt;plugin&gt;
    &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
    &lt;artifactId&gt;aspectj-maven-plugin&lt;/artifactId&gt;
    &lt;version&gt;1.0-st&lt;/version&gt;
    &lt;executions&gt;
        &lt;execution&gt;
            &lt;goals&gt;
                &lt;goal&gt;compile&lt;/goal&gt;
                &lt;goal&gt;test-compile&lt;/goal&gt;
            &lt;/goals&gt;
        &lt;/execution&gt;
    &lt;/executions&gt;
    &lt;configuration&gt;
        &lt;source&gt;1.5&lt;/source&gt;
        &lt;target&gt;1.5&lt;/target&gt;

        &lt;showWeaveInfo&gt;true&lt;/showWeaveInfo&gt;
        &lt;verbose&gt;true&lt;/verbose&gt;
        &lt;proceedOnError&gt;false&lt;/proceedOnError&gt;

        &lt;weaveDependencies&gt;
            &lt;weaveDependency&gt;
                &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                &lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
            &lt;/weaveDependency&gt;
        &lt;/weaveDependencies&gt;

        &lt;aspectLibrarys&gt;
            &lt;aspectLibrary&gt;
                &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                &lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
            &lt;/aspectLibrary&gt;
        &lt;/aspectLibrarys&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;</pre>
<p>Change the plug-in version number as required, of course.</p>
<p>Finally, step four: <strong>pimp your application</strong>. You&#8217;ll need the following in your Spring configuration:</p>
<pre>&lt;context:spring-configured/&gt;</pre>
<p>Right, after all that you can start the @Configurable love. Create your bean:</p>
<pre>package com.signtechno.example;

@Configurable
public class DummyBean {
    private int value;

    public void setValue(final int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}</pre>
<p>Create a Spring prototype:</p>
<pre>&lt;bean class=“com.signtechno.example.Dummy” scope=“prototype”&gt;
    &lt;property name=“value” value=“17”/&gt;
&lt;/bean&gt;</pre>
<p>Create your logic:</p>
<pre>final DummyBean dummyBean = new DummyBean();</pre>
<p>And voila, dummyBean.getValue() == 17. Wasn&#8217;t that easy?</p>
]]></content:encoded>
			<wfw:commentRss>http://infernus.org/2008/09/weaving-maven/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
