-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifyMethod.jrag
More file actions
279 lines (243 loc) · 11.8 KB
/
Copy pathModifyMethod.jrag
File metadata and controls
279 lines (243 loc) · 11.8 KB
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
/*
* DominoJ is an extension to Java for supporting method slots,
* and this project is an implementation of its compiler built on top of JastAddJ.
*
* Copyright (c) 2011-, YungYu Zhuang. All rights reserved.
*
* The contents of this file are licensed under the modified BSD License
* (please see the LICENSE file for the full text).
*/
/*
* Method declarations are modified for method slots when collectErrors.
*/
aspect ModifyMethod {
/*
* allow declaring non-abstract method without body as empty slot
*/
refine NameCheck public void MethodDecl.nameCheck() {
if(!isAbstract() && !hasBlock()) {
Block empty = new Block();
if(!isVoid()) empty.addStmt(new ReturnStmt(getTypeAccess().type().getDefaultValue()));
setBlock(empty);
}
if(!done()) modifyParms();
refined();
}
refine Modifiers public void TypeDecl.checkModifiers() {
if(!isEnumDecl()) {
Iterator iter = methodsIterator();
while(iter.hasNext()) {
MethodDecl md = (MethodDecl)iter.next();
if(!md.done()) {
//System.out.println("checkModifiers of " + name() + ": modifyParms " + md.signature());
md.modifyParms();
flushCache();
}
}
if(isClassDecl()) {
iter = constructors().iterator();
while(iter.hasNext()) {
ConstructorDecl cons = (ConstructorDecl)iter.next();
if(!cons.done()) {
//System.our.println("checkModifiers of " + name() + ": modifyParms " + cons.signature());
cons.modifyParms();
flushCache();
}
}
ClassDecl cd = (ClassDecl)this;
while(cd.hasSuperclass()) {
cd = cd.superclass();
cd.flushCache();
}
}
}
refined();
}
public void MethodDeclSubstituted.modifyParms() {
if(!sourceMethodDecl().done()) {
//System.out.println("modifyParms sourceMethodDecl of " + hostType().name() + ":" + signature());
sourceMethodDecl().modifyParms();
}
}
/*
* transform a method into a method slot.
* 1. check and import the necessary packages.
* 2. add parameters for keywords.
* 3. copy the method declaration to a new method $impl.
* 4. create a closure for the method body (the default closure).
* 5. create a closure for the method slot itself.
* 6. prepare an array and its handler methods for the method slot.
* 7. append the default closure to the method slot in the initializer.
* 8. iterate and execute the closures in the method slot.
*/
public void MethodDecl.modifyParms() {
//System.out.println("check parameters for keywords. " + hostType().name() + ":" + signature());
//System.out.println(parent.dumpTree());
//System.out.println("----" + signature());
//System.out.println(getBlock());
//System.out.println("----" + signature());
boolean hasExceptions = getNumException()>0;
String closure = hasExceptions ? "Closure" : "ClosureNoException";
TypeDecl td = hostType();
TypeDecl ctype = td.lookupType("dominoj", "Closure");
//System.out.println("GenericMethodDecl?" + (this instanceof GenericMethodDecl) + ", MethodDeclSubstituted?" + (this instanceof MethodDeclSubstituted) + ", isMethodSlot?" + isMethodSlot() + ", generated?" + generated + ", isDummy?" + isDummy());
if(!name().endsWith("$impl")
&& !td.instanceOf(ctype)
&& !td.isEnumDecl()
&& !(name().equals("main") && isStatic())
&& isMethodSlot()
&& !generated
&& !(!td.compilationUnit().fromSource() && isParameterModified())
&& !isDummy()) {
td.importPkgs();
//ClassDecl cd = td instanceof ClassDecl ? (ClassDecl)td : null;
//System.out.println("Class: " + cd.name() + " " + cd.hashCode());
//System.out.println("Method: " + name() + " " + hashCode());
//System.out.println("entered for adding parameters..." + td.name() + ":" + signature());
MethodDecl nmd = fullCopy();
TypeDecl type = getTypeAccess().type();
String rvar = null;
int idx = 0;
if(!isVoid()) {
// add parameter $retval
nmd.insertParameter(new ParameterDeclaration(new Modifiers(), type.createQualifiedAccess(), "$retval"), idx++);
rvar = "$retval";
}
// add parameter $caller, $predecessor, $self, $proceed, and $impl
TypeDecl otype = td.lookupType("java.lang", "Object");
nmd.insertParameter(new ParameterDeclaration(new Modifiers(), otype.createQualifiedAccess(), "$caller"), idx++);
nmd.insertParameter(new ParameterDeclaration(new Modifiers(), otype.createQualifiedAccess(), "$predecessor"),idx++);
nmd.insertParameter(new ParameterDeclaration(new Modifiers(), otype.createQualifiedAccess(), "$self"), idx++);
nmd.insertParameter(new ParameterDeclaration(new Modifiers(), ctype.createQualifiedAccess(), "$proceed"), idx++);
nmd.insertParameter(new ParameterDeclaration(new Modifiers(), ctype.createQualifiedAccess(), "$impl"), idx++);
nmd.flushCache();
setAnnotation(lookupType("dominoj", "Dummy"), true);
flushCache();
td.addMethodDecl(nmd);
td.flushCache();
if(td.isClassDecl() || td.isInterfaceDecl()) {
// copy only visibility modifiers and static
Modifiers mods = new Modifiers();
if(isPublic()) mods.addModifier(new Modifier("public"));
else if(isProtected()) mods.addModifier(new Modifier("protected"));
else mods.addModifier(new Modifier("private"));
if(isStatic()) mods.addModifier(new Modifier("static"));
// id for overridden methods
String mid = nmd.methodId();
// id for non-overridden methods
String sid = nmd.methodId(false, true);
if(td.isClassDecl() && !isAbstract()) {
// rename original method
String implid = sid + "$impl";
MethodDecl imd = nmd.fullCopy();
imd.setID(implid);
imd.setModifiers(mods);
td.addMethodDecl(imd);
td.flushCache();
// create a closure for the method
td.createMemberField(sid+"$c", "dominoj", "Closure", mods.fullCopy(),
nmd.generateClosureInit(nmd.name(), type, hasExceptions ? getExceptionList() : null));
// create a closure for the original method. it is assigned in the iterator
td.createMemberField(sid+"$impl"+"$c", "dominoj", "Closure", mods.fullCopy(), new NullLiteral("null"));
// prepare an array to store closure objects
String list = sid + "$slot";
td.createMemberField(list, "java.util", "ArrayList", mods.fullCopy(), new NullLiteral("null"));
// create iterator which does the initialization when it is first called.
// initializing it in the instance initializer cannot handle the case that
// the overriding method is called in the constructor of its superclass.
nmd.createSlotGetterMethod(sid);
}
// create array handlers
String getter = sid + "$getSlot";
nmd.createArrayHandlerMethod(mid+"$after", getter, "dominoj", "Closure", true, -1);
nmd.createArrayHandlerMethod(mid+"$before", getter, "dominoj", "Closure", true, 0);
nmd.createArrayHandlerMethod(mid+"$remove", getter, "dominoj", "Closure", false, -1);
nmd.createArrayClearMethod( mid+"$clear", getter);
if(td.isClassDecl() && !isAbstract()) {
Block mb = new Block();
// add invocation code
List<Stmt> invocation = nmd.generateInvocationStmts(sid, hasExceptions ? getExceptionList() : null);
for(int i=0; i<invocation.getNumChild(); i++) {
mb.addStmt(invocation.getChild(i));
}
nmd.setBlock(mb);
Block empty = new Block();
if(!isVoid()) empty.addStmt(new ReturnStmt(getTypeAccess().type().getDefaultValue()));
setBlock(empty);
flushCaches();
}
td.flushCache();
}
nmd.setAnnotation(lookupType("dominoj", "MethodSlot"), true);
nmd.flushCaches();
//System.out.println("---->" + td.name() + ":" + nmd.signature() + " " + nmd.isMethodSlot());
//System.out.println("---->" + td.name() + ":" + signature() + " " + isDummy());
//System.out.println(mb);
//System.out.println("----" + signature());
//System.out.println(td.compilationUnit());
}
}
refine GenericsCodegen public void MethodDecl.transformation() {
if(!isDummy()) refined();
}
refine public void EnumDecl.addBodyDecl(BodyDecl node) {
if(node instanceof MethodDecl) {
((MethodDecl)node).generated = true;
}
refined(node);
}
/* Enum-related handlings are defined as lazy, but they are flushed by their hostType. */
private HashMap TypeDecl.enumMap = null;
public Object TypeDecl.getEnum(ASTNode source, String name) {
ArrayList key = new ArrayList(2);
key.add(source);
key.add(name);
if(enumMap == null || !enumMap.containsKey(key)) return null;
return enumMap.get(key);
}
public void TypeDecl.addEnum(ASTNode source, String name, Object decl) {
ArrayList key = new ArrayList(2);
key.add(source);
key.add(name);
if(enumMap == null) enumMap = new HashMap();
enumMap.put(key, decl);
}
refine EnumsCodegen eq TypeDecl.createEnumMethod(TypeDecl enumDecl) {
MethodDecl m = (MethodDecl)getEnum(enumDecl, "method");
if(m != null) return m;
m = refined(enumDecl);
m.generated = true;
addEnum(enumDecl, "method", m);
return m;
}
refine EnumsCodegen eq TypeDecl.createEnumArray(TypeDecl enumDecl) {
FieldDeclaration f = (FieldDeclaration)getEnum(enumDecl, "array");
if(f != null) return f;
f = refined(enumDecl);
addEnum(enumDecl, "array", f);
return f;
}
refine EnumsCodegen eq TypeDecl.createEnumIndex(EnumConstant e) {
Integer i = (Integer)getEnum(e, "index");
if(i != null) return i;
i = refined(e);
addEnum(e, "index", new Integer(i));
return i;
}
/* after the cache of a TypeDecl is flushed, its arrayType might be created twice and fail to be compared */
private TypeDecl TypeDecl.arrayTypeDecl = null;
refine GenericsArrays eq TypeDecl.arrayType() {
if(arrayTypeDecl == null) {
arrayTypeDecl = refined();
}
return arrayTypeDecl;
}
refine InnerClasses public void TypeDecl.addAccessor(ASTNode source, String name, ASTNode accessor) {
refined(source, name, accessor);
if(accessor instanceof MethodDecl) {
MethodDecl md = (MethodDecl)accessor;
//System.out.println("TypeDecl.addAccessor: " + md.signature());
md.generated = true;
}
}
}