KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > event > FieldEventInstrumentor


1 package alt.jiapi.event;
2
3 import alt.jiapi.reflect.Instruction;
4 import alt.jiapi.reflect.InstructionFactory;
5 import alt.jiapi.reflect.InstructionList;
6 import alt.jiapi.reflect.JiapiClass;
7 import alt.jiapi.reflect.JiapiField;
8 import alt.jiapi.reflect.JiapiMethod;
9 import alt.jiapi.reflect.Signature;
10
11 import alt.jiapi.reflect.instruction.FieldAccess;
12 import alt.jiapi.reflect.instruction.OpcodeGroups;
13 import alt.jiapi.reflect.instruction.Opcodes;
14
15
16 /**
17  * Class MethodEventInstrumentor.
18  *
19  * @author Mika Riekkinen
20  */

21 class FieldEventInstrumentor extends EventInstrumentor {
22     FieldEventInstrumentor(FieldEventProducer fep) {
23         super(fep);
24     }
25
26     public void instrument(JiapiMethod jm) {
27         InstructionList il = jm.getInstructionList();
28         InstructionFactory factory = il.getInstructionFactory();
29         JiapiClass fep = getEventProducer();
30         JiapiMethod fieldGet = null;
31         JiapiMethod fieldSet = null;
32         JiapiField jiapiField = getEventProducerField();
33
34         try {
35             fieldGet =
36                 fep.getDeclaredMethod("fieldGet",
37                                       new String JavaDoc[] { "java.lang.Object",
38                                                      "java.lang.String" });
39             fieldSet =
40                 fep.getDeclaredMethod("fieldSet",
41                                       new String JavaDoc[] { "java.lang.Object",
42                                                      "java.lang.String" });
43         }
44         catch(Exception JavaDoc e) {
45             System.out.println("ERROR: " + e);
46         }
47
48         //System.out.println("Instrumenting " + il.getDeclaringMethod());
49

50         int idx = -1;
51         while ((idx = il.indexOf(OpcodeGroups.FIELD_ACCESS_INSTRUCTIONS, idx+1)) != -1) {
52             FieldAccess ins = (FieldAccess)il.get(idx);
53             // Do not instrument on __jiapi_fields
54
if (ins.getName().startsWith("__jiapi_field")) {
55                 continue;
56             }
57
58             //System.out.println(" found " + ins);
59
InstructionList invokeList = il.createEmptyList();
60
61             switch(ins.getOpcode()) {
62             case Opcodes.PUTFIELD:
63             case Opcodes.GETFIELD:
64                 invokeList.add(factory.getField(jiapiField));
65                 invokeList.add(factory.pushThis());
66                 invokeList.add(factory.pushConstant(ins.getFieldName()));
67                 if (ins.getOpcode() == Opcodes.GETFIELD) {
68                     invokeList.add(factory.invoke(fieldGet));
69                 }
70                 else {
71                     invokeList.add(factory.invoke(fieldSet));
72                 }
73                 break;
74             case Opcodes.PUTSTATIC:
75             case Opcodes.GETSTATIC:
76                 invokeList.add(factory.getField(jiapiField));
77                 invokeList.add(factory.getField(jiapiField)); // BUG: Class
78
invokeList.add(factory.pushConstant(ins.getFieldName()));
79                 if (ins.getOpcode() == Opcodes.GETSTATIC) {
80                     invokeList.add(factory.invoke(fieldGet));
81                 }
82                 else {
83                     invokeList.add(factory.invoke(fieldSet));
84                 }
85                 break;
86             }
87
88             // Insert list after field access operation
89
il.insert(idx + 1, invokeList);
90
91             // Next index. Skip Instructions created above.
92
idx += invokeList.size();
93         }
94     }
95 }
96
Popular Tags