1 30 package org.objectweb.asm.xml; 31 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import org.objectweb.asm.AnnotationVisitor; 36 import org.objectweb.asm.Attribute; 37 import org.objectweb.asm.MethodVisitor; 38 import org.objectweb.asm.Opcodes; 39 import org.objectweb.asm.Label; 40 import org.objectweb.asm.Type; 41 import org.objectweb.asm.util.AbstractVisitor; 42 import org.xml.sax.ContentHandler ; 43 import org.xml.sax.helpers.AttributesImpl ; 44 45 54 public final class SAXCodeAdapter extends SAXAdapter implements MethodVisitor { 55 private Map labelNames; 56 57 63 public SAXCodeAdapter(ContentHandler h, int access) { 64 super(h); 65 labelNames = new HashMap (); 66 67 if ((access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_NATIVE)) == 0) 68 { 69 addStart("code", new AttributesImpl ()); 70 } 71 } 72 73 public final void visitCode() { 74 } 75 76 public final void visitInsn(int opcode) { 77 addElement(AbstractVisitor.OPCODES[opcode], new AttributesImpl ()); 78 } 79 80 public final void visitIntInsn(int opcode, int operand) { 81 AttributesImpl attrs = new AttributesImpl (); 82 attrs.addAttribute("", "value", "value", "", Integer.toString(operand)); 83 addElement(AbstractVisitor.OPCODES[opcode], attrs); 84 } 85 86 public final void visitVarInsn(int opcode, int var) { 87 AttributesImpl attrs = new AttributesImpl (); 88 attrs.addAttribute("", "var", "var", "", Integer.toString(var)); 89 addElement(AbstractVisitor.OPCODES[opcode], attrs); 90 } 91 92 public final void visitTypeInsn(int opcode, String desc) { 93 AttributesImpl attrs = new AttributesImpl (); 94 attrs.addAttribute("", "desc", "desc", "", desc); 95 addElement(AbstractVisitor.OPCODES[opcode], attrs); 96 } 97 98 public final void visitFieldInsn( 99 int opcode, 100 String owner, 101 String name, 102 String desc) 103 { 104 AttributesImpl attrs = new AttributesImpl (); 105 attrs.addAttribute("", "owner", "owner", "", owner); 106 attrs.addAttribute("", "name", "name", "", name); 107 attrs.addAttribute("", "desc", "desc", "", desc); 108 addElement(AbstractVisitor.OPCODES[opcode], attrs); 109 } 110 111 public final void visitMethodInsn( 112 int opcode, 113 String owner, 114 String name, 115 String desc) 116 { 117 AttributesImpl attrs = new AttributesImpl (); 118 attrs.addAttribute("", "owner", "owner", "", owner); 119 attrs.addAttribute("", "name", "name", "", name); 120 attrs.addAttribute("", "desc", "desc", "", desc); 121 addElement(AbstractVisitor.OPCODES[opcode], attrs); 122 } 123 124 public final void visitJumpInsn(int opcode, Label label) { 125 AttributesImpl attrs = new AttributesImpl (); 126 attrs.addAttribute("", "label", "label", "", getLabel(label)); 127 addElement(AbstractVisitor.OPCODES[opcode], attrs); 128 } 129 130 public final void visitLabel(Label label) { 131 AttributesImpl attrs = new AttributesImpl (); 132 attrs.addAttribute("", "name", "name", "", getLabel(label)); 133 addElement("Label", attrs); 134 } 135 136 public final void visitLdcInsn(Object cst) { 137 AttributesImpl attrs = new AttributesImpl (); 138 attrs.addAttribute("", 139 "cst", 140 "cst", 141 "", 142 SAXClassAdapter.encode(cst.toString())); 143 attrs.addAttribute("", 144 "desc", 145 "desc", 146 "", 147 Type.getDescriptor(cst.getClass())); 148 addElement(AbstractVisitor.OPCODES[Opcodes.LDC], attrs); 149 } 150 151 public final void visitIincInsn(int var, int increment) { 152 AttributesImpl attrs = new AttributesImpl (); 153 attrs.addAttribute("", "var", "var", "", Integer.toString(var)); 154 attrs.addAttribute("", "inc", "inc", "", Integer.toString(increment)); 155 addElement(AbstractVisitor.OPCODES[Opcodes.IINC], attrs); 156 } 157 158 public final void visitTableSwitchInsn( 159 int min, 160 int max, 161 Label dflt, 162 Label[] labels) 163 { 164 AttributesImpl attrs = new AttributesImpl (); 165 attrs.addAttribute("", "min", "min", "", Integer.toString(min)); 166 attrs.addAttribute("", "max", "max", "", Integer.toString(max)); 167 attrs.addAttribute("", "dflt", "dflt", "", getLabel(dflt)); 168 String o = AbstractVisitor.OPCODES[Opcodes.TABLESWITCH]; 169 addStart(o, attrs); 170 for (int i = 0; i < labels.length; i++) { 171 AttributesImpl att2 = new AttributesImpl (); 172 att2.addAttribute("", "name", "name", "", getLabel(labels[i])); 173 addElement("label", att2); 174 } 175 addEnd(o); 176 } 177 178 public final void visitLookupSwitchInsn( 179 Label dflt, 180 int[] keys, 181 Label[] labels) 182 { 183 AttributesImpl att = new AttributesImpl (); 184 att.addAttribute("", "dflt", "dflt", "", getLabel(dflt)); 185 String o = AbstractVisitor.OPCODES[Opcodes.LOOKUPSWITCH]; 186 addStart(o, att); 187 for (int i = 0; i < labels.length; i++) { 188 AttributesImpl att2 = new AttributesImpl (); 189 att2.addAttribute("", "name", "name", "", getLabel(labels[i])); 190 att2.addAttribute("", "key", "key", "", Integer.toString(keys[i])); 191 addElement("label", att2); 192 } 193 addEnd(o); 194 } 195 196 public final void visitMultiANewArrayInsn(String desc, int dims) { 197 AttributesImpl attrs = new AttributesImpl (); 198 attrs.addAttribute("", "desc", "desc", "", desc); 199 attrs.addAttribute("", "dims", "dims", "", Integer.toString(dims)); 200 addElement(AbstractVisitor.OPCODES[Opcodes.MULTIANEWARRAY], attrs); 201 } 202 203 public final void visitTryCatchBlock( 204 Label start, 205 Label end, 206 Label handler, 207 String type) 208 { 209 AttributesImpl attrs = new AttributesImpl (); 210 attrs.addAttribute("", "start", "start", "", getLabel(start)); 211 attrs.addAttribute("", "end", "end", "", getLabel(end)); 212 attrs.addAttribute("", "handler", "handler", "", getLabel(handler)); 213 if (type != null) 214 attrs.addAttribute("", "type", "type", "", type); 215 addElement("TryCatch", attrs); 216 } 217 218 public final void visitMaxs(int maxStack, int maxLocals) { 219 AttributesImpl attrs = new AttributesImpl (); 220 attrs.addAttribute("", 221 "maxStack", 222 "maxStack", 223 "", 224 Integer.toString(maxStack)); 225 attrs.addAttribute("", 226 "maxLocals", 227 "maxLocals", 228 "", 229 Integer.toString(maxLocals)); 230 addElement("Max", attrs); 231 232 addEnd("code"); 233 } 234 235 public void visitLocalVariable( 236 String name, 237 String desc, 238 String signature, 239 Label start, 240 Label end, 241 int index) 242 { 243 AttributesImpl attrs = new AttributesImpl (); 244 attrs.addAttribute("", "name", "name", "", name); 245 attrs.addAttribute("", "desc", "desc", "", desc); 246 if (signature != null) 247 attrs.addAttribute("", 248 "signature", 249 "signature", 250 "", 251 SAXClassAdapter.encode(signature)); 252 attrs.addAttribute("", "start", "start", "", getLabel(start)); 253 attrs.addAttribute("", "end", "end", "", getLabel(end)); 254 attrs.addAttribute("", "var", "var", "", Integer.toString(index)); 255 addElement("LocalVar", attrs); 256 } 257 258 public final void visitLineNumber(int line, Label start) { 259 AttributesImpl attrs = new AttributesImpl (); 260 attrs.addAttribute("", "line", "line", "", Integer.toString(line)); 261 attrs.addAttribute("", "start", "start", "", getLabel(start)); 262 addElement("LineNumber", attrs); 263 } 264 265 public AnnotationVisitor visitAnnotationDefault() { 266 return new SAXAnnotationAdapter(getContentHandler(), 267 "annotationDefault", 268 0, 269 null, 270 null); 271 } 272 273 public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 274 return new SAXAnnotationAdapter(getContentHandler(), 275 "annotation", 276 visible ? 1 : -1, 277 null, 278 desc); 279 } 280 281 public AnnotationVisitor visitParameterAnnotation( 282 int parameter, 283 String desc, 284 boolean visible) 285 { 286 return new SAXAnnotationAdapter(getContentHandler(), 287 "parameterAnnotation", 288 visible ? 1 : -1, 289 parameter, 290 desc); 291 } 292 293 public void visitEnd() { 294 addEnd("method"); 295 } 296 297 public final void visitAttribute(Attribute attr) { 298 } 300 301 private final String getLabel(Label label) { 302 String name = (String ) labelNames.get(label); 303 if (name == null) { 304 name = Integer.toString(labelNames.size()); 305 labelNames.put(label, name); 306 } 307 return name; 308 } 309 310 } 311 | Popular Tags |