Aspects are now usable


Aspects are now usable

A new software package Aspectwerkz looks like it might really bring Aspects to Java for good (if Xerox doesn’t mind).

Well, I definitely was skeptical at first, but after reading Cedric’s post on this I have to say I am very impressed. Aspects seemed like they would confuse more developers than help given the complicated implementations that I have seen. This package though is amazing and leverages all the software the open source community has to give. Here is an example I threw together:

Aspect definition:

public Object execute(JoinPoint jp) throws Throwable {
 long start = System.currentTimeMillis();
 Object result = jp.proceed();
 long end = System.currentTimeMillis();
 System.err.println(“Time for “ + jp + “: “ + (end — start) + “ ms”);
 return result;
}

Application:

<aspectwerkz>
 <advice name=”TimeMethod” advice=”com.sampullara.j2ee.MethodTimer” deploymentModel=”perJVM”/>
 <aspect class=”com.sampullara.test.HelloWorld”>
 <pointcut type=”method” pattern=”main” advices=”TimeMethod”/>
 </aspect>
</aspectwerkz>

Execution:

/Users/sam/Projects/J2EEWatcher:> aspectwerkz -Daspectwerkz.definition.file=aspectwerkz.xml com.sampullara.test.HelloWorld
Hello, world!
Time for [aspectwerkz.joinpoint.StaticMethodJoinPoint@fd66647e: 0,public static void com.sampullara.test.HelloWorld.___originalMethod$main$1(java.lang.String[]),[Ljava.lang.Object;@4cd580,null,0]: 0 ms

WOW! Simple as can be. The potential applications to WebLogic alone stagger the imagination.