How to access private static target field in aspect in AspectJ?
- by LihO
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?