Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Pointcut on an array field-setter


I understand from the AspectJ specification that get and set pointcuts are activated when the whole field is readed or written.
In your code you only write the array once, in the following line:
.

public class testje {

           

            int[] a = new int[5];
....

When you do: inst.a[1] = 1; you are not accessing testje.a but a position of the array (which it is a separate Object).
But when you do:
inst.b = inst.a[1]; you are doing two accesses, one for the field (and thos activates  getPC pointcut) and other for the first position of the array.


I'd like very much an example of a pointcut capturing the moment in which a component of an array is accessed (in read and/or write mode). Perhaps you can try with something like this (it's only an idea): set(int int[].*) &&
set(int[] *.*) ?????????????????????


Hope this helps. Have anyone another explanation? Do anyone knows how to do it?


Enrique J. Amodeo Rubio


Bert Hajee wrote:

Hello,

 

I’am trying to make an aspect that works on every field set operation on an integer array. This is the test case I used.

 

 

 

public aspect SetAndGetAspect {

            pointcut setPC() : set(int[] *.*);

            pointcut getPC() : get(int[] *.*);

           

            before(): setPC(){

                       

                        System.out.println("Reached set pointcut " + thisJoinPoint);

            }

           

            before() : getPC(){

                        System.out.println("Reached get pointcut " + thisJoinPoint);

            }

 

}

 

 

This is the test program

 

ublic class testje {

           

            int[] a = new int[5];

            int b;

 

            public static void main(String[] args) {

                        testje inst = new testje();

                       

                        inst.a[1] = 1;

                        inst.a[2] = 2;

                        inst.a[3] = 3;

                        inst.a[4] = 4;

                       

                        inst.b = inst.a[1];

                        inst.b = inst.a[2];

                        inst.b = inst.a[3];

                        inst.b = inst.a[4];

            }

}

This is the output:

 

Reached set pointcut set(int[] testje.a)

Reached get pointcut get(int[] testje.a)

Reached get pointcut get(int[] testje.a)

Reached get pointcut get(int[] testje.a)

Reached get pointcut get(int[] testje.a)

Reached get pointcut get(int[] testje.a)

Reached get pointcut get(int[] testje.a)

Reached get pointcut get(int[] testje.a)

Reached get pointcut get(int[] testje.a)

 

 

Why doesn’t the pointcut setPC trigger on all the array assignments?

 

Thanks in advance,

 

Bert Hajee

 

hajee@xxxxxxxxxxx

 



Back to the top