1 30 package org.objectweb.asm.attrs; 31 32 import java.io.PrintWriter ; 33 import java.io.StringWriter ; 34 import java.util.Arrays ; 35 36 import org.objectweb.asm.AbstractTest; 37 import org.objectweb.asm.Attribute; 38 import org.objectweb.asm.ClassReader; 39 import org.objectweb.asm.ClassVisitor; 40 import org.objectweb.asm.ClassWriter; 41 import org.objectweb.asm.Label; 42 import org.objectweb.asm.util.TraceClassVisitor; 43 import org.objectweb.asm.util.TraceMethodVisitor; 44 import org.objectweb.asm.util.attrs.ASMStackMapAttribute; 45 import org.objectweb.asm.util.attrs.ASMStackMapTableAttribute; 46 47 52 public class StackMapTableAttributeTest extends AbstractTest { 53 54 public static final String TEST_CLASS = "StackMapTableSample.data"; 55 56 public StackMapTableAttributeTest() { 57 super(); 58 is = getClass().getResourceAsStream(TEST_CLASS); 59 } 60 61 public void test() throws Exception { 62 Attribute[] tattributes = new Attribute[] { new StubStackMapTableAttribute() }; 63 Attribute[] attributes = new Attribute[] { new ASMStackMapTableAttribute() }; 64 65 ClassWriter cw = new ClassWriter(false); 66 67 TraceClassVisitor tv1 = new TraceClassVisitor(cw, 68 new PrintWriter (System.err)); 69 TraceClassVisitor tv2 = new TraceClassVisitor(cw, 70 new PrintWriter (System.err)) 71 { 72 protected TraceMethodVisitor createTraceMethodVisitor() { 73 return new TraceMethodVisitor() { 74 protected void appendLabel(Label l) { 75 super.appendLabel(l); 76 buf.append(" " + System.identityHashCode(l)); 77 } 78 }; 79 } 80 }; 81 82 ClassReader cr1 = new ClassReader(is); 83 cr1.accept(cw, attributes, true); 84 85 ClassReader cr2 = new ClassReader(cw.toByteArray()); 86 87 if (!Arrays.equals(cr1.b, cr2.b)) { 88 StringWriter sw1 = new StringWriter (); 89 StringWriter sw2 = new StringWriter (); 90 ClassVisitor cv1 = new TraceClassVisitor(new PrintWriter (sw1)); 91 ClassVisitor cv2 = new TraceClassVisitor(new PrintWriter (sw2)); 92 cr1.accept(cv1, attributes, true); 93 cr2.accept(cv2, attributes, true); 94 assertEquals("different data", sw1.toString(), sw2.toString()); 95 } 96 97 } 98 99 private static final class StubStackMapTableAttribute extends Attribute { 100 private static String s = "0123456789ABCDEF"; 101 102 private String data; 103 104 public StubStackMapTableAttribute() { 105 super("StackMapTable"); 106 } 107 108 protected Attribute read( 109 ClassReader cr, 110 int off, 111 int len, 112 char[] buf, 113 int codeOff, 114 Label[] labels) 115 { 116 StringBuffer sb = new StringBuffer (); 117 for (int i = 0; i < len; i++) { 118 int b = cr.readByte(off + i); 119 sb.append(s.charAt(b >> 4)) 120 .append(s.charAt(b & 0xF)) 121 .append(" "); 122 } 123 StubStackMapTableAttribute att = new StubStackMapTableAttribute(); 124 att.data = sb.toString(); 125 return att; 126 } 127 128 public String toString() { 129 return data; 130 } 131 132 } 133 134 } 135 | Popular Tags |