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
|
// attempts to be a comprehensive test of assert statements that have
// only intra-package behaviour.
import org.aspectj.testing.Tester;
public class AssertInOnePackage {
public static void main(String[] args) {
turnOnAssertions();
runTests();
}
// we first turn on assertions. This is run _before_ any other
// method is run, so the only classes that are loaded are
// AssertInOnePackage.
static void turnOnAssertions() {
ClassLoader cl = AssertInOnePackage.class.getClassLoader();
cl.setClassAssertionStatus("StaticInitializerOnHelper", true);
cl.setClassAssertionStatus("StaticInitializerOffHelper", false);
cl.setClassAssertionStatus("ConstructorOnHelper", true);
cl.setClassAssertionStatus("ConstructorOffHelper", false);
cl.setClassAssertionStatus("InnerStaticInitializerOnHelper", true);
cl.setClassAssertionStatus("InnerStaticInitializerOffHelper", false);
cl.setClassAssertionStatus("InnerStaticInitializerOnHelperI", true);
cl.setClassAssertionStatus("InnerStaticInitializerOffHelperI", false);
cl.setClassAssertionStatus("CycleSubOn", true);
cl.setClassAssertionStatus("CycleSubOff", false);
}
// In the following tests, the assignment to the static field
// translates into a putstatic bytecode, which, by section 5.5 of
// the JVM spec, will initialize the class.
static void runTests() {
check(true,
"static initializer should throw",
new Runnable() {
void run() { StaticInitializerOnHelper.x = 3; }
});
check(false,
"static initializer should not throw",
new Runnable() {
void run() { StaticInitializerOffHelper.x = 3; }
});
check(true,
"constructor should throw",
new Runnable() {
void pre() { ConstructorOnHelper.x = 3; }
void run() { new ConstructorOnHelper(); }
});
check(false,
"static initializer should not throw",
new Runnable() {
void pre() { ConstructorOffHelper.x = 3; }
void run() { new ConstructorOffHelper(); }
});
check(true,
"inner static initializer should throw",
new Runnable() {
void run() { InnerStaticInitializerOnHelper.Inner.x = 3; }
});
check(false,
"inner static initializer should not throw",
new Runnable() {
void run() { InnerStaticInitializerOffHelper.Inner.x = 3; }
});
check(true,
"inner static initializer of interface should throw",
new Runnable() {
void run() { InnerStaticInitializerOnHelperI.Inner.x = 3; }
});
check(false,
"inner static initializer of interface should not throw",
new Runnable() {
void run() { InnerStaticInitializerOffHelperI.Inner.x = 3; }
});
check(true,
"static initializer in cyclic should throw",
new Runnable() {
void run() { CycleSubOn.x = 3; }
});
check(true,
"static initializer in cyclic should always throw",
new Runnable() {
void run() { CycleSubOff.x = 3; }
});
}
static void check(boolean shouldThrow, String message, Runnable r) {
r.pre();
boolean threw = false;
try {
r.run();
} catch (AssertionError e) {
threw = true;
}
if (threw != shouldThrow) {
//System.err.println(message);
Tester.check(false, message);
}
r.post();
}
static class Runnable {
void pre() {}
void run() {}
void post() {}
}
}
// ------------------------------
// Asserts in a static initializer of a class
class StaticInitializerOnHelper {
static int x;
static {
assert false;
}
}
class StaticInitializerOffHelper {
static int x;
static {
assert false;
}
}
// ------------------------------
// Asserts in a constructor of a class. This stands in for all
// "normal" assertion, so I'm not going to bother writing asserts in
// other post-class-initialization contexts.
class ConstructorOnHelper {
static int x;
ConstructorOnHelper() {
assert false;
}
}
class ConstructorOffHelper {
static int x;
ConstructorOffHelper() {
assert false;
}
}
// ------------------------------
// Asserts in a static initializer of an inner class.
class InnerStaticInitializerOnHelper {
static class Inner {
static int x;
static {
assert false;
}
}
}
class InnerStaticInitializerOffHelper {
static class Inner {
static int x;
static {
assert false;
}
}
}
// ------------------------------
// Asserts in a static initializer of an inner class of an interface.
interface InnerStaticInitializerOnHelperI {
static class Inner {
static int x;
static {
assert false;
}
}
}
interface InnerStaticInitializerOffHelperI {
static class Inner {
static int x;
static {
assert false;
}
}
}
// ------------------------------
// Asserts in the subclass called in an initialization inversion
class CycleSuperOn {
static {
CycleSubOn.foo();
}
}
class CycleSubOn extends CycleSuperOn {
static int x;
static void foo() {
assert false;
}
}
class CycleSuperOff {
static {
CycleSubOff.foo();
}
}
class CycleSubOff extends CycleSuperOff {
static int x;
static void foo() {
assert false;
}
}
|