KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.io.StringWriter JavaDoc;
35 import java.lang.instrument.ClassFileTransformer JavaDoc;
36 import java.lang.instrument.IllegalClassFormatException JavaDoc;
37 import java.lang.instrument.Instrumentation JavaDoc;
38 import java.security.ProtectionDomain JavaDoc;
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 /**
51  * LocalVariablesSorter tests.
52  *
53  * @author Eric Bruneton
54  */

55 public class LocalVariablesSorterTest2 extends AbstractTest {
56
57     public static void premain(
58         final String JavaDoc agentArgs,
59         final Instrumentation JavaDoc inst)
60     {
61         inst.addTransformer(new ClassFileTransformer JavaDoc() {
62             public byte[] transform(
63                 final ClassLoader JavaDoc loader,
64                 final String JavaDoc className,
65                 final Class JavaDoc classBeingRedefined,
66                 final ProtectionDomain JavaDoc domain,
67                 final byte[] classFileBuffer)
68                     throws IllegalClassFormatException JavaDoc
69             {
70                 String JavaDoc 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 JavaDoc name,
88                 final String JavaDoc desc,
89                 final String JavaDoc signature,
90                 final String JavaDoc[] 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 JavaDoc {
106         return new LocalVariablesSorterTest2().getSuite();
107     }
108
109     public void test() throws Exception JavaDoc {
110         try {
111             Class.forName(n, true, getClass().getClassLoader());
112         } catch (NoClassDefFoundError JavaDoc ncdfe) {
113             // ignored
114
} catch (UnsatisfiedLinkError JavaDoc ule) {
115             // ignored
116
} catch (ClassFormatError JavaDoc cfe) {
117             fail(cfe.getMessage());
118         } catch (VerifyError JavaDoc ve) {
119             String JavaDoc s = n.replace('.', '/') + ".class";
120             InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream(s);
121             ClassReader cr = new ClassReader(is);
122             byte[] b = transformClass(cr.b);
123             StringWriter JavaDoc sw1 = new StringWriter JavaDoc();
124             StringWriter JavaDoc sw2 = new StringWriter JavaDoc();
125             sw2.write(ve.toString() + "\n");
126             ClassVisitor cv1 = new TraceClassVisitor(new PrintWriter JavaDoc(sw1));
127             ClassVisitor cv2 = new TraceClassVisitor(new PrintWriter JavaDoc(sw2));
128             cr.accept(cv1, 0);
129             new ClassReader(b).accept(cv2, 0);
130             String JavaDoc s1 = sw1.toString();
131             String JavaDoc s2 = sw2.toString();
132             assertEquals("different data", s1, s2);
133         }
134     }
135 }
136
Popular Tags