KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > ClassWriterResizeInsnsTest


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;
31
32 import java.lang.instrument.ClassFileTransformer JavaDoc;
33 import java.lang.instrument.IllegalClassFormatException JavaDoc;
34 import java.lang.instrument.Instrumentation JavaDoc;
35 import java.security.ProtectionDomain JavaDoc;
36 import java.util.HashSet JavaDoc;
37
38 import org.objectweb.asm.attrs.CodeComment;
39
40 import junit.framework.TestSuite;
41
42 public class ClassWriterResizeInsnsTest extends AbstractTest {
43
44     public static void premain(
45         final String JavaDoc agentArgs,
46         final Instrumentation JavaDoc inst)
47     {
48         inst.addTransformer(new ClassFileTransformer JavaDoc() {
49             public byte[] transform(
50                 final ClassLoader JavaDoc loader,
51                 final String JavaDoc className,
52                 final Class JavaDoc classBeingRedefined,
53                 final ProtectionDomain JavaDoc domain,
54                 byte[] b) throws IllegalClassFormatException JavaDoc
55             {
56                 String JavaDoc n = className.replace('/', '.');
57                 if (agentArgs.length() == 0 || n.indexOf(agentArgs) != -1) {
58                     try {
59                         b = transformClass(b, ClassWriter.COMPUTE_FRAMES);
60                         if (n.equals("pkg.FrameMap")) {
61                             transformClass(b, 0);
62                         }
63                         return b;
64                     } catch (Throwable JavaDoc e) {
65                         return transformClass(b, 0);
66                     }
67                 } else {
68                     return null;
69                 }
70             }
71         });
72     }
73
74     private static byte[] transformClass(final byte[] clazz, final int flags) {
75         ClassReader cr = new ClassReader(clazz);
76         ClassWriter cw = new ClassWriter(flags) {
77             protected String JavaDoc getCommonSuperClass(
78                 final String JavaDoc type1,
79                 final String JavaDoc type2)
80             {
81                 ClassInfo c, d;
82                 try {
83                     c = new ClassInfo(type1, getClass().getClassLoader());
84                     d = new ClassInfo(type2, getClass().getClassLoader());
85                 } catch (Throwable JavaDoc e) {
86                     throw new RuntimeException JavaDoc(e);
87                 }
88                 if (c.isAssignableFrom(d)) {
89                     return type1;
90                 }
91                 if (d.isAssignableFrom(c)) {
92                     return type2;
93                 }
94                 if (c.isInterface() || d.isInterface()) {
95                     return "java/lang/Object";
96                 } else {
97                     do {
98                         c = c.getSuperclass();
99                     } while (!c.isAssignableFrom(d));
100                     return c.getType().getInternalName();
101                 }
102             }
103         };
104         ClassAdapter ca = new ClassAdapter(cw) {
105
106             private boolean transformed = false;
107
108             public void visit(
109                 int version,
110                 int access,
111                 String JavaDoc name,
112                 String JavaDoc signature,
113                 String JavaDoc superName,
114                 String JavaDoc[] interfaces)
115             {
116                 if (flags == ClassWriter.COMPUTE_FRAMES) {
117                     version = Opcodes.V1_6;
118                 }
119                 super.visit(version,
120                         access,
121                         name,
122                         signature,
123                         superName,
124                         interfaces);
125             }
126
127             public MethodVisitor visitMethod(
128                 final int access,
129                 final String JavaDoc name,
130                 final String JavaDoc desc,
131                 final String JavaDoc signature,
132                 final String JavaDoc[] exceptions)
133             {
134                 return new MethodAdapter(cv.visitMethod(access,
135                         name,
136                         desc,
137                         signature,
138                         exceptions))
139                 {
140                     private HashSet JavaDoc labels = new HashSet JavaDoc();
141
142                     public void visitLabel(final Label label) {
143                         super.visitLabel(label);
144                         labels.add(label);
145                     }
146
147                     public void visitJumpInsn(
148                         final int opcode,
149                         final Label label)
150                     {
151                         super.visitJumpInsn(opcode, label);
152                         if (opcode != Opcodes.GOTO) {
153                             if (!transformed && !labels.contains(label)) {
154                                 transformed = true;
155                                 for (int i = 0; i < 33000; ++i) {
156                                     mv.visitInsn(Opcodes.NOP);
157                                 }
158                             }
159                         }
160                     }
161                 };
162             }
163         };
164         cr.accept(ca, new Attribute[] { new CodeComment() }, 0);
165         return cw.toByteArray();
166     }
167
168     public static TestSuite suite() throws Exception JavaDoc {
169         return new ClassWriterResizeInsnsTest().getSuite();
170     }
171
172     public void test() throws Exception JavaDoc {
173         try {
174             Class.forName(n, true, getClass().getClassLoader());
175         } catch (NoClassDefFoundError JavaDoc ncdfe) {
176             // ignored
177
} catch (UnsatisfiedLinkError JavaDoc ule) {
178             // ignored
179
} catch (ClassFormatError JavaDoc cfe) {
180             fail(cfe.getMessage());
181         } catch (VerifyError JavaDoc ve) {
182             fail(ve.toString());
183         }
184     }
185 }
186
Popular Tags