<?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>Saulo Silva&#039;s Blog &#187; java</title>
	<atom:link href="http://saulosilva.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://saulosilva.com</link>
	<description>On technology, programming, et cetera.</description>
	<lastBuildDate>Thu, 22 Jul 2010 14:14:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Aspect-oriented programming with AspectJ</title>
		<link>http://saulosilva.com/2009/03/aspect-oriented-programming-with-aspectj/</link>
		<comments>http://saulosilva.com/2009/03/aspect-oriented-programming-with-aspectj/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 03:35:47 +0000</pubDate>
		<dc:creator>Saulo</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://pensador.org/?p=276</guid>
		<description><![CDATA[In one of the courses I took this session I was introduced to aspect-oriented programming. For those who are not familiar with it, an aspect can be used to implement a concern that is crosscutting among different components. There are a few such concerns that are commonly affected by crosscutting, namely: authentication, persistence, logging and [...]]]></description>
			<content:encoded><![CDATA[<p>In one of the courses I took this session I was introduced to aspect-oriented programming. For those who are not familiar with it, an aspect can be used to implement a concern that is crosscutting among different components.</p>
<p>There are a few such concerns that are commonly affected by crosscutting, namely: authentication, persistence, logging and contract checking.</p>
<p>It is argued that with object-oriented programming, even after proper refactoring, it is not always possible to map a requirement to a single component. When a requirement is implemented by more than one component, scattering occurs. <acronym title="Aspect-oriented programming">AOP</acronym> allows you to localize the scattered requirement into an aspect.</p>
<p>AspectJ is an extension to the Java language that adds support to <acronym title="Aspect-oriented programming">AOP</acronym>. An aspect in AspectJ is composed of <em>pointcuts</em> and <em>advices</em>. A pointcut defines the condition that needs to be met in order for the logic in an advice to be executed. A pointcut could be, for instance, the execution of a method in a particular class.</p>
<h3>How AspectJ can be useful</h3>
<p>Let&#8217;s look at an example. Say we want to log when <a title="A horse drinking water." href="http://www.flickr.com/photos/architekt2/185791912/">horses drink water</a> and when <a title="A cow eating grass." href="http://www.flickr.com/photos/dizzo/2168240431/">cows eat grass</a>. Assume we have a class <code>Logger</code> with a static method <code>log()</code>. Here&#8217;s what the implementation could look like:</p>
<pre class="brush: java;">public class Horse {
	private int consumedWaterInLitres;

	public void drink() {
		consumedWaterInLitres++;
		Logger.log(&quot;A horse is drinking water.&quot;);
	}
}

public class Cow {
	private int consumedGrassInGrams;

	public void eat() {
		consumedGrassInGrams++;
		Logger.log(&quot;A cow is eating grass.&quot;);
	}
}</pre>
<p>The argument is that the concern of logging is now crosscut among the Horse and Cow components. In addition, it can be said that <code>Horse.drink()</code> and <code>Cow.eat()</code> are responsible for more core logic (eating and drinking) than they should have been. Let&#8217;s try to fix this problem with an aspect:</p>
<pre class="brush: java;">public aspect Logging {
	pointcut logHorseDrinking() : call(public void Horse.drink());
	pointcut logCowEating() : call(public void Cow.eat());

	void after() : logHorseDrinking() {
		Logger.log(&quot;A horse is drinking water.&quot;);
	}

	void after() : logCowEating() {
		Logger.log(&quot;A cow is eating grass.&quot;);
	}
}</pre>
<p>Here we defined two pointcuts that capture calls to <code>Horse.drink()</code> and to <code>Cow.eat()</code>. We&#8217;ve attached these pointcuts to two advices that will run after these methods are executed. With the Logging aspect in place, we can now remove all calls to <code>Logger.log()</code> from <code>Horse</code> and <code>Cow</code>. Isn&#8217;t that great?</p>
<h3>How AspectJ can be dangerous</h3>
<p><img class="size-full wp-image-330 alignright" title="Dog Class Diagram" src="http://saulosilva.com/wp-content/uploads/2009/03/dog_class_diagram.png" alt="Dog before DogAspect" width="86" height="46" /></p>
<p>Aspects can also add state, behaviour and inheritance to a component. Privileged aspects have access to all features in a system—including private ones. Does that raise a red flag? Let&#8217;s look at the class <code>Dog</code> below.</p>
<pre class="brush: java;">public class Dog {
}</pre>
<p>It has no features—no attributes nor methods—and doesn&#8217;t extend from any class, right? Wrong:</p>
<pre class="brush: java;">public aspect DogAspect {
	declare parents: Dog extends Animal;
	private String Dog.ownersName = &quot;Bobert&quot;;

	public String Dog.getOwnwersName() {
		return ownersName;
	}
}</pre>
<p><img class="size-full wp-image-353 alignright" title="Class Dog after DogAspect" src="http://saulosilva.com/wp-content/uploads/2009/03/dog_after_dogaspect.png" alt="Dog after DogAspect" width="146" height="136" /></p>
<p>The aspect above has completely changed the class <code>Dog</code>. It has made it an <code>Animal</code>, it has added an attribute <em>ownersName</em> and a getter for it. The worst part is that <code>Dog</code> is completely oblivious to the aspect—it will never know about it. In fact, unless you as a developer look at all aspects on the system, you will never know about it either.</p>
<p>That is not completely true because some <acronym title="Integrated development environment">IDE</acronym>s will provide a visual clue whenever a class is affected by an aspect. The <a href="http://www.eclipse.org/ajdt/">AspectJ Developement Tools</a> add-on for <a href="http://eclipse.org/">Eclipse</a> (a great <acronym title="Integrated development environment">IDE</acronym> by the way) is supposed to show a marker on the editor margin whenever a component is being advised by an aspect. I have the latest version installed (ADJT 1.6.4) but for some reason the marker is not showing up, unfortunately.</p>
<p>There are also other issues one might encounter when using AspectJ. It is not an easy task to document the impact aspects have on a system—<acronym title="Unified Modeling Language">UML</acronym> has no support for aspects at this moment, and how are you going to show that on a sequence diagram? Also, the debugging and tracing of execution of a class that is being advised by an aspect can get pretty tricky.</p>
<h3>In conclusion</h3>
<p>I believe that the application of <acronym title="Aspect-oriented programming">AOP</acronym> could indeed improve the quality of a system through the localization of crosscutting concerns. However, its Java implementation—AspectJ—provides a level of control that is too risky to be used in industrial medium- to large-scale projects. Its supporting technologies and documenting tools have not yet reached the desired maturity level.</p>
]]></content:encoded>
			<wfw:commentRss>http://saulosilva.com/2009/03/aspect-oriented-programming-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
