1 30 package org.objectweb.asm.util; 31 32 import org.objectweb.asm.AnnotationVisitor; 33 import org.objectweb.asm.MethodVisitor; 34 import org.objectweb.asm.Label; 35 36 import java.util.HashMap ; 37 38 45 public class ASMifierMethodVisitor extends ASMifierAbstractVisitor implements 46 MethodVisitor 47 { 48 49 52 public ASMifierMethodVisitor() { 53 super("mv"); 54 this.labelNames = new HashMap (); 55 } 56 57 public AnnotationVisitor visitAnnotationDefault() { 58 buf.setLength(0); 59 buf.append("{\n").append("av0 = mv.visitAnnotationDefault();\n"); 60 text.add(buf.toString()); 61 ASMifierAnnotationVisitor av = new ASMifierAnnotationVisitor(0); 62 text.add(av.getText()); 63 text.add("}\n"); 64 return av; 65 } 66 67 public AnnotationVisitor visitParameterAnnotation( 68 final int parameter, 69 final String desc, 70 final boolean visible) 71 { 72 buf.setLength(0); 73 buf.append("{\n") 74 .append("av0 = mv.visitParameterAnnotation(") 75 .append(parameter) 76 .append(", "); 77 appendConstant(desc); 78 buf.append(", ").append(visible).append(");\n"); 79 text.add(buf.toString()); 80 ASMifierAnnotationVisitor av = new ASMifierAnnotationVisitor(0); 81 text.add(av.getText()); 82 text.add("}\n"); 83 return av; 84 } 85 86 public void visitCode() { 87 text.add("mv.visitCode();\n"); 88 } 89 90 public void visitInsn(final int opcode) { 91 buf.setLength(0); 92 buf.append("mv.visitInsn(").append(OPCODES[opcode]).append(");\n"); 93 text.add(buf.toString()); 94 } 95 96 public void visitIntInsn(final int opcode, final int operand) { 97 buf.setLength(0); 98 buf.append("mv.visitIntInsn(") 99 .append(OPCODES[opcode]) 100 .append(", ") 101 .append(operand) 102 .append(");\n"); 103 text.add(buf.toString()); 104 } 105 106 public void visitVarInsn(final int opcode, final int var) { 107 buf.setLength(0); 108 buf.append("mv.visitVarInsn(") 109 .append(OPCODES[opcode]) 110 .append(", ") 111 .append(var) 112 .append(");\n"); 113 text.add(buf.toString()); 114 } 115 116 public void visitTypeInsn(final int opcode, final String desc) { 117 buf.setLength(0); 118 buf.append("mv.visitTypeInsn(").append(OPCODES[opcode]).append(", "); 119 appendConstant(desc); 120 buf.append(");\n"); 121 text.add(buf.toString()); 122 } 123 124 public void visitFieldInsn( 125 final int opcode, 126 final String owner, 127 final String name, 128 final String desc) 129 { 130 buf.setLength(0); 131 buf.append("mv.visitFieldInsn(").append(OPCODES[opcode]).append(", "); 132 appendConstant(owner); 133 buf.append(", "); 134 appendConstant(name); 135 buf.append(", "); 136 appendConstant(desc); 137 buf.append(");\n"); 138 text.add(buf.toString()); 139 } 140 141 public void visitMethodInsn( 142 final int opcode, 143 final String owner, 144 final String name, 145 final String desc) 146 { 147 buf.setLength(0); 148 buf.append("mv.visitMethodInsn(").append(OPCODES[opcode]).append(", "); 149 appendConstant(owner); 150 buf.append(", "); 151 appendConstant(name); 152 buf.append(", "); 153 appendConstant(desc); 154 buf.append(");\n"); 155 text.add(buf.toString()); 156 } 157 158 public void visitJumpInsn(final int opcode, final Label label) { 159 buf.setLength(0); 160 declareLabel(label); 161 buf.append("mv.visitJumpInsn(").append(OPCODES[opcode]).append(", "); 162 appendLabel(label); 163 buf.append(");\n"); 164 text.add(buf.toString()); 165 } 166 167 public void visitLabel(final Label label) { 168 buf.setLength(0); 169 declareLabel(label); 170 buf.append("mv.visitLabel("); 171 appendLabel(label); 172 buf.append(");\n"); 173 text.add(buf.toString()); 174 } 175 176 public void visitLdcInsn(final Object cst) { 177 buf.setLength(0); 178 buf.append("mv.visitLdcInsn("); 179 appendConstant(cst); 180 buf.append(");\n"); 181 text.add(buf.toString()); 182 } 183 184 public void visitIincInsn(final int var, final int increment) { 185 buf.setLength(0); 186 buf.append("mv.visitIincInsn(") 187 .append(var) 188 .append(", ") 189 .append(increment) 190 .append(");\n"); 191 text.add(buf.toString()); 192 } 193 194 public void visitTableSwitchInsn( 195 final int min, 196 final int max, 197 final Label dflt, 198 final Label labels[]) 199 { 200 buf.setLength(0); 201 for (int i = 0; i < labels.length; ++i) { 202 declareLabel(labels[i]); 203 } 204 declareLabel(dflt); 205 206 buf.append("mv.visitTableSwitchInsn(") 207 .append(min) 208 .append(", ") 209 .append(max) 210 .append(", "); 211 appendLabel(dflt); 212 buf.append(", new Label[] {"); 213 for (int i = 0; i < labels.length; ++i) { 214 buf.append(i == 0 ? " " : ", "); 215 appendLabel(labels[i]); 216 } 217 buf.append(" });\n"); 218 text.add(buf.toString()); 219 } 220 221 public void visitLookupSwitchInsn( 222 final Label dflt, 223 final int keys[], 224 final Label labels[]) 225 { 226 buf.setLength(0); 227 for (int i = 0; i < labels.length; ++i) { 228 declareLabel(labels[i]); 229 } 230 declareLabel(dflt); 231 232 buf.append("mv.visitLookupSwitchInsn("); 233 appendLabel(dflt); 234 buf.append(", new int[] {"); 235 for (int i = 0; i < keys.length; ++i) { 236 buf.append(i == 0 ? " " : ", ").append(keys[i]); 237 } 238 buf.append(" }, new Label[] {"); 239 for (int i = 0; i < labels.length; ++i) { 240 buf.append(i == 0 ? " " : ", "); 241 appendLabel(labels[i]); 242 } 243 buf.append(" });\n"); 244 text.add(buf.toString()); 245 } 246 247 public void visitMultiANewArrayInsn(final String desc, final int dims) { 248 buf.setLength(0); 249 buf.append("mv.visitMultiANewArrayInsn("); 250 appendConstant(desc); 251 buf.append(", ").append(dims).append(");\n"); 252 text.add(buf.toString()); 253 } 254 255 public void visitTryCatchBlock( 256 final Label start, 257 final Label end, 258 final Label handler, 259 final String type) 260 { 261 buf.setLength(0); 262 buf.append("mv.visitTryCatchBlock("); 263 appendLabel(start); 264 buf.append(", "); 265 appendLabel(end); 266 buf.append(", "); 267 appendLabel(handler); 268 buf.append(", "); 269 appendConstant(type); 270 buf.append(");\n"); 271 text.add(buf.toString()); 272 } 273 274 public void visitLocalVariable( 275 final String name, 276 final String desc, 277 final String signature, 278 final Label start, 279 final Label end, 280 final int index) 281 { 282 buf.setLength(0); 283 buf.append("mv.visitLocalVariable("); 284 appendConstant(name); 285 buf.append(", "); 286 appendConstant(desc); 287 buf.append(", "); 288 appendConstant(signature); 289 buf.append(", "); 290 appendLabel(start); 291 buf.append(", "); 292 appendLabel(end); 293 buf.append(", ").append(index).append(");\n"); 294 text.add(buf.toString()); 295 } 296 297 public void visitLineNumber(final int line, final Label start) { 298 buf.setLength(0); 299 buf.append("mv.visitLineNumber(").append(line).append(", "); 300 appendLabel(start); 301 buf.append(");\n"); 302 text.add(buf.toString()); 303 } 304 305 public void visitMaxs(final int maxStack, final int maxLocals) { 306 buf.setLength(0); 307 buf.append("mv.visitMaxs(") 308 .append(maxStack) 309 .append(", ") 310 .append(maxLocals) 311 .append(");\n"); 312 text.add(buf.toString()); 313 } 314 315 322 private void declareLabel(final Label l) { 323 String name = (String ) labelNames.get(l); 324 if (name == null) { 325 name = "l" + labelNames.size(); 326 labelNames.put(l, name); 327 buf.append("Label ").append(name).append(" = new Label();\n"); 328 } 329 } 330 331 338 private void appendLabel(final Label l) { 339 buf.append((String ) labelNames.get(l)); 340 } 341 } 342 | Popular Tags |