blob: 18c194a3b3dd049e0d955b4e218032d81f61b293 (
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
|
package walter;
import io.devnulllabs.openjava.ptree.*;
import io.devnulllabs.openjava.ptree.ModifierList;
import io.devnulllabs.openjava.mop.*;
public class multiTrig instantiates Metaclass extends OJClass {
private static final String MULTI_TRIG = "multi_trig";
private final int triggerValue = 10;
public static boolean isRegisteredModifier(String keyword) {
if (keyword.equals(MULTI_TRIG))
return true;
return OJClass.isRegisteredModifier(keyword);
}
public void translateDefinition() throws MOPException {
OJMethod[] methods = getDeclaredMethods();
for (int i = 0; i < methods.length; ++i) {
if (!methods[i].getModifiers().has(MULTI_TRIG))
continue;
String name = methods[i].getName(); // is a multi-trigged method
ModifierList ml = new ModifierList(ModifierList.PRIVATE);
TypeName tn = new TypeName("int");
/* inserts a field for counting the method calls
private int <method name>Counter = 0 */
String counterName = name + "Counter";
VariableInitializer vi = Literal.makeLiteral(0);
FieldDeclaration fd = new FieldDeclaration(ml, tn, counterName, vi);
OJField field = new OJField(getEnvironment(), this, fd);
addField(field);
/* inserts a field keeping the trigger value
private int <method name>Trigger = 10 */
String triggerName = name + "Trigger";
vi = Literal.makeLiteral(triggerValue);
fd = new FieldDeclaration(ml, tn, triggerName, vi);
field = new OJField(getEnvironment(), this, fd);
addField(field);
/* inserts a precondition on the execution of the method body
if (<method name>Counter == <method name>Trigger) {
and a post action
<method body>
<method name>Counter = 0;
} <method name>Counter++; */
StatementList newBody =
makeStatementList(
methods[i].getEnvironment(),
"if ("
+ counterName
+ " == "
+ triggerName
+ ") {"
+ methods[i].getBody().toString()
+ counterName
+ "=0;}; "
+ counterName
+ "++;");
methods[i].setBody(newBody);
}
}
}
|