1 30 package org.objectweb.asm.util; 31 32 import java.io.PrintWriter ; 33 import java.io.StringReader ; 34 import java.io.StringWriter ; 35 import java.lang.reflect.Method ; 36 import java.net.URL ; 37 import java.net.URLClassLoader ; 38 39 import junit.framework.TestSuite; 40 41 import org.codehaus.janino.ClassLoaderIClassLoader; 42 import org.codehaus.janino.DebuggingInformation; 43 import org.codehaus.janino.IClassLoader; 44 import org.codehaus.janino.Parser; 45 import org.codehaus.janino.Scanner; 46 import org.codehaus.janino.UnitCompiler; 47 48 import org.objectweb.asm.AbstractTest; 49 import org.objectweb.asm.ClassReader; 50 import org.objectweb.asm.util.ASMifierClassVisitor; 51 52 58 public class ASMifierTest extends AbstractTest { 59 60 public static final Compiler COMPILER = new Compiler (); 61 62 public static final TestClassLoader LOADER = new TestClassLoader(); 63 64 public static TestSuite suite() throws Exception { 65 return new ASMifierTest().getSuite(); 66 } 67 68 public void test() throws Exception { 69 ClassReader cr = new ClassReader(is); 70 71 if (cr.b.length > 20000) { 72 return; 73 } 74 75 StringWriter sw = new StringWriter (); 76 ASMifierClassVisitor cv = new ASMifierClassVisitor(new PrintWriter (sw)); 77 cr.accept(cv, false); 78 79 String generated = sw.toString(); 80 81 byte[] generatorClassData; 82 try { 83 generatorClassData = COMPILER.compile(n, generated); 84 } catch (Exception ex) { 85 System.err.println(generated); 86 System.err.println("------------------"); 87 throw ex; 88 } 89 90 Class c = LOADER.defineClass("asm." + n + "Dump", generatorClassData); 91 Method m = c.getMethod("dump", new Class [0]); 92 byte[] b = (byte[]) m.invoke(null, new Object [0]); 93 94 assertEquals(cr, new ClassReader(b)); 95 } 96 97 public static class TestClassLoader extends ClassLoader { 98 99 public Class defineClass(final String name, final byte[] b) { 100 return defineClass(name, b, 0, b.length); 101 } 102 } 103 104 public static class Compiler { 105 106 final static IClassLoader CL = new ClassLoaderIClassLoader(new URLClassLoader (new URL [0])); 107 108 public byte[] compile(String name, String source) throws Exception { 109 Parser p = new Parser(new Scanner(name, new StringReader (source))); 110 UnitCompiler uc = new UnitCompiler(p.parseCompilationUnit(), CL); 111 return uc.compileUnit(DebuggingInformation.ALL)[0].toByteArray(); 112 } 113 } 114 } 115 | Popular Tags |