package org.jboss.aop.advice; import org.jboss.aop.joinpoint.Invocation; public interface Interceptor { public String getName(); public Object invoke(Invocation invocation) throws Throwable; }
import org.jboss.aop.Bind; import org.jboss.aop.InterceptorDef; @InterceptorDef @Bind (pointcut = "all(POJO)") public class SimpleInterceptor implements Interceptor { ... }
The @InterceptorDef marks the class as an interceptor so that JBoss AOP knows what to do with it. The @Bind annotation specifies the pointcut the interceptor should be bound to. So in this case SimpleInterceptor gets bound to all constructor executions, method calls and field accesses on the POJO class. This example uses an all pointcut expression, but you can of course use any pointcut expression you like. SimpleInterceptor.java contains a few commented out example bindings so you can get rid of and introduce the ones you want. (Note that there can be only one uncommented @Bind at any time)
... <aopc compilerclasspathref="classpath" classpathref="classpath" verbose="true"> <classpath path="."/> <src path="."/> <aopclasspath path="."/> </aopc> ... <java fork="yes" failOnError="true" className="Driver"> <sysproperty key="jboss.aop.class.path" value="."/> <classpath refid="classpath"/> </java> ...
$ antIt will javac the files and then run the AOPC precompiler to manipulate the bytecode, then finally run the example. The output should read as follows:
run: [java] <<< Entering SimpleInterceptor for: public POJO() [java] constructor [java] >>> Leaving SimpleInterceptor [java] --- pojo.noop(); --- [java] <<< Entering SimpleInterceptor for: public void POJO.noop() [java] noop() [java] >>> Leaving SimpleInterceptor [java] --- pojo.test1(String param); --- [java] <<< Entering SimpleInterceptor for: public void POJO.test1(java.lang.String) [java] test1(String param): hello world [java] >>> Leaving SimpleInterceptor