<?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>Jeff Sinclair's Website &#187; aspectj</title>
	<atom:link href="http://www.cooljeff.co.uk/tag/aspectj/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cooljeff.co.uk</link>
	<description>my 2 pence to the world</description>
	<lastBuildDate>Sat, 12 Dec 2009 22:17:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>APT v AspectJ</title>
		<link>http://www.cooljeff.co.uk/2009/01/02/apt-v-aspectj/</link>
		<comments>http://www.cooljeff.co.uk/2009/01/02/apt-v-aspectj/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 18:35:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[apt]]></category>
		<category><![CDATA[aspectj]]></category>

		<guid isPermaLink="false">http://www.cooljeff.co.uk/?p=39</guid>
		<description><![CDATA[APT has interested me for quite some time because after reading various blog entries and articles, it was unclear to me where APT fits in relation to other tools that provide functionality triggered by annotations. Specifically I've been interested in any overlap between APT and AspectJ.

Both AspectJ and APT provide the following common functionality:

<ul>
<li>Ability to intercept annotations.</li>
<li>Ability to declare custom compiler errors and warnings based off annotation matching.</li>
</ul>

I've attempted to implement an architecture enforcement requirement using both tools in order to better understand where APT fits in the developer toolbox.]]></description>
			<content:encoded><![CDATA[<p>APT has interested me for quite some time because after reading various blog entries and articles, it was unclear to me where APT fits in relation to other tools that provide functionality triggered by annotations. Specifically I&#8217;ve been interested in any overlap between APT and AspectJ.</p>
<p>Both AspectJ and APT provide the following common functionality:</p>
<ul>
<li>Ability to intercept annotations.</li>
<li>Ability to declare custom compiler errors and warnings based off annotation matching.</li>
</ul>
<p>I attempted to implement an architecture enforcement requirement using both tools. The requirement was simple: a compile error should occur when an attempt is made to access a class annotated with <em>@RestrictedResource</em> unless the caller is annotated with <em>@SuppressRestrictions</em>.</p>
<p>This was relatively simple with AspectJ, the following Aspect provides the enforcement:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> aspect RestrictedResourceEnforcer <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> pointcut accessToRestrictedResource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> :
        call<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>@RestrictedResource <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>.<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>..<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                || call<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>@RestrictedResource <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">&#40;</span>..<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> pointcut withinUnrestrictedCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> :
        @withincode<span style="color: #009900;">&#40;</span>SuppressRestrictions<span style="color: #009900;">&#41;</span> || @within<span style="color: #009900;">&#40;</span>SuppressRestrictions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    declare error : accessToRestrictedResource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>withinUnrestrictedCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>:
        <span style="color: #0000ff;">&quot;Attempt to access a restricted resource.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>The following test demonstrated the compiler error as well as the lack of compiler error should <em>@SuppressRestrictions</em> be used:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RestrictedResourceEnforcerTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> testAccessToRestrictedResource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Compile error: attempt to access restricted resource.</span>
        RestrictedClass restrictedClass = <span style="color: #000000; font-weight: bold;">new</span> RestrictedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Compile error: attempt to access restricted resource.</span>
        restrictedClass.<span style="color: #006633;">method1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    @SuppressRestrictions
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> testAccessToSuppressedRestrictionResource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        RestrictedClass restrictedClass = <span style="color: #000000; font-weight: bold;">new</span> RestrictedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        restrictedClass.<span style="color: #006633;">method1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    @RestrictedResource
    <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;">class</span> RestrictedClass <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> method1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Immediately I realised that it is not possible to implement this kind of architecture enforcement using APT without annotating the calling class (i.e <em>RestrictedResourceEnforcerTest</em>). Without marking the calling class, I&#8217;m not going to get a process() callback from APT.</p>
<p>This attempt has shown me that APT is only of real use for processing that is completely contained within the annotated class.</p>
<p>I still wanted try APT in direct comparison with AspectJ so I tried enforcing immutability on any class with an <em>@Immutable</em> annotation. I gave up pretty quickly using APT for this because although it is technically possible, it requires a lot of work with ASM to generate the correct source to enforce immutability (i.e. return unmodifiable collections on getters, throw an exception with setters). AspectJ provides a much more maintainable solution without ever having to go near something like ASM.</p>
<p>So I&#8217;m left wondering if APT has any value to your regular Java developer (rather than toolkit developer), I don&#8217;t think it has. APT&#8217;s use-case looks purely in the space of creating extensions to the regular Java compiler to generate bye products directly related to the class with the annotation.</p>
<p>As a general rule I&#8217;d break down APT and AspectJ usage as follows:</p>
<ul>
<li>Use AspectJ if you need to add a functional requirement to existing entities. Examples include: monitoring, architecture enforcement, transactional functionality.</li>
<li>Use APT if you need to generate bye products for framework integration. Examples include: schema generation, source code generation tools (e.g. JAXB, JAXWS)</li>
</ul>
<p>APT is not designed to be updating code to meet a functional requirement, AspectJ is. Instead APT provides a compiler extension that allows you to generate bye products driven by meta data on a class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cooljeff.co.uk/2009/01/02/apt-v-aspectj/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
	</channel>
</rss>
