How to access private static target field in aspect in AspectJ?

Posted by LihO on Stack Overflow See other posts from Stack Overflow or by LihO
Published on 2013-10-28T21:51:13Z Indexed on 2013/10/28 21:53 UTC
Read the original article Hit count: 220

Filed under:
|
|
|

I have a simple class Main with private static int x and an aspect that should output the old value of x before it is reassigned:

public class Main {
    private static int x;

    public static void main(String[] args) {
         foo(7);
    }

    public static void foo(int y) {
        x = y;
    }
}

and MonitorX.aj:

public aspect MonitorX {

    before() : set(static int Main.x){
        System.out.println(Main.x);
    }
}

which doesn't work since I can't access private x using Main.x. I've also tried:

    before(int t) : set(static int Main.x) && target(t){
        System.out.println(t);
    }

which doesn't work either (nothing is outputted, if I try to output string, it seems that the aspect isn't invoked at all). However printing out the new value that is being assigned works:

    before(int newVal) : set(static int Main.x) && args(newVal){
        System.out.println(newVal);
    }

What am I missing?

© Stack Overflow or respective owner

Related posts about java

Related posts about aspectj