1 30 package org.objectweb.asm.commons; 31 32 import java.io.PrintWriter ; 33 import java.io.StringReader ; 34 import java.io.StringWriter ; 35 import java.lang.reflect.InvocationTargetException ; 36 import java.lang.reflect.Method ; 37 import java.net.URL ; 38 import java.net.URLClassLoader ; 39 40 import junit.framework.TestSuite; 41 42 import org.codehaus.janino.ClassLoaderIClassLoader; 43 import org.codehaus.janino.DebuggingInformation; 44 import org.codehaus.janino.IClassLoader; 45 import org.codehaus.janino.Parser; 46 import org.codehaus.janino.Scanner; 47 import org.codehaus.janino.UnitCompiler; 48 49 import org.objectweb.asm.AbstractTest; 50 import org.objectweb.asm.ClassAdapter; 51 import org.objectweb.asm.ClassReader; 52 import org.objectweb.asm.ClassWriter; 53 import org.objectweb.asm.MethodVisitor; 54 55 61 public class GASMifierTest extends AbstractTest { 62 63 public static final Compiler COMPILER = new Compiler (); 64 65 private final static TestClassLoader LOADER = new TestClassLoader(); 66 67 public static TestSuite suite() throws Exception { 68 return new GASMifierTest().getSuite(); 69 } 70 71 public void test() throws Exception { 72 ClassReader cr = new ClassReader(is); 73 74 if (cr.b.length > 20000) { 75 return; 76 } 77 78 StringWriter sw = new StringWriter (); 79 GASMifierClassVisitor cv = new GASMifierClassVisitor(new PrintWriter (sw)); 80 cr.accept(cv, false); 81 82 String generated = sw.toString(); 83 84 byte[] generatorClassData; 85 try { 86 generatorClassData = COMPILER.compile(n, generated); 87 } catch (Exception ex) { 88 trace(generated); 89 throw ex; 90 } 91 92 ClassWriter cw = new ClassWriter(true); 93 cr.accept(new ClassAdapter(cw) { 94 public MethodVisitor visitMethod( 95 int access, 96 String name, 97 String desc, 98 String signature, 99 String [] exceptions) 100 { 101 return new LocalVariablesSorter(access, 102 desc, 103 super.visitMethod(access, 104 name, 105 desc, 106 signature, 107 exceptions)); 108 } 109 }, false); 110 cr = new ClassReader(cw.toByteArray()); 111 112 Class c = LOADER.defineClass("asm." + n + "Dump", generatorClassData); 113 Method m = c.getMethod("dump", new Class [0]); 114 byte[] b; 115 try { 116 b = (byte[]) m.invoke(null, new Object [0]); 117 } catch (InvocationTargetException ex) { 118 trace(generated); 119 throw (Exception ) ex.getTargetException(); 120 } 121 122 try { 123 assertEquals(cr, new ClassReader(b)); 124 } catch (Throwable e) { 125 trace(generated); 126 assertEquals(cr, new ClassReader(b)); 127 } 128 } 129 130 private void trace(String generated) { 131 if (System.getProperty("asm.test.class") != null) { 132 System.err.println(generated); 133 } 134 } 135 136 private static class TestClassLoader extends ClassLoader { 137 138 public Class defineClass(final String name, final byte[] b) { 139 return defineClass(name, b, 0, b.length); 140 } 141 } 142 143 private static class Compiler { 144 145 final static IClassLoader CL = new ClassLoaderIClassLoader(new URLClassLoader (new URL [0])); 146 147 public byte[] compile(String name, String source) throws Exception { 148 Parser p = new Parser(new Scanner(name, new StringReader (source))); 149 UnitCompiler uc = new UnitCompiler(p.parseCompilationUnit(), CL); 150 return uc.compileUnit(DebuggingInformation.ALL)[0].toByteArray(); 151 } 152 } 153 } 154 | Popular Tags |