First AOP use case
October 2, 2005 Leave a comment
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.
}
}
}