summaryrefslogtreecommitdiffstats
path: root/tests/new/InnerClassInPrivilegedAspect.java
blob: f6422158120df97bc1efd9bfb58838144e262dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import org.aspectj.testing.Tester; 

/** @testcase PR#555 inner classes of privileged aspects cannot see target class private members */
public class InnerClassInPrivilegedAspect {
    public static void main(String[] args) {
        Util.start();
        Target.main(args);
        new Target().toString();
        Util.end();
    }
}

class Util {
    public static void event(String s) {
        //System.out.println(s);
        Tester.event(s);
    }
    public static void start() {
        // failing
        Tester.expectEvent("before runner1");
        Tester.expectEvent("before intro1");
        Tester.expectEvent("new1");
        Tester.expectEvent("around runner1");
        Tester.expectEvent("around intro1");
        Tester.expectEvent("before instance1");
        Tester.expectEvent("around instance1");
        Tester.expectEvent("pcd if()1");
        Tester.expectEvent("before run1");
        Tester.expectEvent("around run1");

        // passing
        Tester.expectEvent("before pcd if()1");
        Tester.expectEvent("before1");
        Tester.expectEvent("around1");
    }
    public static void end() {
        Tester.checkAllEvents();
    }
}

class Target {
    private static int privateStaticInt = 1;
    private int privateInt = 1;
    public static void main(String args[]) { }
}

interface Test {
    public boolean t();
}
privileged aspect PrivilegedAspectBefore {
    pointcut p() : execution(static void Target.main(..));

    static class IfTest {
        public boolean t(){ Util.event("pcd if()"+Target.privateStaticInt); return true;}
    }

    pointcut q() : p() 
        && if(new IfTest().t());
                
   
    before() : q() { Util.event("before pcd if()" + Target.privateStaticInt); }

    /** @testcase privileged access to target private static variables in introduced member (of anonymous class type)*/
    static Runnable Target.runner = new Runnable() { 
            public void run() { Util.event("before runner"+Target.privateStaticInt); }
        };
    before() : p() { Target.runner.run(); }

    before() : p() {
        /** @testcase privileged access to target private static variables in before advice (ok) */
        Util.event("before" +Target.privateStaticInt);
    }
    before() : p() {
        /** @testcase privileged access to target private static variables from inner class inside before advice */
        Runnable inner = new Runnable() {
                public void run() {
                    Util.event("before run" +Target.privateStaticInt);
                }
            };
        inner.run();
    }

    /** @testcase privileged access to target private static variables from inner class inside introduced method */
    before() : p() { Target.runbefore(); }
    static void Target.runbefore() {
        Runnable inner = new Runnable() {
                public void run() {
                    Util.event("before intro" +Target.privateStaticInt);
                }
            };
        inner.run();
    }    
}
/** differs from PrivilegedAspectBefore only in using around advice */
privileged aspect PrivilegedAspectAround {
    pointcut p() : execution(static void Target.main(..));

    /** @testcase privileged access to target private static variables from inner class in  introduced constructor */
    Target.new() {
            int i = privateStaticInt;
            Runnable p = new Runnable() { 
                    public void run() { Util.event("new"+Target.privateStaticInt); }
                };
            p.run();
        }
    /** @testcase privileged access to target private static variables in introduced member (of anonymous class type)*/
    static Runnable Target.runner2 = new Runnable() { 
            public void run() { Util.event("around runner"+Target.privateStaticInt); }
        };
    Object around() : p() { Target.runner2.run(); return proceed();}
    Object around() : p() {
        /** @testcase privileged access to target private static variables in before advice (ok) */
        Util.event("around" +Target.privateStaticInt);
        return proceed();
    }
    Object around() : p() {
        /** @testcase privileged access to target private static variables from inner class inside around advice */
        Runnable inner = new Runnable() {
                public void run() {
                    Util.event("around run" +Target.privateStaticInt);
                }
            };
        inner.run();
        return proceed();
    }

    Object around() : p() { Target.runaround(); return proceed(); }
    /** @testcase privileged access to target private static variables from inner class inside introduced method */
    static void Target.runaround() {
        Runnable inner = new Runnable() {
                public void run() {
                    Util.event("around intro" +Target.privateStaticInt);
                }
            };
        inner.run();
    }
} 

privileged aspect PrivilegedAspectAroundNonStatic {
    /** @testcase privileged access to target private variables from inner class inside introduced method */
    before(Target t) : call(String Object.toString()) && target(t){ t.runbefore2(); }
    void Target.runbefore2() {
        Runnable inner = new Runnable() {
                public void run() {
                    Util.event("before instance" +privateInt);
                }
            };
        inner.run();
    }   
    /** @testcase privileged access to target private variables from inner class inside introduced method */
    Object around(Target t) : call(String Object.toString()) && target(t){ t.runbefore3(); return proceed(t); }
    void Target.runbefore3() {
        Runnable inner = new Runnable() {
                public void run() {
                    Util.event("around instance" +privateInt);
                }
            };
        inner.run();
    }
}