KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > geronimo > transform > ProxyMethodInterceptorAdapter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.geronimo.transform;
5
6 import com.tc.asm.ClassAdapter;
7 import com.tc.asm.ClassVisitor;
8 import com.tc.asm.Label;
9 import com.tc.asm.MethodAdapter;
10 import com.tc.asm.MethodVisitor;
11 import com.tc.asm.Opcodes;
12 import com.tc.object.bytecode.ByteCodeUtil;
13 import com.tc.object.bytecode.ClassAdapterFactory;
14
15 public class ProxyMethodInterceptorAdapter extends ClassAdapter implements ClassAdapterFactory {
16
17   public ProxyMethodInterceptorAdapter() {
18     super(null);
19   }
20   
21   private ProxyMethodInterceptorAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
22     super(cv);
23   }
24
25   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
26     return new ProxyMethodInterceptorAdapter(visitor, loader);
27   }
28   
29   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
30     // public final Object intercept(Object object, Method method, Object args[], MethodProxy proxy)
31

32     MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
33
34     if ("intercept".equals(name)) { return new InterceptAdapter(mv); }
35
36     return mv;
37
38   }
39
40   private static class InterceptAdapter extends MethodAdapter implements Opcodes {
41
42     public InterceptAdapter(MethodVisitor mv) {
43       super(mv);
44     }
45
46     public void visitCode() {
47       super.visitCode();
48
49       // For DSO introduced methods -- no interceptor exists, just call super in this
50
mv.visitVarInsn(ALOAD, 2);
51       mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "getName", "()Ljava/lang/String;");
52       mv.visitLdcInsn(ByteCodeUtil.TC_METHOD_PREFIX);
53       mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "startsWith", "(Ljava/lang/String;)Z");
54       Label notTC = new Label();
55       mv.visitJumpInsn(IFEQ, notTC);
56       mv.visitVarInsn(ALOAD, 4);
57       mv.visitVarInsn(ALOAD, 1);
58       mv.visitVarInsn(ALOAD, 3);
59       mv.visitMethodInsn(INVOKEVIRTUAL, "net/sf/cglib/proxy/MethodProxy", "invokeSuper",
60                          "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
61       mv.visitInsn(ARETURN);
62       mv.visitLabel(notTC);
63     }
64
65   }
66
67 }
68
Popular Tags