1 30 package org.objectweb.asm.commons; 31 32 import java.io.InputStream ; 33 import java.io.PrintWriter ; 34 import java.io.StringWriter ; 35 import java.lang.instrument.ClassFileTransformer ; 36 import java.lang.instrument.IllegalClassFormatException ; 37 import java.lang.instrument.Instrumentation ; 38 import java.security.ProtectionDomain ; 39 40 import junit.framework.TestSuite; 41 42 import org.objectweb.asm.AbstractTest; 43 import org.objectweb.asm.ClassAdapter; 44 import org.objectweb.asm.ClassReader; 45 import org.objectweb.asm.ClassVisitor; 46 import org.objectweb.asm.ClassWriter; 47 import org.objectweb.asm.MethodVisitor; 48 import org.objectweb.asm.util.TraceClassVisitor; 49 50 55 public class LocalVariablesSorterTest2 extends AbstractTest { 56 57 public static void premain( 58 final String agentArgs, 59 final Instrumentation inst) 60 { 61 inst.addTransformer(new ClassFileTransformer () { 62 public byte[] transform( 63 final ClassLoader loader, 64 final String className, 65 final Class classBeingRedefined, 66 final ProtectionDomain domain, 67 final byte[] classFileBuffer) 68 throws IllegalClassFormatException 69 { 70 String n = className.replace('/', '.'); 71 if (agentArgs.length() == 0 || n.indexOf(agentArgs) != -1) { 72 return transformClass(classFileBuffer); 73 } else { 74 return null; 75 } 76 } 77 }); 78 } 79 80 private static byte[] transformClass(final byte[] clazz) { 81 ClassReader cr = new ClassReader(clazz); 82 ClassWriter cw = new ClassWriter(0); 83 cr.accept(new ClassAdapter(cw) { 84 85 public MethodVisitor visitMethod( 86 final int access, 87 final String name, 88 final String desc, 89 final String signature, 90 final String [] exceptions) 91 { 92 return new LocalVariablesSorter(access, 93 desc, 94 cv.visitMethod(access, 95 name, 96 desc, 97 signature, 98 exceptions)); 99 } 100 101 }, ClassReader.EXPAND_FRAMES); 102 return cw.toByteArray(); 103 } 104 105 public static TestSuite suite() throws Exception { 106 return new LocalVariablesSorterTest2().getSuite(); 107 } 108 109 public void test() throws Exception { 110 try { 111 Class.forName(n, true, getClass().getClassLoader()); 112 } catch (NoClassDefFoundError ncdfe) { 113 } catch (UnsatisfiedLinkError ule) { 115 } catch (ClassFormatError cfe) { 117 fail(cfe.getMessage()); 118 } catch (VerifyError ve) { 119 String s = n.replace('.', '/') + ".class"; 120 InputStream is = getClass().getClassLoader().getResourceAsStream(s); 121 ClassReader cr = new ClassReader(is); 122 byte[] b = transformClass(cr.b); 123 StringWriter sw1 = new StringWriter (); 124 StringWriter sw2 = new StringWriter (); 125 sw2.write(ve.toString() + "\n"); 126 ClassVisitor cv1 = new TraceClassVisitor(new PrintWriter (sw1)); 127 ClassVisitor cv2 = new TraceClassVisitor(new PrintWriter (sw2)); 128 cr.accept(cv1, 0); 129 new ClassReader(b).accept(cv2, 0); 130 String s1 = sw1.toString(); 131 String s2 = sw2.toString(); 132 assertEquals("different data", s1, s2); 133 } 134 } 135 } 136 | Popular Tags |