mirror of
https://github.com/eclipse-aspectj/aspectj.git
synced 2024-09-13 15:45:38 +02:00
34 lines
640 B
Java
34 lines
640 B
Java
|
import org.aspectj.testing.Tester;
|
||
|
|
||
|
public class AdviceOnPrivileged {
|
||
|
public static void main(String[] args) {
|
||
|
C c1 = new C();
|
||
|
c1.getX();
|
||
|
|
||
|
Tester.check(!c1.gotXOutsideC, "normal field access");
|
||
|
|
||
|
C c2 = new C();
|
||
|
ExposeC.getX(c2);
|
||
|
|
||
|
Tester.check(c2.gotXOutsideC, "privileged field access");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class C {
|
||
|
public boolean gotXOutsideC = false;
|
||
|
|
||
|
private int x=1;
|
||
|
public int getX() { return x; }
|
||
|
}
|
||
|
|
||
|
aspect WatchGets {
|
||
|
before(C c): get(int C.x) && target(c) && !within(C) {
|
||
|
System.out.println("got C.x");
|
||
|
c.gotXOutsideC = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
privileged aspect ExposeC {
|
||
|
public static int getX(C c) { return c.x; }
|
||
|
}
|