Version 6

    Field interception

     

    Overview

    JBoss AOP allows you to intercept a field access and insert behavior their.

    If you look at Driver.java and POJO.java you will see that they are accessing a number of fields.

     

    How do I apply an Interceptor to a field execution?

    To bind an interceptor to the access of a field, you must create an XML file.  Open up jboss-aop.xml and take a look.  Let's take a look at the first bindings.

    <aop>
    ...
       <bind pointcut="get(private java.lang.String POJO->var1)">
           <interceptor class="GetInterceptor"></interceptor>
       </bind>
    ...
    </aop>
    

    This bindings states that when POJO.var1 is accessed for a read, invoke the GetInterceptor.

     

    The next one is:

    ...
       <bind pointcut="set(private java.lang.String POJO->var2)">
           <interceptor class="SetInterceptor"></interceptor>
       </bind>
    ...
    

    Whenever the field POJO.var2 is written to, invoke the SetInterceptor.

     

    The next one is:

    ...
       <bind pointcut="field(public static * POJO->*)">
           <interceptor class="FieldInterceptor"></interceptor>
       </bind>
    ...
    

    Whenever any public static field in POJO is accessed for a read or a write, call the FieldInterceptor.  The field expression matches both get/set of a field.

     

    Running

    Let's try these babies out....

    To compile and run:

      $ ant
    

    It 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] --- pojo.getVar1(); ---
         [java] <<< Entering GetInterceptor for: var1
         [java] >>> Leaving GetInterceptor
         [java] hello
         [java] --- pojo.setVar2(); ---
         [java] <<< Entering SetInterceptor for: var2
         [java] >>> Leaving SetInterceptor
         [java] --- pojo.getVar2(); ---
         [java] world
         [java] --- POJO.var3++; ---
         [java] <<< Entering FieldInterceptor for: var3 type: org.jboss.aop.joinpoint.FieldReadInvocation
         [java] >>> Leaving FieldInterceptor
         [java] <<< Entering FieldInterceptor for: var3 type: org.jboss.aop.joinpoint.FieldWriteInvocation
         [java] >>> Leaving FieldInterceptor