1 30 31 package oracle.toplink.libraries.asm.util; 32 33 import oracle.toplink.libraries.asm.CodeVisitor; 34 import oracle.toplink.libraries.asm.Label; 35 import oracle.toplink.libraries.asm.Attribute; 36 import oracle.toplink.libraries.asm.Type; 37 38 import java.util.HashMap ; 39 40 46 47 public class TraceCodeVisitor extends PrintCodeVisitor { 48 49 53 54 protected final CodeVisitor cv; 55 56 59 60 private final HashMap labelNames; 61 62 68 69 public TraceCodeVisitor (final CodeVisitor cv) { 70 this.cv = cv; 71 this.labelNames = new HashMap (); 72 } 73 74 public void printInsn (final int opcode) { 75 buf.append(" ") 76 .append(OPCODES[opcode]) 77 .append("\n"); 78 79 if (cv != null) { 80 cv.visitInsn(opcode); 81 } 82 } 83 84 public void printIntInsn (final int opcode, final int operand) { 85 buf.append(" ") 86 .append(OPCODES[opcode]) 87 .append(" ").append(operand) 88 .append("\n"); 89 90 if (cv != null) { 91 cv.visitIntInsn(opcode, operand); 92 } 93 } 94 95 public void printVarInsn (final int opcode, final int var) { 96 buf.append(" ") 97 .append(OPCODES[opcode]) 98 .append(" ") 99 .append(var) 100 .append("\n"); 101 102 if (cv != null) { 103 cv.visitVarInsn(opcode, var); 104 } 105 } 106 107 public void printTypeInsn (final int opcode, final String desc) { 108 buf.append(" ") 109 .append(OPCODES[opcode]) 110 .append(" ") 111 .append(desc) 112 .append("\n"); 113 114 if (cv != null) { 115 cv.visitTypeInsn(opcode, desc); 116 } 117 } 118 119 public void printFieldInsn ( 120 final int opcode, 121 final String owner, 122 final String name, 123 final String desc) 124 { 125 buf.append(" ") 126 .append(OPCODES[opcode]) 127 .append(" ") 128 .append(owner) 129 .append(" ") 130 .append(name) 131 .append(" ") 132 .append(desc) 133 .append("\n"); 134 135 if (cv != null) { 136 cv.visitFieldInsn(opcode, owner, name, desc); 137 } 138 } 139 140 public void printMethodInsn ( 141 final int opcode, 142 final String owner, 143 final String name, 144 final String desc) 145 { 146 buf.append(" ") 147 .append(OPCODES[opcode]) 148 .append(" ") 149 .append(owner) 150 .append(" ") 151 .append(name) 152 .append(" ") 153 .append(desc) 154 .append("\n"); 155 156 if (cv != null) { 157 cv.visitMethodInsn(opcode, owner, name, desc); 158 } 159 } 160 161 public void printJumpInsn (final int opcode, final Label label) { 162 buf.append(" ") 163 .append(OPCODES[opcode]). 164 append(" "); 165 appendLabel(label); 166 buf.append("\n"); 167 168 if (cv != null) { 169 cv.visitJumpInsn(opcode, label); 170 } 171 } 172 173 public void printLabel (final Label label) { 174 buf.append(" "); 175 appendLabel(label); 176 buf.append("\n"); 178 179 if (cv != null) { 180 cv.visitLabel(label); 181 } 182 } 183 184 public void printLdcInsn (final Object cst) { 185 buf.append(" LDC "); 186 if (cst instanceof String ) { 187 buf.append("\"").append(cst).append("\""); 188 } else if (cst instanceof Type) { 189 buf.append(((Type)cst).getDescriptor() + ".class"); 190 } else { 191 buf.append(cst); 192 } 193 buf.append("\n"); 194 195 if (cv != null) { 196 cv.visitLdcInsn(cst); 197 } 198 } 199 200 public void printIincInsn (final int var, final int increment) { 201 buf.append(" IINC ") 202 .append(var) 203 .append(" ") 204 .append(increment) 205 .append("\n"); 206 207 if (cv != null) { 208 cv.visitIincInsn(var, increment); 209 } 210 } 211 212 public void printTableSwitchInsn ( 213 final int min, 214 final int max, 215 final Label dflt, 216 final Label labels[]) 217 { 218 buf.append(" TABLESWITCH\n"); 219 for (int i = 0; i < labels.length; ++i) { 220 buf.append(" ") 221 .append(min + i) 222 .append(": "); 223 appendLabel(labels[i]); 224 buf.append("\n"); 225 } 226 buf.append(" default: "); 227 appendLabel(dflt); 228 buf.append("\n"); 229 230 if (cv != null) { 231 cv.visitTableSwitchInsn(min, max, dflt, labels); 232 } 233 } 234 235 public void printLookupSwitchInsn ( 236 final Label dflt, 237 final int keys[], 238 final Label labels[]) 239 { 240 buf.append(" LOOKUPSWITCH\n"); 241 for (int i = 0; i < labels.length; ++i) { 242 buf.append(" ") 243 .append(keys[i]) 244 .append(": "); 245 appendLabel(labels[i]); 246 buf.append("\n"); 247 } 248 buf.append(" default: "); 249 appendLabel(dflt); 250 buf.append("\n"); 251 252 if (cv != null) { 253 cv.visitLookupSwitchInsn(dflt, keys, labels); 254 } 255 } 256 257 public void printMultiANewArrayInsn (final String desc, final int dims) { 258 buf.append(" MULTIANEWARRAY ") 259 .append(desc) 260 .append(" ") 261 .append(dims) 262 .append("\n"); 263 264 if (cv != null) { 265 cv.visitMultiANewArrayInsn(desc, dims); 266 } 267 } 268 269 public void printTryCatchBlock ( 270 final Label start, 271 final Label end, 272 final Label handler, 273 final String type) 274 { 275 buf.append(" TRYCATCHBLOCK "); 276 appendLabel(start); 277 buf.append(" "); 278 appendLabel(end); 279 buf.append(" "); 280 appendLabel(handler); 281 buf.append(" ") 282 .append(type) 283 .append("\n"); 284 285 if (cv != null) { 286 cv.visitTryCatchBlock(start, end, handler, type); 287 } 288 } 289 290 public void printMaxs (final int maxStack, final int maxLocals) { 291 buf.append(" MAXSTACK = ") 292 .append(maxStack) 293 .append("\n MAXLOCALS = ") 294 .append(maxLocals) 295 .append("\n\n"); 296 297 if (cv != null) { 298 cv.visitMaxs(maxStack, maxLocals); 299 } 300 } 301 302 public void printLocalVariable ( 303 final String name, 304 final String desc, 305 final Label start, 306 final Label end, 307 final int index) 308 { 309 buf.append(" LOCALVARIABLE ") 310 .append(name) 311 .append(" ") 312 .append(desc) 313 .append(" "); 314 appendLabel(start); 315 buf.append(" "); 316 appendLabel(end); 317 buf.append(" ") 318 .append(index) 319 .append("\n"); 320 321 if (cv != null) { 322 cv.visitLocalVariable(name, desc, start, end, index); 323 } 324 } 325 326 public void printLineNumber (final int line, final Label start) { 327 buf.append(" LINENUMBER ") 328 .append(line) 329 .append(" "); 330 appendLabel(start); 331 buf.append("\n"); 332 333 if (cv != null) { 334 cv.visitLineNumber(line, start); 335 } 336 } 337 338 public void printAttribute (final Attribute attr) { 339 buf.append(" CODE ATTRIBUTE ").append(attr.type).append(" : ") 340 .append(attr.toString()).append("\n"); 341 342 if (cv != null) { 343 cv.visitAttribute(attr); 344 } 345 } 346 347 353 354 private void appendLabel (final Label l) { 355 String name = (String )labelNames.get(l); 356 if (name == null) { 357 name = "L" + labelNames.size(); 358 labelNames.put(l, name); 359 } 360 buf.append(name); 361 } 362 } 363 | Popular Tags |