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