<aop> <bind pointcut="execution(POJO->new(int))"> <interceptor class="ConstructorInterceptor"/> </bind> </aop>To apply the interceptor you must create a binding and a pointcut that specifies where in your Java code you want the interceptor applied. execution(constructor expression) defines whenever the constructor is executed. A constructor expression requires a class expression followed by '->' followed by 'new' followed by a list of parameters. You can optionally provide constructor attributes like 'public', 'static', etc. if so desired. You do not have to specify the entire signature of the constructor. It works in much the same way method expressions work.
Each type of intercepted execution (method, constructor, field, etc.) has a specific class that extends the base class org.jboss.aop.joinpoint.Invocation. If you open up ConstructorInterceptor.java, you will see that you can typecast the Invocation parameter into a org.jboss.aop.joinpoint.ConstructorInvocation object. The ConstructorInvocation class allows you to obtain additional information about the particular constructor call like the java.lang.reflect.Constructor object representing the call, the arguments.
$ 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] --- new POJO(); --- [java] empty constructor [java] --- new POJO(String); --- [java] String constructor [java] --- new POJO(int); --- [java] <<< Entering ConstructorInterceptor for: public POJO(int) [java] int constructor [java] >>> Leaving ConstructorInterceptor