001: /***
002: * ASM tests
003: * Copyright (c) 2002-2005 France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.objectweb.asm.commons;
030:
031: import java.io.PrintWriter;
032: import java.io.StringReader;
033: import java.io.StringWriter;
034: import java.lang.reflect.InvocationTargetException;
035: import java.lang.reflect.Method;
036: import java.net.URL;
037: import java.net.URLClassLoader;
038:
039: import junit.framework.TestSuite;
040:
041: import org.codehaus.janino.ClassLoaderIClassLoader;
042: import org.codehaus.janino.DebuggingInformation;
043: import org.codehaus.janino.IClassLoader;
044: import org.codehaus.janino.Parser;
045: import org.codehaus.janino.Scanner;
046: import org.codehaus.janino.UnitCompiler;
047:
048: import org.objectweb.asm.AbstractTest;
049: import org.objectweb.asm.Attribute;
050: import org.objectweb.asm.ClassAdapter;
051: import org.objectweb.asm.ClassReader;
052: import org.objectweb.asm.ClassWriter;
053: import org.objectweb.asm.MethodAdapter;
054: import org.objectweb.asm.MethodVisitor;
055: import org.objectweb.asm.attrs.CodeComment;
056: import org.objectweb.asm.attrs.Comment;
057:
058: /**
059: * GASMifier tests.
060: *
061: * @author Eugene Kuleshov
062: * @author Eric Bruneton
063: */
064: public class GASMifierTest extends AbstractTest {
065:
066: public static final Compiler COMPILER = new Compiler();
067:
068: private final static TestClassLoader LOADER = new TestClassLoader();
069:
070: public static TestSuite suite() throws Exception {
071: return new GASMifierTest().getSuite();
072: }
073:
074: public void test() throws Exception {
075: ClassReader cr = new ClassReader(is);
076:
077: if (cr.b.length > 20000) {
078: return;
079: }
080:
081: StringWriter sw = new StringWriter();
082: GASMifierClassVisitor cv = new GASMifierClassVisitor(
083: new PrintWriter(sw));
084: cr.accept(cv, new Attribute[] { new Comment(),
085: new CodeComment() }, ClassReader.EXPAND_FRAMES);
086:
087: String generated = sw.toString();
088:
089: byte[] generatorClassData;
090: try {
091: generatorClassData = COMPILER.compile(n, generated);
092: } catch (Exception ex) {
093: trace(generated);
094: throw ex;
095: }
096:
097: ClassWriter cw = new ClassWriter(0);
098: cr.accept(new ClassAdapter(cw) {
099: public MethodVisitor visitMethod(final int access,
100: final String name, final String desc,
101: final String signature, final String[] exceptions) {
102: return new LocalVariablesSorter(access, desc, super
103: .visitMethod(access, name, desc, signature,
104: exceptions));
105: }
106: }, new Attribute[] { new Comment(), new CodeComment() },
107: ClassReader.EXPAND_FRAMES);
108: cr = new ClassReader(cw.toByteArray());
109:
110: String nd = n + "Dump";
111: if (n.indexOf('.') != -1) {
112: nd = "asm." + nd;
113: }
114:
115: Class c = LOADER.defineClass(nd, generatorClassData);
116: Method m = c.getMethod("dump", new Class[0]);
117: byte[] b;
118: try {
119: b = (byte[]) m.invoke(null, new Object[0]);
120: } catch (InvocationTargetException ex) {
121: trace(generated);
122: throw (Exception) ex.getTargetException();
123: }
124:
125: try {
126: assertEquals(cr, new ClassReader(b), new Filter(),
127: new Filter());
128: } catch (Throwable e) {
129: trace(generated);
130: assertEquals(cr, new ClassReader(b), new Filter(),
131: new Filter());
132: }
133: }
134:
135: private void trace(final String generated) {
136: if (System.getProperty("asm.test.class") != null) {
137: System.err.println(generated);
138: }
139: }
140:
141: private static class TestClassLoader extends ClassLoader {
142:
143: public Class defineClass(final String name, final byte[] b) {
144: return defineClass(name, b, 0, b.length);
145: }
146: }
147:
148: private static class Compiler {
149:
150: final static IClassLoader CL = new ClassLoaderIClassLoader(
151: new URLClassLoader(new URL[0]));
152:
153: public byte[] compile(final String name, final String source)
154: throws Exception {
155: Parser p = new Parser(new Scanner(name, new StringReader(
156: source)));
157: UnitCompiler uc = new UnitCompiler(
158: p.parseCompilationUnit(), CL);
159: return uc.compileUnit(DebuggingInformation.ALL)[0]
160: .toByteArray();
161: }
162: }
163:
164: private static class Filter extends ClassAdapter {
165:
166: public Filter() {
167: super (null);
168: }
169:
170: public MethodVisitor visitMethod(final int access,
171: final String name, final String desc,
172: final String signature, final String[] exceptions) {
173: return new MethodAdapter(super .visitMethod(access, name,
174: desc, signature, exceptions)) {
175: public void visitMaxs(final int maxStack,
176: final int maxLocals) {
177: super .visitMaxs(0, 0);
178: }
179: };
180: }
181: }
182: }
|