aboutsummaryrefslogtreecommitdiffstats
path: root/tests/new/InitializerTest.java
blob: 31b5ca6983eaa1e3647d7ad1246a8dd109816cae (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
import org.aspectj.testing.Tester;

class C {
    public static String staticField = "initialized";
    
    public String state = "C-initialized";
    
    public C() {
        state = "C-constructed";
    }
}

class SubC extends C implements I {
    {
        state = "SubC-initialized";
    }
    
    public SubC() {
        state = "SubC-constructed";
    }
}

interface I {
    public static String s = "initialized";
    public static String s1 = new String("s1");
}


aspect A issingleton () {
    before(): staticinitialization(C) {
        Tester.checkEqual(C.staticField, null, "C.staticField");
    }
    after(): staticinitialization(C) {
        Tester.checkEqual(C.staticField, "initialized", "C.staticField");
        Tester.note("static initialized C");
    }
    after(): staticinitialization(SubC) {
        Tester.note("static initialized SubC");
    }
    /*
      before(): staticinitializations(I) {
      Tester.checkEqual(I.s, null, "I.s");
      }
      after(): staticinitializations(I) {
      Tester.checkEqual(I.s, "initialized", "I.s");
      Tester.note("static initialized I");
      }
    */
    
    
    before(C c): initialization(C.new(..)) && this(c) {
        Tester.checkEqual(c.state, null, "c.state");
    }

    before(C c): execution(C.new(..)) && this(c) {
    	// change from 1.0 is that fields aren't initialized at this point
        Tester.checkEqual(c.state, null, "c.state pre-constructor"); //"C-initialized", "c.state");
        Tester.note("constructed C");
    }

    after(C c): initialization(C.new(..)) && this(c) {
        Tester.checkEqual(c.state, "C-constructed", "c.state");
        Tester.note("initialized C");
    }
    

    before(SubC subc): initialization(SubC.new(..)) && this(subc) {
        Tester.checkEqual(subc.state, "C-constructed", "c.state");
    }
    before(SubC subc): execution(SubC.new(..)) && this(subc) {
    	// change from 1.0 is that fields aren't initialized at this point
        Tester.checkEqual(subc.state, "C-constructed", "c.state"); //"SubC-initialized", "c.state");
        Tester.note("constructed SubC");
    }

    after(SubC subc): initialization(SubC.new(..)) && this(subc) {
        Tester.checkEqual(subc.state, "SubC-constructed", thisJoinPoint.toString());
        Tester.note("initialized SubC");
    }
    
    before(I i): initialization(I.new()) && this(i) {
        Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
    }
//    before(I i): execution(I.new()) && this(i) {
//        Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
//        Tester.note("constructed I");
//    }
    after(I i): initialization(I.new()) && this(i) {
        Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
        Tester.note("initialized I");
    }
}

public class InitializerTest {
    public static void main(String[] args) {
        new SubC();
        Tester.check("initialized C");
        Tester.check("initialized SubC");
        Tester.check("constructed C");
        Tester.check("constructed SubC");

        Tester.check("initialized I");
        //Tester.check("constructed I");

        Tester.check("static initialized C");
        Tester.check("static initialized SubC");
        //XXX not doing static initializers in interfaces yet
        //XXX Tester.check("static initialized I");
    }
}