First AOP use case

My first real AOP use case uses Java 5 annotations and AspectWerkz 2.0. I use AspectWerkz because I can look at their Java source code and I could use Pointcuts to pick out Java 5 annotations. At this point I don’t combine Generics and AOP. I wanted to show a status message only if particular methods were called. This is done so that the status is more accurate. The user will know the exact status at any point in time.

My annotation is

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE,
       ElementType.METHOD })
public @interface StatusShow {

 public String message();
}

I annotate like this
    @StatusShow( message=”Select diagram” )

I use Java reflection to get the argument to the annotation and print in the
status bar.
@Aspect(“perJVM”)
public class StatusReportAspect {
 
 @Expression(“execution(@com.blueprint.util.annotation.StatusShow * *..*(..))”)
    Pointcut statusMethods;

 @Before(“statusMethods”)
 public void before( final StaticJoinPoint sjp, final JoinPoint jp ) {
  try{
   Class<PaperSheet> clazz = PaperSheet.class;
   Method method = clazz.getMethod( sjp.getSignature().getName(),
                                                                  Graphics2D.class);
   if( method.isAnnotationPresent( StatusShow.class )){
    BluePrint.getStatus().
      setText( method.getAnnotation( StatusShow.class ).
                   message() );
   }
  }catch( Throwable t ){
   t.printStackTrace();
   //This is benign. Just ignore.
  }
 }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: