Array Element Interception

Array Element Interception

To intercept when elements of an array is matched we add the following to our jboss-aop.xml:

   <arrayreplacement class="Woven"/>
   <prepare expr="field(* Woven->ints)"/>
   
   <arraybind type="READ_WRITE">
       <interceptor-ref name="ArrayInterceptor"/>
   </arraybind>

This first arrayreplacement says that whenever an array is being acessed in the class Woven we should delegate those accesses on to JBoss AOP. Next we have a prepare statement that picks out an array field. Since that field belongs to a class picked out by arrayreplacement, that field gets registered for array element interception. Finally, we have a arraybind that says that whenever an array whose access has been woven and intercepted intercepted we should apply ArrayInterceptor.

Running

To compile and run (for further detail, refer to our Compiling and Running Examples Guide):

  $ ant run.aopc

It will javac the files and then run the AOPC precompiler to manipulate the bytecode, then finally run the example. The output should be similar to this:

_run.aopc:
     [java] --- new Woven(); ---
     [java] Initialising array
     [java] --- woven.setInt(0, 100); ---
     [java] <<< Entering ArrayInterceptor type: org.jboss.aop.array.IntArrayElementWriteInvocation
     [java] <<< We have an array element write invocation of type: org.jboss.aop.array.IntArrayElementWriteInvocation
     [java] New value for index 0 of [I@d9e5ad is 100
     [java] Typed value is 100
     [java] >>> Leaving ArrayInterceptor
     [java] --- woven.getInt(0); ---
     [java] <<< Entering ArrayInterceptor type: org.jboss.aop.array.IntArrayElementReadInvocation
     [java] <<< We have an array element read invocation of type: org.jboss.aop.array.IntArrayElementReadInvocation
     [java] >>> Returned value was 100
     [java] >>> Leaving ArrayInterceptor
     [java] --- woven.getInt(0) was 100 ---
     [java] --- new NotWoven(); ---
     [java] Initialising array
     [java] --- notWoven.setInt(0, 100); ---
     [java] --- notWoven.getInt(0); ---
     [java] --- notWoven.getInt(0) was 100 ---