1 34 35 package org.logicalcobwebs.asm.util; 36 37 import org.logicalcobwebs.asm.Label; 38 import org.logicalcobwebs.asm.Attribute; 39 40 import java.util.HashMap ; 41 42 46 47 public class DumpCodeVisitor extends PrintCodeVisitor { 48 49 52 53 private final HashMap labelNames; 54 55 58 59 public DumpCodeVisitor () { 60 this.labelNames = new HashMap (); 61 } 62 63 public void printInsn (final int opcode) { 64 buf.append("cv.visitInsn("). 65 append(OPCODES[opcode]). 66 append(");\n"); 67 } 68 69 public void printIntInsn (final int opcode, final int operand) { 70 buf.append("cv.visitIntInsn("). 71 append(OPCODES[opcode]). 72 append(", "). 73 append(operand). 74 append(");\n"); 75 } 76 77 public void printVarInsn (final int opcode, final int var) { 78 buf.append("cv.visitVarInsn("). 79 append(OPCODES[opcode]). 80 append(", "). 81 append(var). 82 append(");\n"); 83 } 84 85 public void printTypeInsn (final int opcode, final String desc) { 86 buf.append("cv.visitTypeInsn("). 87 append(OPCODES[opcode]). 88 append(", "); 89 DumpClassVisitor.appendConstant(buf, desc); 90 buf.append(");\n"); 91 } 92 93 public void printFieldInsn ( 94 final int opcode, 95 final String owner, 96 final String name, 97 final String desc) 98 { 99 buf.append("cv.visitFieldInsn(") 100 .append(OPCODES[opcode]) 101 .append(", "); 102 DumpClassVisitor.appendConstant(buf, owner); 103 buf.append(", "); 104 DumpClassVisitor.appendConstant(buf, name); 105 buf.append(", "); 106 DumpClassVisitor.appendConstant(buf, desc); 107 buf.append(");\n"); 108 } 109 110 public void printMethodInsn ( 111 final int opcode, 112 final String owner, 113 final String name, 114 final String desc) 115 { 116 buf.append("cv.visitMethodInsn(") 117 .append(OPCODES[opcode]) 118 .append(", "); 119 DumpClassVisitor.appendConstant(buf, owner); 120 buf.append(", "); 121 DumpClassVisitor.appendConstant(buf, name); 122 buf.append(", "); 123 DumpClassVisitor.appendConstant(buf, desc); 124 buf.append(");\n"); 125 } 126 127 public void printJumpInsn (final int opcode, final Label label) { 128 declareLabel(label); 129 buf.append("cv.visitJumpInsn(") 130 .append(OPCODES[opcode]) 131 .append(", "); 132 appendLabel(label); 133 buf.append(");\n"); 134 } 135 136 public void printLabel (final Label label) { 137 declareLabel(label); 138 buf.append("cv.visitLabel("); 139 appendLabel(label); 140 buf.append(");\n"); 141 } 142 143 public void printLdcInsn (final Object cst) { 144 buf.append("cv.visitLdcInsn("); 145 DumpClassVisitor.appendConstant(buf, cst); 146 buf.append(");\n"); 147 } 148 149 public void printIincInsn (final int var, final int increment) { 150 buf.append("cv.visitIincInsn(") 151 .append(var) 152 .append(", ") 153 .append(increment) 154 .append(");\n"); 155 } 156 157 public void printTableSwitchInsn ( 158 final int min, 159 final int max, 160 final Label dflt, 161 final Label labels[]) 162 { 163 for (int i = 0; i < labels.length; ++i) { 164 declareLabel(labels[i]); 165 } 166 declareLabel(dflt); 167 168 buf.append("cv.visitTableSwitchInsn(") 169 .append(min) 170 .append(", ") 171 .append(max) 172 .append(", "); 173 appendLabel(dflt); 174 buf.append(", new Label[] {"); 175 for (int i = 0; i < labels.length; ++i) { 176 buf.append(i == 0 ? " " : ", "); 177 appendLabel(labels[i]); 178 } 179 buf.append(" });\n"); 180 } 181 182 public void printLookupSwitchInsn ( 183 final Label dflt, 184 final int keys[], 185 final Label labels[]) 186 { 187 for (int i = 0; i < labels.length; ++i) { 188 declareLabel(labels[i]); 189 } 190 declareLabel(dflt); 191 192 buf.append("cv.visitLookupSwitchInsn("); 193 appendLabel(dflt); 194 buf.append(", new int[] {"); 195 for (int i = 0; i < keys.length; ++i) { 196 buf.append(i == 0 ? " " : ", ").append(keys[i]); 197 } 198 buf.append(" }, new Label[] {"); 199 for (int i = 0; i < labels.length; ++i) { 200 buf.append(i == 0 ? " " : ", "); 201 appendLabel(labels[i]); 202 } 203 buf.append(" });\n"); 204 } 205 206 public void printMultiANewArrayInsn (final String desc, final int dims) { 207 buf.append("cv.visitMultiANewArrayInsn("); 208 DumpClassVisitor.appendConstant(buf, desc); 209 buf.append(", ") 210 .append(dims) 211 .append(");\n"); 212 } 213 214 public void printTryCatchBlock ( 215 final Label start, 216 final Label end, 217 final Label handler, 218 final String type) 219 { 220 buf.append("cv.visitTryCatchBlock("); 221 appendLabel(start); 222 buf.append(", "); 223 appendLabel(end); 224 buf.append(", "); 225 appendLabel(handler); 226 buf.append(", "); 227 DumpClassVisitor.appendConstant(buf, type); 228 buf.append(");\n"); 229 } 230 231 public void printMaxs (final int maxStack, final int maxLocals) { 232 buf.append("cv.visitMaxs(") 233 .append(maxStack) 234 .append(", ") 235 .append(maxLocals) 236 .append(");\n"); 237 } 238 239 public void printLocalVariable ( 240 final String name, 241 final String desc, 242 final Label start, 243 final Label end, 244 final int index) 245 { 246 buf.append("cv.visitLocalVariable("); 247 DumpClassVisitor.appendConstant(buf, name); 248 buf.append(", "); 249 DumpClassVisitor.appendConstant(buf, desc); 250 buf.append(", "); 251 appendLabel(start); 252 buf.append(", "); 253 appendLabel(end); 254 buf.append(", ").append(index).append(");\n"); 255 } 256 257 public void printLineNumber (final int line, final Label start) { 258 buf.append("cv.visitLineNumber(") 259 .append(line) 260 .append(", "); 261 appendLabel(start); 262 buf.append(");\n"); 263 } 264 265 public void printAttribute (final Attribute attr) { 266 buf.append("// WARNING! skipped a non standard code attribute of type \""); 267 buf.append(attr.type); 268 buf.append("\"\n"); 269 } 270 271 278 279 private void declareLabel (final Label l) { 280 String name = (String )labelNames.get(l); 281 if (name == null) { 282 name = "l" + labelNames.size(); 283 labelNames.put(l, name); 284 buf.append("Label ") 285 .append(name) 286 .append(" = new Label();\n"); 287 } 288 } 289 290 297 298 private void appendLabel (final Label l) { 299 buf.append((String )labelNames.get(l)); 300 } 301 } 302 | Popular Tags |