<?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>Christian Schenk&#187; aop</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/aop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Caching with AspectJ</title>
		<link>http://www.christianschenk.org/blog/caching-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/caching-with-aspectj/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 08:28:59 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/caching-with-aspectj/</guid>
		<description><![CDATA[Shows how to use AspectJ and annotations to implement a simple cache that speeds up your application]]></description>
			<content:encoded><![CDATA[<p>In my <a title="Implementing a simple cache with Java" href="http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/">last post</a> I presented a very simple cache that stores objects. Now I want to use this cache with <a href="http://www.eclipse.org/aspectj/">AspectJ</a> in a small sample application.</p>
<p>You can download the Eclipse project as a <a href="http://data.christianschenk.org/caching-with-aspectj/CachingWithAspectJ-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/caching-with-aspectj/CachingWithAspectJ-1.0.zip">zip</a> file or browse the code online <a href="http://data.christianschenk.org/caching-with-aspectj/xref/">here</a>.</p>
<p><span id="more-72"></span></p>
<h2>Implementing the aspect</h2>
<p>I decided to use an annotation and an aspect with a pointcut that matches this annotation. All I had to do was to annotate the methods that should be cached.</p>
<p>Here&#8217;s the very generic version of a pointcut that matches the execution of all methods annotated with <code>@Cachable</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;execution(@Cachable * *.*(..))&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> cache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>The logic is this simple:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cache()&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> aroundProfileMethods<span style="color: #009900;">&#40;</span>ProceedingJoinPoint thisJoinPoint<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// construct genericIdentifier</span>
  <span style="color: #003399;">Object</span> obj <span style="color: #339933;">=</span> cache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>genericIdentifier<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    obj <span style="color: #339933;">=</span> thisJoinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cache.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>genericIdentifier, obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">return</span> obj<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Everytime a cachable method is executed we&#8217;ll first have a look at the cache. If there&#8217;s a cache hit we&#8217;ll immediately return the object. If there&#8217;s no such object in the cache, we&#8217;ll call the cachable method, put the returned object in the cache and finally return the object.</p>
<p>The <code>genericIdentifier</code> is a string consisting of the package-, class- and method-name and the supplied arguments. For a method named <code>foo</code> from a class <code>bar</code> inside a package called <code>baz</code> this might result in:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;baz.bar.foo-Integer-42&quot;</span></pre></div></div>

<p>The method <code>foo</code> takes one argument and at the time of this execution it was <code>42</code>. This way we&#8217;ll only return objects from the cache if this <code>generic identifier</code> matches.</p>
<h2>Using the aspect</h2>
<p>All we have to do is to use the <code>@Cachable</code> annotation. Have a look at it:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
@Target<span style="color: #009900;">&#40;</span>ElementType.<span style="color: #006633;">METHOD</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> Cachable <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>It can be used with any method that returns something:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Cachable
<span style="color: #000000; font-weight: bold;">public</span> BigObject getObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> BigObject<span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first time this method is called we&#8217;ll store the object in the cache. If it&#8217;s called again with the same parameter we&#8217;ll return the object from the cache.</p>
<h2>Conclusion</h2>
<p>Implementing a cache is simple and so is using it with AspectJ. I presented a straightforward way to speed up any application that deals with <em>big</em> objects which take a long time to load. All you have to do is to use the <code>@Cachable</code> annotation along with the aspect. Implementing another cache in the aspect should be simple, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/caching-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Singletons with AspectJ</title>
		<link>http://www.christianschenk.org/blog/singletons-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/singletons-with-aspectj/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 11:18:41 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/singletons-with-aspectj/</guid>
		<description><![CDATA[Although I wouldn't recommend it this post shows you how to implement the Singleton-Pattern with AspectJ 5]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://www.stumbleupon.com/url/www.sophisticated-it.de/blog/index.php/2007/07/17/singleton-pattern-mit-aspectj/">stumbled</a> upon this <a title="Singleton-Pattern mit AspectJ" rel="nofollow" href="http://www.sophisticated-it.de/index.php/2007/07/17/singleton-pattern-mit-aspectj/">post</a> (<em>german</em>) that shows how to implement the singleton pattern with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a>. Although you may want to use <a title="Category: Dependency Injection" href="/blog/tag/dependency-injection/">dependency injection</a> with <a title="Springframework.org" href="http://www.springframework.org/">Spring</a> or <a title="google-guice" href="http://code.google.com/p/google-guice/">Guice</a>, there might be (hopefully rare) occasions on which you opt for this solution. In this post we&#8217;ll check out how to do this with AspectJ 5.</p>
<p>The Eclipse project with the sample code for this post can be downloaded as <a href="http://data.christianschenk.org/singletons-with-aspectj/SingletonWithAspectJ-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/singletons-with-aspectj/SingletonWithAspectJ-1.0.zip">zip</a>; make sure to install the <a title="Eclipse AspectJ Development Tools" href="http://www.eclipse.org/ajdt/">AspectJ Development Tools</a>. You can browse the code online <a title="Singletons with AspectJ - Code Reference" href="http://data.christianschenk.org/singletons-with-aspectj/xref/">here</a>.</p>
<p><span id="more-37"></span></p>
<h2>Classic singleton pattern</h2>
<p>The traditional way to implement a singleton consists of a <code>private</code> constructor, a <code>static final</code> field that holds the instance of the class and a <code>getInstance</code> method that returns this instance. In Java this looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Singleton <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Singleton singleton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Singleton getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> singleton<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This gets really annoying if you&#8217;ve got this code more than once in your application. So lets have a look at an aspect that resembles this functionality.</p>
<h2>Implementing the aspect</h2>
<p>If you&#8217;ve got more than one singleton in your application you may think of a singleton as a crosscutting concern. Consequently, you&#8217;ll write an aspect that intercepts calls to the constructor of the singleton class and instead of creating a new instance every time you&#8217;ll return the instance that was created at the very fist call to the constructor.</p>
<p>The logic for this idea can be expressed with the following code fragment:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Map<span style="color: #339933;">&lt;</span>Class<span style="color: #339933;">&lt;?&gt;</span>, Object<span style="color: #339933;">&gt;</span> singletons<span style="color: #339933;">;</span>
...
<span style="color: #006633;">Class</span><span style="color: #339933;">&lt;?&gt;</span> singletonClass <span style="color: #339933;">=</span> joinPoint.<span style="color: #006633;">getSignature</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDeclaringType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Object</span> singletonObject <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">singletons</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>singletonClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>singletonObject <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  singletonObject <span style="color: #339933;">=</span> joinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">singletons</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>singletonClass, singletonObject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">return</span> singletonObject<span style="color: #339933;">;</span></pre></div></div>

<p>We have a map that holds references to all instances of our singleton classes. The method that intercepts the calls to the constructor of a given class stores a reference to the created instance in the map and returns that instance on the following calls to the constructor of the same class.</p>
<p>So, you can do the following in the code and will get references to the same instance of the class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Singleton s1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Singleton s2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// s1 == s2</span></pre></div></div>

<p>All that&#8217;s left to do, is to define a pointcut and to remove the boilerplate code from the singleton class. A pointcut can look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;call(*.SingletonAop.new(..))&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> singletons<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Conclusion</h2>
<p>We <a title="Singletons with AspectJ - Code Reference" href="http://data.christianschenk.org/singletons-with-aspectj/xref/">implemented</a> a solution for the singleton pattern with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a> 5. The used approach caches the instances of the singleton classes and returns the cached ones once the constructor has been called for the first time. Note: to simplify matters we left out thread safety.</p>
<p>Although this solution is somehow elegant and may be useful to clean up some code in a legacy system, we should consider the following points:</p>
<dl>
<dt>Can we use a dependency injection framework?</dt>
<dd>Spring uses singletons by default and with Guice you can mark singletons with the <code>@Singleton</code> annotation. So, we might want to choose one of these frameworks instead of implementing this functionality on our own.</dd>
<dt>Java skill of the team?</dt>
<dd>The presented implementation should be well documented, because nothing would be more irritating than a novice developer in your team who doesn&#8217;t know about this <em>magic</em> behind the scenes and relies on different instances of the <em>singleton</em> class (<em>&#8220;I thought <code>new</code> would be doing&#8230;&#8221;</em>).</dd>
</dl>
<p>There are <em>other</em> solutions available to this problem. So, I wouldn&#8217;t recommend to implement this kind of <em>caching</em> with AspectJ.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/singletons-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Profiling with AspectJ</title>
		<link>http://www.christianschenk.org/blog/profiling-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/profiling-with-aspectj/#comments</comments>
		<pubDate>Thu, 09 Aug 2007 10:22:23 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/profiling-with-aspectj/</guid>
		<description><![CDATA[This post shows how to profile applications easily with AspectJ]]></description>
			<content:encoded><![CDATA[<p>Lately, I read a <a href="http://www.bibsonomy.org/bibtex/24354d3b3d13708590824241967d18cea/cschenk">paper</a> about profiling with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a>: it investigates how to profile heap usage, object lifetime, wasted time and time spent. I was wondering how difficult it would be to write my own simple profiler; and as we&#8217;ll see it&#8217;s easy and fun.</p>
<p>If you&#8217;d like to check out the sample code that accompanies this post, you can download the Eclipse project as <a href="http://data.christianschenk.org/profiling-with-aspectj/ProfilingWithAspectJ-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/profiling-with-aspectj/ProfilingWithAspectJ-1.0.zip">zip</a>; make sure to install the <a title="Eclipse AspectJ Development Tools" href="http://www.eclipse.org/ajdt/">AspectJ Development Tools</a>. You can browse the code online <a title="Profiling with AspectJ - Code Reference" href="http://data.christianschenk.org/profiling-with-aspectj/xref/">here</a>.</p>
<p><span id="more-36"></span></p>
<h2>How to profile methods</h2>
<p>Writing an aspect that adds advice to the methods you&#8217;d like to profile is as easy as this. You define a pointcut that matches the methods you&#8217;d like to monitor and define an <code>around</code> advice that calculates the time it took the method to execute. Have a look at the following piece of code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;profile()&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> aroundInterestingMethods<span style="color: #009900;">&#40;</span>ProceedingJoinPoint thisJoinPoint<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> start, end<span style="color: #339933;">;</span>
  start <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">nanoTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  thisJoinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  end <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">nanoTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// calculate time: (end - start) / 1000) / 1000;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All that&#8217;s left to do is to define a pointcut named <code>profile</code>, that matches the methods you&#8217;d like to monitor. Since the pointcut will be variable, I wrote an <a title="Abstract profiling aspect" href="http://data.christianschenk.org/profiling-with-aspectj/xref/org/christianschenk/aspectj/profiling/AbstractProfilingAspect.html">abstract aspect</a> that can be subclassed for different monitoring needs.</p>
<p>In a <a title="Sample code for an implementation" href="http://data.christianschenk.org/profiling-with-aspectj/xref/org/christianschenk/aspectj/profiling/RunnerProfilingAspect.html">subclass</a> you would implement the <code>profile</code> method like so, if you&#8217;d like to monitor all methods named <code>run</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;execution(* *.*.run())&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> profile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h2>What about the results?</h2>
<p>If you would run the <a title="Profiling with AspectJ - Code Reference" href="http://data.christianschenk.org/profiling-with-aspectj/xref/">sample code</a>, you could see this:</p>
<pre class="code">
    Results for Runner.run (1002ms)
    Method: TaskB.run() took 600ms (60.0%)
    Method: TaskA.run() took 400ms (40.0%)
</pre>
<p>Once the <code>run</code> method of the <a title="Monitored class from the sample code" href="http://data.christianschenk.org/profiling-with-aspectj/xref/org/christianschenk/aspectj/profiling/example/Runner.html">Runner</a> class comes to an end, we can see these statistics, produced by the <code>afterTopLevelMethod</code> method from the profiling <a title="Abstract profiling aspect" href="http://data.christianschenk.org/profiling-with-aspectj/xref/org/christianschenk/aspectj/profiling/AbstractProfilingAspect.html">aspect</a>.</p>
<p>Based on this textual results it would be pretty easy to produce a graphical representation with <a title="gnuplot homepage" href="http://www.gnuplot.info/">gnuplot</a> or <a title="JFreeChart - Java chart library" href="http://www.jfree.org/jfreechart/">JFreeChart</a>. I could imagine a picture like this:</p>
<p><img src="http://data.christianschenk.org/profiling-with-aspectj/profiling.png" alt="Graphical representation of the profiling results"/></p>
<p>Although this looks very simple, a picture might be easier to absorb if there would be more methods.</p>
<h2>Conclusion</h2>
<p>As we&#8217;ve seen, profiling with AspectJ can be implemented straight forward. My simple example showed how to monitor certain methods and calculate the their execution time. The authors of this <a href="http://www.bibsonomy.org/bibtex/24354d3b3d13708590824241967d18cea/cschenk">paper</a> point us to some more things we might want to profile, e.g. heap space. Although I haven&#8217;t tried it yet, it should be possible to implement this too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/profiling-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Logging with AspectJ</title>
		<link>http://www.christianschenk.org/blog/logging-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/logging-with-aspectj/#comments</comments>
		<pubDate>Fri, 03 Aug 2007 06:15:12 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/logging-with-aspectj/</guid>
		<description><![CDATA[This post shows how to add logging to your application with AspectJ]]></description>
			<content:encoded><![CDATA[<p><a title="AOP with AspectJ" href="http://www.christianschenk.org/blog/aop-with-aspectj/">Again</a>, I would like to endorse the usage of aspect oriented programming because it may ease development significantly. That&#8217;s almost always the case with crosscutting concerns like logging and in this post we&#8217;ll develop a solution with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a>. So, adding logging to your application is as easy as this:</p>
<ul>
<li>implement <a title="AbstractLoggingAspect code" href="http://data.christianschenk.org/logging-with-aspectj/xref/org/christianschenk/aspectj/logging/AbstractLoggingAspect.html">this</a> <code>AbstractLoggingAspect</code> and define the methods you&#8217;d like to monitor</li>
<li>declare advice <code>before</code>, <code>after</code> or <code>around</code> the logging pointcut and <code>println</code> your logging to <code>stdout</code>.</li>
</ul>
<p>Once you&#8217;re finished with this you&#8217;ve got logging in your application without touching the original code. We&#8217;ll have a look at the details in the next sections.</p>
<p>If you&#8217;d like to check out the code that accompanies this post you can download the Eclipse project as <a href="http://data.christianschenk.org/logging-with-aspectj/LoggingWithAspectJ-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/logging-with-aspectj/LoggingWithAspectJ-1.0.zip">zip</a>; make sure to install the <a title="Eclipse AspectJ Development Tools" href="http://www.eclipse.org/ajdt/">AspectJ Development Tools</a>. You can browse the code online <a title="LoggingWithAspectJ - Code Reference" href="http://data.christianschenk.org/logging-with-aspectj/xref/">here</a>.</p>
<p><span id="more-35"></span></p>
<h2>The logging template</h2>
<p>The abstract aspect I <a title="AbstractLoggingAspect code" href="http://data.christianschenk.org/logging-with-aspectj/xref/org/christianschenk/aspectj/logging/AbstractLoggingAspect.html">wrote</a> was inspired by the <code>IndentedLogging</code> aspect from the book <a href="http://www.bibsonomy.org/bibtex/2684fcd95b8bce37858bcc13753047a7e/cschenk">AspectJ in Action</a> by <a title="Homepage of Ramnivas Laddad" href="http://ramnivas.com/">Ramnivas Laddad</a>. My <code>AbstractLoggingAspect</code> class does the following:</p>
<ul>
<li>indents logging and</li>
<li>supplies an abstract method.</li>
</ul>
<p>The abstract method needs to be implemented by an aspect, that defines <em>things</em> that should be monitored. The advice for this logging method should just print messages to <code>stdout</code> with <code>System.out</code>&#8217;s <code>println</code> &#8211; this way they&#8217;re getting indented.</p>
<h2>Implementing the aspect</h2>
<p>A subclass of <code>AbstractLoggingAspect</code> implements the <code>logging</code> method and uses a pointcut to define join points that should be monitored. I <a title="MyLogicLoggingAspect sample code" href="http://data.christianschenk.org/logging-with-aspectj/xref/org/christianschenk/aspectj/logging/MyLogicLoggingAspect.html">wrote</a> one for the sample code like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(execution(* *.*doEverything()) ||
            execution(* *.*doSomething())) &amp;&amp;
           !within(*Test)&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> logging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Every execution of the methods <code>doEverything</code> and <code>doSomething</code> that isn&#8217;t inside a test class will be matched. After that we can <a title="MyLogicLoggingAspect sample code" href="http://data.christianschenk.org/logging-with-aspectj/xref/org/christianschenk/aspectj/logging/MyLogicLoggingAspect.html">write</a> an around advice to print some logging information. If you would run the sample code it would print the following:</p>
<pre class="code">
Entering [MyLogic.doEverything()]
  Doing everything...
  Entering [A.doSomething()]
    Doing : A
  Leaving  [A.doSomething()]
  Entering [B.doSomething()]
    Doing : B
  Leaving  [B.doSomething()]
Leaving  [MyLogic.doEverything()]
</pre>
<p>The calls to the different methods are logged and indented.</p>
<h2>Conclusion</h2>
<p>Adding fancy indented logging to your application with AspectJ is very easy: subclass the <code>AbstractLoggingAspect</code> and customize the logging to your needs. After that you&#8217;ve logging in your application without changing the original code &#8211; all the work gets done inside the aspects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/logging-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>AOP with AspectJ</title>
		<link>http://www.christianschenk.org/blog/aop-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/aop-with-aspectj/#comments</comments>
		<pubDate>Mon, 02 Jul 2007 14:48:11 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[aspectwerkz]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/aop-with-aspectj/</guid>
		<description><![CDATA[This post is about AspectJ and why you might want to use it every day]]></description>
			<content:encoded><![CDATA[<p>Recently I read <a href="http://www.bibsonomy.org/bibtex/2684fcd95b8bce37858bcc13753047a7e/cschenk">AspectJ in Action</a> by <a title="Homepage of Ramnivas Laddad" href="http://ramnivas.com/">Ramnivas Laddad</a> for the second time. Since I first read the book about three years ago, I haven&#8217;t used AspectJ in a <em>real</em> project yet. Generally speaking I think that AOP techniques aren&#8217;t applied that often in practice.</p>
<p>I think this should change, so I put together some sensible arguments for AOP. After that we&#8217;ll have a quick look at AspectJ. I&#8217;d like to emphasize the word <em>quick</em> here, because there&#8217;s a lot of information out there. If you&#8217;d like to read more about AOP, you may check out the following links:</p>
<ul>
<li><a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ Project</a> homepage</li>
<li><a title="Codehaus - AspectWerkz" href="http://aspectwerkz.codehaus.org/">AspectWerkz</a> at the <a title="The Codehaus" href="http://codehaus.org/">Codehaus</a></li>
<li><a title="AOP Benchmark - AspectWerkz" href="http://docs.codehaus.org/display/AW/AOP+Benchmark">AWbench</a> is a benchmark of different AOP implementations</li>
<li><a title="The AspectJ 5 Development Kit Developer's Notebook" href="http://www.eclipse.org/aspectj/doc/released/adk15notebook/index.html">The AspectJ<sup>TM</sup> 5 Developer&#8217;s Notebook</a></li>
<li>an IBM article <a title="IBM - Introducing AspectJ 5" href="http://www.ibm.com/developerworks/java/library/j-aopwork8/">introducing AspectJ 5</a></li>
<li>don&#8217;t confuse with <a title="Introduction to Abject-Oriented Programming" href="http://typicalprogrammer.com/programming/abject-oriented/">Abject-Oriented Programming</a></li>
<li>do a <a title="Google search about AOP" href="http://www.google.com/search?q=aspect+oriented+programming">Google search</a></li>
</ul>
<p><span id="more-30"></span></p>
<h2>Why AOP?</h2>
<p>Suppose you&#8217;re building a web application. The more you code the more features you&#8217;d like to add. More features translate to more <em>concerns</em> in your application: you might want to add logging, security and persistence just to name a few. These concerns are <em>crosscutting concerns</em> because you need to add them in various places in your application. Of course you&#8217;ll create one single class (OOP-style) that&#8217;ll handle logging but this class has to be called in a lot of different places throughout your application. The same thing holds true for the other concerns and you probably end up with a system like this:</p>
<p><img class="center" src="http://data.christianschenk.org/aop-with-aspectj/no-aop.png" alt="Crosscutting concerns scattered all over your code" /></p>
<p>Suddenly your excellently organized code gets polluted with totally different <em>aspects</em> of the desired features. As more and more aspects are scattered over your code, it&#8217;ll will be harder to read and you&#8217;ll have a hard time figuring out where your business logic is.</p>
<p>Luckily, you can avoid this if you reorganize the different aspects into separate classes: there&#8217;ll be one for logging, security, persistence and so on. This keeps your business logic code concise and to the point. Furthermore it&#8217;ll be easier for you if you&#8217;d like to change the behavior of these concerns &#8211; just change the corresponding aspects. This leads to a system with aspects in different modules separated from the business logic code:</p>
<p><img class="center" src="http://data.christianschenk.org/aop-with-aspectj/aop.png" alt="Crosscutting concerns in separate modules" /></p>
<h2>How to AOP?</h2>
<p>The rule of thumb is this: if you have to copy code into different modules of your application over and over again, consider writing an aspect instead, because you might be working on a crosscutting concern. More often than not you already have a separate class and calls on various locations in the code to methods of that class. Instead of calling these methods everywhere in your code, you can put them into an aspect.</p>
<p>Think of a class that handles a sessions for you; whatever a <em>session</em> might be (e.g. one for a database). Now there are some methods that need a session object and the boilerplate code in the methods always looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> someMethod<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> Session session<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    session.<span style="color: #006633;">startSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
  session.<span style="color: #006633;">endSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So you&#8217;ll have this <code>try-finally</code> block, <code>startSession()</code> and <code>endSession()</code> everywhere in your code where you need to handle a session. A much cleaner solution would be the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> someMethod<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> Session session<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and an aspect like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> aroundSomeMethod<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> ProceedingJoinPoint thisJoinPoint,
                             <span style="color: #000000; font-weight: bold;">final</span> Session session<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    session.<span style="color: #006633;">startSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    thisJoinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
    session.<span style="color: #006633;">endSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now you have a single method (<code>aroundSomeMethod()</code>) where you can configure the calls to a session object and all the other methods (e.g. <code>someMethod()</code>) that need a session object will remain small and will contain only business logic.</p>
<p>I wrote a simple example for this and you can browse the code <a title="AspectJTest - Code Reference" href="http://data.christianschenk.org/aop-with-aspectj/xref/">here</a>; have a look at the <code>example</code> package and the <code>Runner</code> class. The code demonstrates how to get rid of boilerplate code and do things the AOP way:</p>
<p><img class="center" src="http://data.christianschenk.org/aop-with-aspectj/comparison.png" alt="Comparison between conventional coding and AOP" /></p>
<p>What do you think when looking at the picture? <a title="Posts tagged with Inversion of control" href="http://www.christianschenk.org/blog/tag/inversion-of-control/">Inversion of control</a>? I think that AOP can be very helpful when designing an application with IoC in mind.</p>
<h2>Quick look at AspectJ</h2>
<p>I wrote a very simple <em>Hello World</em> application with two aspects: <code>AspectJAspect</code> and <code>AspectJAnnotation</code>. While AspectJ at first introduced a Java-like language to write aspects, with AspectJ 5 it&#8217;s possible to write aspects as classes with special annotations: the two aspects I wrote demonstrate both approaches. Second, there&#8217;s the code for the example I explained in the previous section.</p>
<p>You can download the Eclipse project as <a href="http://data.christianschenk.org/aop-with-aspectj/AspectJTest-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/aop-with-aspectj/AspectJTest-1.0.zip">zip</a> or browse the code <a title="AspectJTest - Code Reference" href="http://data.christianschenk.org/aop-with-aspectj/xref/">here</a>. If you&#8217;d like to run the project in Eclipse, make sure that you&#8217;ve got the <a title="AJDT: AspectJ Development Tools - Downloads" href="http://www.eclipse.org/ajdt/downloads/">AspectJ Development Tools</a> installed.</p>
<p>All the code does is introducing a <code>Main</code> class that has a <code>helloWorld()</code> method. The two aspects <code>AspectJAspect</code> and <code>AspectJAnnotation</code> print some information <code>before</code> and <code>after</code> the <code>helloWorld()</code> method and <code>around</code> the constructor and <code>main()</code> method of the <code>Main</code> class.</p>
<h2>Conclusion</h2>
<p>Although the example code doesn&#8217;t do very much, it showcases some important things:</p>
<ul>
<li>AspectJ integrates seamlessly into Maven and Eclipse. That are the most important points to check: if <em>something</em> doesn&#8217;t work with Maven or Eclipse, it&#8217;s probably not worth using anyway. This isn&#8217;t true for <em>things</em> that are really cool &#8211; if it&#8217;s really cool I might be willing to do some cumbersome hacks to us it.</li>
<li>AspectJ 5 is very easy to use: some annotations here and there &#8211; that&#8217;s it. This is a good thing because this way it should be much easier to promote AOP. There are still some weird error messages, but&#8230; shush, you don&#8217;t have to mention that <img src='http://www.christianschenk.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</li>
</ul>
<p>Because AOP isn&#8217;t <em>common knowledge</em> yet, encourage other programmers to read up on AOP; hand them a copy of <a href="http://www.bibsonomy.org/bibtex/2684fcd95b8bce37858bcc13753047a7e/cschenk">AspectJ in Action</a>. If you can&#8217;t convince other people to use AOP, use it to increase your own productivity, i.e. start using it for logging, tracing or coding policy enforcement.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/aop-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
