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
|
import org.aspectj.lang.reflect.*;
import java.util.*;
import java.lang.reflect.*;
public class ReflectOnCodeStyleITDs {
public static void main(String[] args) {
new ReflectOnCodeStyleITDs().runTests();
}
public void runTests() {
AjType<InterTypeDeclarations> ajType = AjTypeSystem.getAjType(InterTypeDeclarations.class);
testDeclaredInterTypeConstructors(ajType);
testInterTypeConstructors(ajType);
testDeclaredInterTypeMethods(ajType);
testInterTypeMethods(ajType);
testDeclaredInterTypeFields(ajType);
testInterTypeFields(ajType);
}
private void testDeclaredInterTypeConstructors(AjType<InterTypeDeclarations> ajType) {
InterTypeConstructorDeclaration[] itdcs = ajType.getDeclaredITDConstructors();
assertEquals(3,itdcs.length,"number of declared ITD constructors");
InterTypeConstructorDeclaration publicITDC = null;
InterTypeConstructorDeclaration defaultITDC = null;
InterTypeConstructorDeclaration privateITDC = null;
for(InterTypeConstructorDeclaration itdc : itdcs) {
if (Modifier.isPublic(itdc.getModifiers())) {
publicITDC = itdc;
} else if (Modifier.isPrivate(itdc.getModifiers())) {
privateITDC = itdc;
} else {
defaultITDC = itdc;
}
}
System.out.println(publicITDC);
System.out.println(defaultITDC);
System.out.println(privateITDC);
try {
InterTypeConstructorDeclaration shouldFind = ajType.getDeclaredITDConstructor(AjTypeSystem.getAjType(C.class), AjTypeSystem.getAjType(int.class));
System.out.println(shouldFind);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("getDeclaredITDConstructor failed");
}
try {
ajType.getDeclaredITDConstructor(AjTypeSystem.getAjType(I.class), AjTypeSystem.getAjType(int.class));
throw new RuntimeException("failed to fail in getting DeclaredITDConstructor #1");
} catch (NoSuchMethodException ex) {
// good!
}
try {
ajType.getDeclaredITDConstructor(AjTypeSystem.getAjType(C.class), AjTypeSystem.getAjType(String.class));
throw new RuntimeException("failed to fail in getting DeclaredITDConstructor #2");
} catch (NoSuchMethodException ex) {
// good!
}
}
private void testInterTypeConstructors(AjType<InterTypeDeclarations> ajType) {
InterTypeConstructorDeclaration[] itdcs = ajType.getITDConstructors();
assertEquals(1,itdcs.length,"number of ITD constructors");
System.out.println(itdcs[0]);
AjType<?> intClass = AjTypeSystem.getAjType(int.class);
try {
InterTypeConstructorDeclaration shouldFind = ajType.getITDConstructor(AjTypeSystem.getAjType(C.class), intClass, intClass, intClass);
System.out.println(shouldFind);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("getITDConstructor failed");
}
try {
ajType.getITDConstructor(AjTypeSystem.getAjType(C.class), AjTypeSystem.getAjType(int.class));
throw new RuntimeException("failed to fail in getting ITDConstructor");
} catch (NoSuchMethodException ex) {
// good!
}
}
private void testDeclaredInterTypeMethods(AjType<InterTypeDeclarations> ajType) {
InterTypeMethodDeclaration[] itdms = ajType.getDeclaredITDMethods();
assertEquals(6,itdms.length,"number of declared ITD methods");
Set s = new TreeSet(new Comparator<InterTypeMethodDeclaration>() {
public int compare(InterTypeMethodDeclaration m1, InterTypeMethodDeclaration m2) {
if (m1 == m2) return 0;
int vis = (m1.getModifiers() - m2.getModifiers());
if (vis != 0) return vis;
int name = (m1.getName().compareTo(m2.getName()));
if (name != 0) return name;
try {
return (
m1.getTargetType().getJavaClass().getName().compareTo(
m2.getTargetType().getJavaClass().getName())
);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
});
for (InterTypeMethodDeclaration itdm : itdms) { s.add(itdm); }
for (Object o : s) { System.out.println(o); }
try {
InterTypeMethodDeclaration shouldFind = ajType.getDeclaredITDMethod("getX",AjTypeSystem.getAjType(C.class));
System.out.println(shouldFind);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("getITDMethod failed");
}
try {
ajType.getDeclaredITDMethod("getP",AjTypeSystem.getAjType(C.class));
throw new RuntimeException("failed to fail in getting ITDMethod");
} catch (NoSuchMethodException ex) {
// good!
}
}
private void testInterTypeMethods(AjType<InterTypeDeclarations> ajType) {
InterTypeMethodDeclaration[] itdms = ajType.getITDMethods();
assertEquals(2,itdms.length,"number of ITD methods");
Set s = new TreeSet(new Comparator<InterTypeMethodDeclaration>() {
public int compare(InterTypeMethodDeclaration m1, InterTypeMethodDeclaration m2) {
if (m1 == m2) return 0;
int vis = (m1.getModifiers() - m2.getModifiers());
if (vis != 0) return vis;
int name = (m1.getName().compareTo(m2.getName()));
if (name != 0) return name;
try {
return (
m1.getTargetType().getJavaClass().getName().compareTo(
m2.getTargetType().getJavaClass().getName())
);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
});
for (InterTypeMethodDeclaration itdm : itdms) { s.add(itdm); }
for (Object o : s) { System.out.println(o); }
try {
InterTypeMethodDeclaration shouldFind = ajType.getITDMethod("getZ",AjTypeSystem.getAjType(C.class));
System.out.println(shouldFind);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("getITDMethod failed");
}
try {
ajType.getITDMethod("getX",AjTypeSystem.getAjType(C.class));
throw new RuntimeException("failed to fail in getting ITDMethod");
} catch (NoSuchMethodException ex) {
// good!
}
}
private void testDeclaredInterTypeFields(AjType<InterTypeDeclarations> ajType) {
InterTypeFieldDeclaration[] itdfs = ajType.getDeclaredITDFields();
assertEquals(6,itdfs.length,"number of declared ITD fields");
Set s = new TreeSet(new Comparator<InterTypeFieldDeclaration>() {
public int compare(InterTypeFieldDeclaration m1, InterTypeFieldDeclaration m2) {
if (m1 == m2) return 0;
int vis = (m1.getModifiers() - m2.getModifiers());
if (vis != 0) return vis;
int name = (m1.getName().compareTo(m2.getName()));
if (name != 0) return name;
try {
return (
m1.getTargetType().getJavaClass().getName().compareTo(
m2.getTargetType().getJavaClass().getName())
);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
});
for (InterTypeFieldDeclaration itdf : itdfs) { s.add(itdf); }
for (Object o : s) { System.out.println(o); }
try {
InterTypeFieldDeclaration shouldFind = ajType.getDeclaredITDField("x",AjTypeSystem.getAjType(C.class));
System.out.println(shouldFind);
} catch (NoSuchFieldException ex) {
throw new RuntimeException("getITDField failed");
}
try {
ajType.getDeclaredITDField("p",AjTypeSystem.getAjType(C.class));
throw new RuntimeException("failed to fail in getting ITDField");
} catch (NoSuchFieldException ex) {
// good!
}
}
private void testInterTypeFields(AjType<InterTypeDeclarations> ajType) {
InterTypeFieldDeclaration[] itdfs = ajType.getITDFields();
assertEquals(2,itdfs.length,"number of declared ITD fields");
Set s = new TreeSet(new Comparator<InterTypeFieldDeclaration>() {
public int compare(InterTypeFieldDeclaration m1, InterTypeFieldDeclaration m2) {
if (m1 == m2) return 0;
int vis = (m1.getModifiers() - m2.getModifiers());
if (vis != 0) return vis;
int name = (m1.getName().compareTo(m2.getName()));
if (name != 0) return name;
try {
return (
m1.getTargetType().getJavaClass().getName().compareTo(
m2.getTargetType().getJavaClass().getName())
);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
});
for (InterTypeFieldDeclaration itdf : itdfs) { s.add(itdf); }
for (Object o : s) { System.out.println(o); }
try {
InterTypeFieldDeclaration shouldFind = ajType.getITDField("z",AjTypeSystem.getAjType(C.class));
System.out.println(shouldFind);
} catch (NoSuchFieldException ex) {
throw new RuntimeException("getITDField failed");
}
try {
ajType.getITDField("x",AjTypeSystem.getAjType(C.class));
throw new RuntimeException("failed to fail in getting ITDField");
} catch (NoSuchFieldException ex) {
// good!
}
}
private void assertEquals(int x, int y, String msg) {
if (x != y) {
throw new RuntimeException(msg + " expecting '" + x + "' but was '" + y + "'");
}
}
}
|