KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.log4j.Category;
25
26 import alt.jiapi.JiapiException;
27 import alt.jiapi.Rule;
28 import alt.jiapi.Runtime;
29 import alt.jiapi.reflect.Instruction;
30 import alt.jiapi.reflect.InstructionFactory;
31 import alt.jiapi.reflect.InstructionList;
32 import alt.jiapi.reflect.JiapiMethod;
33 import alt.jiapi.reflect.TryBlock;
34 import alt.jiapi.reflect.TryBlock.ExceptionHandler;
35
36 import alt.jiapi.reflect.instruction.OpcodeGroups;
37 import alt.jiapi.reflect.instruction.Opcodes;
38
39 /*
40  * NOTE :
41  * It is assumed that provider is able to update Exception tables
42  * without any assistance from instrumentors that are in chain!!!!
43  */

44
45 /**
46  * This Instrumentor forwards all the catch blocks of the method
47  * being instrumented.
48  *
49  * @author Mika Riekkinen
50  * @author Joni Suominen
51  * @version $Revision: 1.12 $ $Date: 2004/03/15 14:47:53 $
52  */

53 public class CatchInstrumentor extends AbstractInstrumentor {
54     private static Category log =
55         Runtime.getLogCategory(CatchInstrumentor.class);
56
57     private Rule[] rules;
58     private boolean reverseMatch = false;
59     private String JavaDoc[] resolutions;
60
61     /**
62      * Empty constructor.
63      */

64     public CatchInstrumentor() {
65     }
66
67
68     /**
69      * Scans current method for catch blocks.
70      *
71      * @param il InstructionList to scan
72      */

73     public void instrument(InstructionList il) {
74         ArrayList JavaDoc al = new ArrayList JavaDoc();
75         JiapiMethod m = il.getDeclaringMethod();
76         Instrumentation instrumentation = getInstrumentation();
77         InstructionList methodInstructions = m.getInstructionList();
78
79 // TryBlock[] tryBlocks = m.getTryBlocks();
80
// for (int i = 0; i < tryBlocks.length; i++) {
81
// ExceptionHandler[] eh = tryBlocks[i].getExceptionHandlers();
82

83 // for (int j = 0; j < eh.length; j++) {
84
// String targetName = eh[j].getName();
85

86 // if (match(targetName)) { // Resolution
87
// Instruction catchStart = eh[j].getHandlerStart();
88

89 // int idx = methodInstructions.indexOf(catchStart);
90
// InstructionList catchInstructions =
91
// methodInstructions.createView(idx);
92
// // eh[j].getInstructionList();
93

94 // instrumentation.setTargetName(targetName);
95

96 // analyze(catchInstructions, instrumentation);
97

98 // // Folowing line forwards catch block without its first
99
// // instruction, which byt default is ASTORE(_X)
100
// // If that is not the case, should we forward view from
101
// // offset 0.
102
// forward(catchInstructions.createView(1));
103
// }
104
// }
105
// }
106

107         throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
108     }
109
110
111     // Analyzes Catch instructions and tries to determine
112
// set of instructions, that is needed to get target
113
// (Exception in this case). Subsequent Instrumentors
114
// might need this piece of code.
115
// Other analyzations may be added later.
116
private boolean analyze(InstructionList il,
117                             Instrumentation instrumentation) {
118         InstructionList targetCode = null;
119         int index = 0;
120
121         if (il.size() > 0) {
122             index = il.indexOf(OpcodeGroups.REFERENCE_STORE_INSTRUCTIONS,0);
123             log.debug("First instruction during analyze is " + il.get(0));
124
125             if (index == 0) {
126                 InstructionFactory factory =
127                     il.getInstructionFactory();
128
129                 // First instruction should be the one that stores
130
// Exception instance into local variable<n>.
131
// If it is not, we might have already instrumented this part
132
// of the code.
133

134 // targetCode = il.createEmptyList();
135
if (true)
136                     throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
137                 Instruction i = il.get(0);
138                 
139
140                 int lvIdx = -1;
141                 switch(i.getOpcode()) {
142                 case Opcodes.ASTORE:
143                     // throws NullPointerException
144
//lvIdx = Integer.parseInt(i.getOperand().toString());
145
lvIdx = -1; // Force failure
146
break;
147                 case Opcodes.ASTORE_0:
148                     lvIdx = 0;
149                     break;
150                 case Opcodes.ASTORE_1:
151                     lvIdx = 1;
152                     break;
153                 case Opcodes.ASTORE_2:
154                     lvIdx = 2;
155                     break;
156                 case Opcodes.ASTORE_3:
157                     lvIdx = 3;
158                     break;
159                 default:
160                     // Not possible
161
}
162                 
163                 if (lvIdx >= 0) {
164                     if (true)
165                         throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
166 // targetCode.add(factory.getLocalVariable(lvIdx, "java.lang.Object"));
167

168                     log.debug("Analyze succeeded. Target code is " +
169                               targetCode + ", local variable is at index " +
170                               lvIdx);
171                 }
172             }
173         }
174
175         if (targetCode == null) {
176             log.debug("Analyze failed, setting target code to null");
177         }
178
179         instrumentation.setTargetCode(targetCode);
180
181         return targetCode != null;
182     }
183     
184     
185     public void setResolutions(String JavaDoc[] resolutions) {
186         this.resolutions = resolutions;
187
188         createRules(resolutions);
189     }
190
191
192
193     /**
194      * Compares given String to resolutions of this Instrumentor.
195      *
196      * @return true, if match is found in any of the resolutions.
197      */

198     protected boolean match(String JavaDoc name) {
199         if (name == null || rules == null) {
200             return false;
201         }
202
203         boolean b = false;
204         for (int i = 0; i < rules.length; i++) {
205             if (rules[i].match(name)) {
206                 b = true;
207                 
208                 break;
209             }
210         }
211
212         // If reverseMatch, toggle b
213
b = b ^ reverseMatch;
214
215         return b;
216     }
217
218
219     // Converts resolutions to rules.
220
private void createRules(String JavaDoc[] resolutions) {
221         if(resolutions == null) {
222             return;
223         }
224
225         this.rules = new Rule[resolutions.length];
226
227         for (int i = 0; i < resolutions.length; i++) {
228             try {
229                 rules[i] = new Rule(resolutions[i]);
230             }
231             catch(Exception JavaDoc e) {
232                 log.error(e.getMessage());
233             }
234         }
235     }
236 }
237
Popular Tags