aboutsummaryrefslogtreecommitdiffstats
path: root/tests/new/NegativeSourceLocation.java
blob: f27bf3925d36b7d5ac7a055ae59a782a6762cd96 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import org.aspectj.lang.*;
import org.aspectj.lang.reflect.*;

import org.aspectj.testing.*;

/** 
 * @testcase PR#525 validate presence/absence of thisEnclosingJoinPointStaticPart  
 * @testcase PR#525 validate SourceLocation
 */
public class NegativeSourceLocation {

    public static void main(String[] args) {
        Signal.expect(Const.EXPECTED);
        TargetClass target = new TargetClass();
        // run from outside code the compiler controls
        Thread t = new Thread(target, "NegativeSourceLocation");
        t.start();
        int i = 0;
        // todo: use Timer to add interrupt?
        while ((10 > i++) && t.isAlive()) {
            try { t.join(); }
            catch (InterruptedException e) {}
        }
        Signal.checkAll();
    }
}

/** indirection for test utility */
class Signal {
    public static final void failed(String s) {
        //Thread.currentThread().dumpStack();
        Tester.checkFailed(s);
    }
    public static final void found(JoinPoint.StaticPart s) {
        Tester.event(s.toString());
    }
    public static final void found(String s) {
        Tester.event(s);
    }
    public static final void checkAll() {
        Tester.checkAllEvents();
    }
    public static final void expect(String[] sra) {
        Tester.expectEventsInString(sra);
    }
} // class Signal

/** TargetClass has most every join point, provoked by initializatin and run() */
class TargetClass implements Runnable {
     TargetClass() {}
     static String staticString = "two";     // jp has -1 source location
     static {
          staticString = "one";
          String s = staticString + "asdf";
     }
     static {
          staticString = "three";
     }
     String string = "bar"; 
     public static void staticRun() {        // execute
          staticString = "foo";              // set - static var
          String s = staticString;           // get - static var
          s = s + "ss";
          staticString = s + "u";            // set - instance var
          TargetClass u = new TargetClass(); // initialization
          staticString = u.toString();                 
     }
     public void run() {                     // execute - no thisEnclosingJoinPoint when called from Thread.start()
         boolean doNotOptimize = (staticString != null);
         if (doNotOptimize) internalRun();
     }
     private void internalRun() {            // execute
          staticString = "foo";              // set - static var
          staticRun();                       // call static
          String s = staticString;           // get - static var
          String t = string;                 // get - instance var
          s = s + t;
          string = s + t;                    // set - instance var
          final Error e = new Error("caught here");
          try {
                throw e;
          } catch (Error er) {               // handler
                if (er != e) {
                     Signal.failed("caught Error=" + er);
                } else {
                     Signal.found("caught");
                }
          }
     }
} // class TargetClass

/**
 * This Aspect attempts to specify join points that have enclosing
 * join points and whether the source locations are valid.
 * It fails in using only pointcuts. 
 * This is just a first cut.  I (wes) have an AroundAll
 * which does this tracking...
 */
aspect Aspect {

    // ------------------------------- pointcuts select logical sets of join points
    // 1.1 includes StringBuffer calls that weren't in 1.0
    pointcut allTargetJoinPoints() 
        : within(TargetClass) && 
        	!call(* StringBuffer.*(..)) && !call(StringBuffer.new(..))
        	&& !call(* String.valueOf(Object));

    /** these have no enclosing join point */
    pointcut noEnclosingJoinPoint() 
        : ((call(public void TargetClass.run()))   // called from Thread.start() (caller-side, but has callee-side?)
           //|| staticinitialization(TargetClass)    // the enclosing jp here is itself
           );
           // || initialization(TargetClass.new())   
           // || execution(TargetClass.new())        // todo: expect it to be self like in methods?

     
    /** these have enclosing join points */
    pointcut hasEnclosingJoinPoint() 
        : allTargetJoinPoints() 
        && !(noEnclosingJoinPoint()) 
        ;

    /** expecting an enclosing join point different from thisJoinPoint */
    pointcut enclosingDiffers() 
        :  get(* TargetClass.*)
            || set(* TargetClass.*)
            || call(* TargetClass.*(..))
            || handler(Error)
        ;

    pointcut hasDifferentEnclosingJoinPoint() 
        : hasEnclosingJoinPoint()
        && enclosingDiffers()
        ;

    pointcut hasSameEnclosingJoinPoint() 
        : hasEnclosingJoinPoint()
        && (!enclosingDiffers());

    /** synthetic join points have no source location */
    pointcut syntheticJoinPoints() 
        : staticinitialization(TargetClass) 
        || initialization(TargetClass.new(UnknownConstructor))
        ;

    pointcut joinPointHasValidSourceLocation()
        : allTargetJoinPoints() 
        && (!syntheticJoinPoints())
        //&& if(expectSourceLocation(thisJoinPointStaticPart))
        ;

    pointcut enclosingJoinPointHasValidSourceLocation() // todo: cannot specify
        : hasEnclosingJoinPoint() 
        && (!syntheticJoinPoints()) 
        //&& if(expectSourceLocation(thisEnclosingJoinPointStaticPart))
        ;

    // ---------------------- advice applies invariants to each logical set of join points
    /** @testcase all join points have non-null thisJoinPoint and thisJoinPointStaticPart */
    before(): allTargetJoinPoints() {
        Signal.found("before AllTargetJoinPoints "  + thisJoinPointStaticPart);
        //System.err.println(thisJoinPointStaticPart + " at " + thisJoinPointStaticPart.getSourceLocation());
        String test = "all join points have non-null thisJoinPointStaticPart";
        if (null == thisJoinPoint) {
            Signal.failed(test + " failed with null thisJoinPoint: " + thisJoinPointStaticPart);
        }
        if (null == thisJoinPointStaticPart) {
            Signal.failed(test + " failed with null thisJoinPointStaticPart: " + thisJoinPoint);
        }
    }

    /** @testcase non-null thisEnclosingStaticJoinPoint at certain join points */
    before() : hasEnclosingJoinPoint() {
        String test = "failed (most join points have non-null thisEnclosingStaticJoinPoint) ";
        if (null == thisEnclosingJoinPointStaticPart) {
            String jpName = thisJoinPointStaticPart.toString();
            Signal.failed(test + render(thisJoinPointStaticPart, thisEnclosingJoinPointStaticPart));
            //if (!jpName.equals("execution(TargetClass.<init>)")) { // todo: unable to specify this...
        }
    }

    /** @testcase non-null thisEnclosingStaticJoinPoint at join points (except for tested exceptions) */
    before() : hasDifferentEnclosingJoinPoint() {
        String test = "join points with different thisEnclosingStaticJoinPoint";
        if (thisEnclosingJoinPointStaticPart != thisEnclosingJoinPointStaticPart) {
            Signal.failed(test + " different static part : " + thisJoinPointStaticPart);
        }
    }

    /** @testcase expecting valid source locations */
    before() : joinPointHasValidSourceLocation() {
        if (null == thisJoinPointStaticPart) {
            Signal.failed("null thisJoinPointStaticPart");
        } else {
            checkSourceLocation(thisJoinPointStaticPart);
        }
    }

    /** @testcase expecting valid source locations in enclosing join point */
    before() : enclosingJoinPointHasValidSourceLocation() {
        if (null == thisEnclosingJoinPointStaticPart) {
            Signal.failed("null thisEnclosingJoinPointStaticPart in " + thisJoinPointStaticPart);
        } else {
            checkSourceLocation(thisEnclosingJoinPointStaticPart);
        }
    }

    /** @testcase non-null thisEnclosingJoinPointStaticPart in static initializer if invoked within a join point?  */
    before() : staticinitialization(AnotherTargetClass) {    
        String test = "static initializer join points have non-null thisJoinPointStaticPart when invoked from CCC";
        if (null == thisJoinPoint) {
            Signal.failed(test + " failed with null thisJoinPoint: " + thisJoinPointStaticPart);
        }
        if (null == thisJoinPointStaticPart) {
            Signal.failed(test + " failed with null thisJoinPointStaticPart: " + thisJoinPoint);
        }
        Signal.found("staticinitialization(AnotherTargetClass))");
        //Signal.found(thisJoinPointStaticPart); // todo: relying on formatting of toString() - fix
    }

    /** @testcase no call from outside CCC has thisEnclosingJoinPointStaticPart (possible mistake) */
    before() : noEnclosingJoinPoint() {
        Signal.found("before noEnclosingJoinPoint "  + thisJoinPointStaticPart);
        if (null != thisEnclosingJoinPointStaticPart) {
                Signal.failed("unexpected non-null thisEnclosingJoinPointStaticPart: "
                          + thisEnclosingJoinPointStaticPart + " from " + thisJoinPointStaticPart);
        }
    }

    static String render(JoinPoint.StaticPart jp, JoinPoint.StaticPart ejp) {
        StringBuffer sb = new StringBuffer();
        sb.append("thisJoinPoint: ");
        sb.append(null == jp ? "null" : jp.toString());
        sb.append("thisEnclosingJoinPoint: ");
        sb.append(null == ejp ? "null" : ejp.toString());
        return sb.toString();
    }

    void checkSourceLocation(JoinPoint.StaticPart jp) { // todo: provide caller context?
        checkSourceLocation(jp.getSourceLocation(), jp.toString());
    }

    /** aborted attempt to check jp by name for jp without enclosing */
    private static boolean expectSourceLocation(JoinPoint.StaticPart jp) {
        if (null == jp) {
            return false;
        } else {
            String name = jp.toString();
            if (-1 != name.indexOf("TargetClass.<init>")) {
                return false;
            }
        }
        return true; // todo: overinclusive
    }

    private boolean inInitCode(JoinPoint.StaticPart jp) {
        return (-1 != jp.toString().indexOf("<init>"));
    }

    void checkSourceLocation(SourceLocation sl, String context) {
        if (sl == null) {
            Signal.failed(context + "null SourceLocation");
        } else {
            int i = sl.getLine();
            if (0 > i) {
                Signal.failed(context + " line<0: " + i);
            }
            // 1.1 doesn't provide column info
//            i = sl.getColumn();
//            if (0 > i) {
//                Signal.failed(context + " column<0: " + i);
//            }
        }
    }
} // Aspect

/** more readable to put expected messages at end of file */
class Const {
    // todo: EXPECTED will break if JoinPoint.StaticPart.toString() changes
    public static final String[] EXPECTED = new String[] 
    {
          "before AllTargetJoinPoints staticinitialization(TargetClass.<clinit>)"
        , "before AllTargetJoinPoints set(String TargetClass.staticString)"
        , "before AllTargetJoinPoints get(String TargetClass.staticString)"
        , "before AllTargetJoinPoints get(String TargetClass.staticString)"
        , "before AllTargetJoinPoints set(String TargetClass.staticString)"
        , "before AllTargetJoinPoints set(String TargetClass.staticString)"
        , "before AllTargetJoinPoints preinitialization(TargetClass())"
        , "before AllTargetJoinPoints initialization(java.lang.Runnable())"
        //, "before AllTargetJoinPoints execution(java.lang.Runnable())"
        , "before AllTargetJoinPoints initialization(TargetClass())"
        //, "before AllTargetJoinPoints execution(TargetClass.<init>)"
        , "before AllTargetJoinPoints set(String TargetClass.string)"
        , "before AllTargetJoinPoints execution(TargetClass())"
        , "before AllTargetJoinPoints execution(void TargetClass.run())"
        , "before AllTargetJoinPoints call(void TargetClass.internalRun())"
        , "before AllTargetJoinPoints execution(void TargetClass.internalRun())"
        , "before AllTargetJoinPoints set(String TargetClass.staticString)"
        , "before AllTargetJoinPoints call(void TargetClass.staticRun())"
        , "before AllTargetJoinPoints execution(void TargetClass.staticRun())"
        , "before AllTargetJoinPoints set(String TargetClass.staticString)"
        , "before AllTargetJoinPoints get(String TargetClass.staticString)"
        , "before AllTargetJoinPoints set(String TargetClass.staticString)"
        , "before AllTargetJoinPoints call(TargetClass())"
        , "before AllTargetJoinPoints preinitialization(TargetClass())"        
        , "before AllTargetJoinPoints initialization(TargetClass())"
        , "before AllTargetJoinPoints initialization(java.lang.Runnable())"
        //, "before AllTargetJoinPoints execution(java.lang.Runnable())"
        //, "before AllTargetJoinPoints execution(TargetClass.<init>)"
        , "before AllTargetJoinPoints set(String TargetClass.string)"
        , "before AllTargetJoinPoints execution(TargetClass())"
        , "before AllTargetJoinPoints call(String java.lang.Object.toString())"
        , "before AllTargetJoinPoints set(String TargetClass.staticString)"
        , "before AllTargetJoinPoints get(String TargetClass.staticString)"
        , "before AllTargetJoinPoints get(String TargetClass.string)"
        , "before AllTargetJoinPoints set(String TargetClass.string)"
        , "before AllTargetJoinPoints call(java.lang.Error(String))"
        , "before AllTargetJoinPoints handler(catch(Error))"
        , "before AllTargetJoinPoints call(void Signal.found(String))"
        , "caught"
    };

}