KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > instrumentor > FieldAssignInstrumentor


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.instrumentor;
20
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23
24 //import java.util.logging.Logger;
25
import org.apache.log4j.Category;
26
27 import alt.jiapi.Runtime;
28 import alt.jiapi.reflect.InstructionFactory;
29 import alt.jiapi.reflect.InstructionList;
30 import alt.jiapi.reflect.JiapiClass;
31 import alt.jiapi.reflect.JiapiField;
32 import alt.jiapi.reflect.JiapiMethod;
33 import alt.jiapi.reflect.JiapiRuntimeException;
34 import alt.jiapi.reflect.Loader;
35
36
37 /**
38  * Creates a patch which sets a value for a field.
39  *
40  * @author Mika Riekkinen
41  * @author Joni Suominen
42  * @version $Revision: 1.12 $ $Date: 2004/03/15 14:47:53 $
43  */

44 public class FieldAssignInstrumentor extends AbstractInstrumentor {
45     //private static Logger log = Logger.getLogger(FieldAssignInstrumentor.class.getName());
46
private static Category log = Runtime.getLogCategory(FieldAssignInstrumentor.class);
47
48     private String JavaDoc fieldName;
49     private Object JavaDoc value;
50     private JiapiMethod getFieldValue;
51
52     public FieldAssignInstrumentor(String JavaDoc fieldName, Object JavaDoc value) {
53         this.fieldName = fieldName;
54         this.value = value;
55
56         Runtime.setFieldValue(fieldName, value);
57
58         try {
59             Loader l = new Loader();
60             JiapiClass jc = l.loadClass("alt.jiapi.Runtime");
61 // Class []params = new Class[] { String.class };
62
// getFieldValue = Runtime.class.getMethod("getFieldValue", params);
63
getFieldValue = jc.getDeclaredMethod("getFieldValue", new String JavaDoc[] {"java.lang.String"});
64         }
65         catch (Exception JavaDoc e) {
66             // not possible, but just in case
67
e.printStackTrace();
68             throw new RuntimeException JavaDoc(e.toString());
69         }
70     }
71
72     public void instrument(InstructionList il) {
73         // Native or abstract methods can't be instrumented.
74
int modifiers = il.getDeclaringMethod().getModifiers();
75         if (Modifier.isNative(modifiers) || Modifier.isAbstract(modifiers)) {
76             log.info("Skipping abstract or native method: " +
77                      il.getDeclaringMethod().getName());
78             forward(il);
79             return;
80         }
81
82         InstructionFactory factory =
83             il.getDeclaringMethod().getInstructionFactory();
84
85         JiapiClass jc = getCurrentClass();
86
87         JiapiField field = null;
88         try {
89             field = jc.getDeclaredField(fieldName);
90         }
91         catch (NoSuchFieldException JavaDoc e) {
92             throw new JiapiRuntimeException("No such field: " + e.getMessage());
93         }
94
95         // Get the initialized value from Runtime.
96
il.add(factory.pushConstant(fieldName));
97         il.add(factory.invoke(getFieldValue));
98         
99         // Cast the field.
100
il.add(factory.cast(field.getTypeName()));
101
102         // Set the field.
103
il.add(factory.setField(field));
104
105         forward(il);
106     }
107 }
108
Popular Tags