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