summaryrefslogtreecommitdiffstats
path: root/docs/sandbox/common/language/Context.java
blob: f0021a28ada439f059f54dd7afcdf518b1ba8a95 (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
package language;

public class Context {
    public static void main(String[] argList) {
        new C().run();
    }
}

class C {
    static int MAX = 2;
    int i;
    C() {
        i = 1;
    }
    public void run() {
        try {
            more();
        } catch (MoreError e) {
            // log but continue
            System.out.println(e.getMessage());
        }
    }

    private void more() {
        i++;
        if (i >= MAX) {
            i = 0;
            throw new MoreError();
        }
    }
    static class MoreError extends Error {
        MoreError() {
            super("was too much!");
        }
    }
}

/** @author Erik Hilsdale, Wes Isberg */
aspect A {

    // START-SAMPLE language-fieldSetContext Check input and result for a field set.
    /** 
     * Check input and result for a field set.
     */
    void around(int input, C targ) : set(int C.i) 
            && args(input) && target(targ) {
        String m = "setting C.i="  + targ.i  + " to " + input;
        System.out.println(m);
        proceed(input, targ);
        if (targ.i != input) {
            throw new Error("expected " + input);
        }
    }
    // END-SAMPLE language-fieldSetContext

    // START-SAMPLE language-handlerContext Log exception being handled
    /** 
     * Log exception being handled
     */
    before (C.MoreError e) : handler(C.MoreError) 
            && args(e) && within(C) {
        System.out.println("handling " + e);
    }
    // END-SAMPLE language-handlerContext

    // See Initialization.java for constructor call,
    // constructor execution, and {pre}-initialization
    
}