KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35
36 import org.objectweb.asm.AbstractTest;
37 import org.objectweb.asm.ClassAdapter;
38 import org.objectweb.asm.ClassReader;
39 import org.objectweb.asm.ClassVisitor;
40 import org.objectweb.asm.ClassWriter;
41 import org.objectweb.asm.MethodVisitor;
42 import org.objectweb.asm.Opcodes;
43
44 /**
45  * Simple example of using AdviceAdapter to implement tracing callback
46  *
47  * @author Eugene Kuleshov
48  */

49 public class AdviceAdapterUnitTest extends AbstractTest {
50
51     public void test() throws Exception JavaDoc {
52         Class JavaDoc c = getClass();
53         String JavaDoc name = c.getName();
54         AdvisingClassLoader cl = new AdvisingClassLoader(name + "$");
55         Class JavaDoc cc = cl.loadClass(name + "$B");
56         Method m = cc.getMethod("run", new Class JavaDoc[] { Integer.TYPE });
57         try {
58             m.invoke(null, new Object JavaDoc[] { new Integer JavaDoc(0) });
59         } catch (InvocationTargetException JavaDoc e) {
60             throw (Exception JavaDoc) e.getTargetException();
61         }
62     }
63
64     private static class AdvisingClassLoader extends ClassLoader JavaDoc {
65         private String JavaDoc prefix;
66
67         public AdvisingClassLoader(final String JavaDoc prefix) throws IOException JavaDoc {
68             this.prefix = prefix;
69         }
70
71         public Class JavaDoc loadClass(final String JavaDoc name) throws ClassNotFoundException JavaDoc
72         {
73             if (name.startsWith(prefix)) {
74                 try {
75                     ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
76                     ClassReader cr = new ClassReader(getClass().getResourceAsStream("/"
77                             + name.replace('.', '/') + ".class"));
78                     cr.accept(new AdviceClassAdapter(cw),
79                             ClassReader.EXPAND_FRAMES);
80                     byte[] bytecode = cw.toByteArray();
81                     return super.defineClass(name, bytecode, 0, bytecode.length);
82                 } catch (IOException JavaDoc ex) {
83                     throw new ClassNotFoundException JavaDoc("Load error: "
84                             + ex.toString(), ex);
85                 }
86             }
87             return super.loadClass(name);
88         }
89
90     }
91
92     // test callback
93
private static int n = 0;
94
95     public static void enter(final String JavaDoc msg) {
96         System.err.println(off().append("enter ").append(msg).toString());
97         n++;
98     }
99
100     public static void exit(final String JavaDoc msg) {
101         n--;
102         System.err.println(off().append("<").toString());
103     }
104
105     private static StringBuffer JavaDoc off() {
106         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
107         for (int i = 0; i < n; i++) {
108             sb.append(" ");
109         }
110         return sb;
111     }
112
113     static class AdviceClassAdapter extends ClassAdapter implements Opcodes {
114         private String JavaDoc cname;
115
116         public AdviceClassAdapter(final ClassVisitor cv) {
117             super(cv);
118         }
119
120         public void visit(
121             final int version,
122             final int access,
123             final String JavaDoc name,
124             final String JavaDoc signature,
125             final String JavaDoc superName,
126             final String JavaDoc[] interfaces)
127         {
128             this.cname = name;
129             super.visit(version, access, name, signature, superName, interfaces);
130         }
131
132         public MethodVisitor visitMethod(
133             final int access,
134             final String JavaDoc name,
135             final String JavaDoc desc,
136             final String JavaDoc signature,
137             final String JavaDoc[] exceptions)
138         {
139             MethodVisitor mv = cv.visitMethod(access,
140                     name,
141                     desc,
142                     signature,
143                     exceptions);
144
145             if (mv == null
146                     || (access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) > 0)
147             {
148                 return mv;
149             }
150
151             return new AdviceAdapter(mv, access, name, desc) {
152                 protected void onMethodEnter() {
153                     mv.visitLdcInsn(cname + "." + name + desc);
154                     mv.visitMethodInsn(INVOKESTATIC,
155                             "org/objectweb/asm/commons/AdviceAdapterUnitTest",
156                             "enter",
157                             "(Ljava/lang/String;)V");
158                 }
159
160                 protected void onMethodExit(final int opcode) {
161                     mv.visitLdcInsn(cname + "." + name + desc);
162                     mv.visitMethodInsn(INVOKESTATIC,
163                             "org/objectweb/asm/commons/AdviceAdapterUnitTest",
164                             "exit",
165                             "(Ljava/lang/String;)V");
166                 }
167
168             };
169         }
170     }
171
172     // TEST CLASSES
173

174     public static class A {
175         final String JavaDoc s;
176
177         public A(final String JavaDoc s) {
178             this.s = s;
179         }
180
181         public A(final A a) {
182             this.s = a.s;
183         }
184     }
185
186     public static class B extends A {
187
188         public B() {
189             super(new B(""));
190             test(this);
191         }
192
193         public B(final A a) {
194             super(a);
195             test(this);
196         }
197
198         public B(final String JavaDoc s) {
199             super(s == null ? new A("") : new A(s));
200             test(this);
201         }
202
203         private static A aa;
204
205         public B(final String JavaDoc s, final A a) {
206             this(s == null ? aa = new A(s) : a);
207             A aa = new A("");
208             test(aa);
209         }
210
211         public B(final String JavaDoc s, final String JavaDoc s1) {
212             super(s != null ? new A(getA(s1).s) : new A(s));
213             test(this);
214         }
215
216         private void test(final Object JavaDoc b) {
217         }
218
219         private static A getA(final String JavaDoc s) {
220             return new A(s);
221         }
222
223         // execute all
224
public static void run(final int n) {
225             new B();
226             new B(new A(""));
227             new B(new B());
228             new B("", new A(""));
229             new B("", "");
230         }
231
232     }
233 }
234
Popular Tags