KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > commons > GASMifierTest


1 /***
2  * ASM tests
3  * Copyright (c) 2002-2005 France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.commons;
31
32 import java.io.PrintWriter JavaDoc;
33 import java.io.StringReader JavaDoc;
34 import java.io.StringWriter JavaDoc;
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.net.URLClassLoader JavaDoc;
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 /**
56  * GASMifier tests.
57  *
58  * @author Eugene Kuleshov
59  * @author Eric Bruneton
60  */

61 public class GASMifierTest extends AbstractTest {
62
63     public static final Compiler JavaDoc COMPILER = new Compiler JavaDoc();
64
65     private final static TestClassLoader LOADER = new TestClassLoader();
66
67     public static TestSuite suite() throws Exception JavaDoc {
68         return new GASMifierTest().getSuite();
69     }
70
71     public void test() throws Exception JavaDoc {
72         ClassReader cr = new ClassReader(is);
73
74         if (cr.b.length > 20000) {
75             return;
76         }
77
78         StringWriter JavaDoc sw = new StringWriter JavaDoc();
79         GASMifierClassVisitor cv = new GASMifierClassVisitor(new PrintWriter JavaDoc(sw));
80         cr.accept(cv, false);
81
82         String JavaDoc generated = sw.toString();
83
84         byte[] generatorClassData;
85         try {
86             generatorClassData = COMPILER.compile(n, generated);
87         } catch (Exception JavaDoc 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 JavaDoc name,
97                 String JavaDoc desc,
98                 String JavaDoc signature,
99                 String JavaDoc[] 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 JavaDoc c = LOADER.defineClass("asm." + n + "Dump", generatorClassData);
113         Method m = c.getMethod("dump", new Class JavaDoc[0]);
114         byte[] b;
115         try {
116             b = (byte[]) m.invoke(null, new Object JavaDoc[0]);
117         } catch (InvocationTargetException JavaDoc ex) {
118             trace(generated);
119             throw (Exception JavaDoc) ex.getTargetException();
120         }
121
122         try {
123             assertEquals(cr, new ClassReader(b));
124         } catch (Throwable JavaDoc e) {
125             trace(generated);
126             assertEquals(cr, new ClassReader(b));
127         }
128     }
129
130     private void trace(String JavaDoc generated) {
131         if (System.getProperty("asm.test.class") != null) {
132             System.err.println(generated);
133         }
134     }
135
136     private static class TestClassLoader extends ClassLoader JavaDoc {
137
138         public Class JavaDoc defineClass(final String JavaDoc 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 JavaDoc(new URL JavaDoc[0]));
146
147         public byte[] compile(String JavaDoc name, String JavaDoc source) throws Exception JavaDoc {
148             Parser p = new Parser(new Scanner(name, new StringReader JavaDoc(source)));
149             UnitCompiler uc = new UnitCompiler(p.parseCompilationUnit(), CL);
150             return uc.compileUnit(DebuggingInformation.ALL)[0].toByteArray();
151         }
152     }
153 }
154
Popular Tags