KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > weblogic > transform > TerracottaServletResponseImplAdapter


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

5 package com.tc.weblogic.transform;
6
7 import com.tc.asm.ClassAdapter;
8 import com.tc.asm.ClassVisitor;
9 import com.tc.asm.MethodVisitor;
10 import com.tc.asm.Opcodes;
11 import com.tc.asm.Type;
12 import com.tc.object.bytecode.ClassAdapterFactory;
13
14 import java.lang.reflect.Method JavaDoc;
15 import java.lang.reflect.Modifier JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 public class TerracottaServletResponseImplAdapter extends ClassAdapter implements Opcodes, ClassAdapterFactory {
21
22   private static final String JavaDoc CLASS_NAME = "weblogic.servlet.internal.ServletResponseImpl";
23
24   private final Map JavaDoc nativeMethods;
25   private String JavaDoc thisClassname;
26
27   public TerracottaServletResponseImplAdapter() {
28     super(null);
29     this.nativeMethods = null;
30   }
31
32   private TerracottaServletResponseImplAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
33     super(cv);
34     this.nativeMethods = getNativeMethods(caller);
35   }
36
37   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
38     return new TerracottaServletResponseImplAdapter(visitor, loader);
39   }
40
41   public void visit(int version, int access, String JavaDoc name, String JavaDoc signature, String JavaDoc superName, String JavaDoc[] interfaces) {
42     super.visit(version, access, name, signature, superName, interfaces);
43     this.thisClassname = name;
44   }
45
46   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
47     String JavaDoc sig = name + Type.getMethodDescriptor(Type.getReturnType(desc), Type.getArgumentTypes(desc));
48     nativeMethods.remove(sig);
49     return super.visitMethod(access, name, desc, signature, exceptions);
50   }
51
52   public void visitEnd() {
53     for (Iterator JavaDoc iter = nativeMethods.values().iterator(); iter.hasNext();) {
54       Method JavaDoc m = (Method JavaDoc) iter.next();
55
56       Class JavaDoc[] exceptionTypes = m.getExceptionTypes();
57       String JavaDoc[] exceptions = new String JavaDoc[exceptionTypes.length];
58       for (int i = 0; i < exceptions.length; i++) {
59         exceptions[i] = exceptionTypes[i].getName().replace('.', '/');
60       }
61
62       Type[] argumentTypes = Type.getArgumentTypes(m);
63
64       MethodVisitor mv = super
65           .visitMethod(m.getModifiers(), m.getName(), Type.getMethodDescriptor(m), null, exceptions);
66       mv.visitCode();
67
68       mv.visitVarInsn(ALOAD, 0);
69       mv.visitFieldInsn(GETFIELD, thisClassname, "nativeResponse", "Lweblogic/servlet/internal/ServletResponseImpl;");
70
71       int slot = 1;
72       for (int i = 0; i < argumentTypes.length; i++) {
73         Type arg = argumentTypes[i];
74         mv.visitVarInsn(arg.getOpcode(ILOAD), slot);
75         slot += arg.getSize();
76       }
77
78       mv.visitMethodInsn(INVOKEVIRTUAL, "weblogic/servlet/internal/ServletResponseImpl", m.getName(), Type
79           .getMethodDescriptor(m));
80
81       Type returnType = Type.getReturnType(m);
82       if (returnType == Type.VOID_TYPE) {
83         mv.visitInsn(RETURN);
84       } else {
85         mv.visitInsn(returnType.getOpcode(IRETURN));
86       }
87
88       mv.visitMaxs(0, 0);
89       mv.visitEnd();
90     }
91   }
92
93   private static Map JavaDoc getNativeMethods(ClassLoader JavaDoc caller) {
94     Class JavaDoc c;
95     try {
96       c = Class.forName(CLASS_NAME, false, caller);
97     } catch (ClassNotFoundException JavaDoc e) {
98       throw new RuntimeException JavaDoc("Unable to load class " + CLASS_NAME);
99     }
100
101     Map JavaDoc rv = new HashMap JavaDoc();
102     Method JavaDoc[] methods = c.getDeclaredMethods();
103     for (int i = 0; i < methods.length; i++) {
104       Method JavaDoc m = methods[i];
105
106       int access = m.getModifiers();
107
108       if (Modifier.isStatic(access) || Modifier.isPrivate(access)) {
109         continue;
110       }
111
112       if (Modifier.isFinal(access)) { throw new RuntimeException JavaDoc("Final modifier found (should have been removed): "
113                                                                  + m.toString()); }
114
115       String JavaDoc sig = m.getName() + Type.getMethodDescriptor(m);
116       Object JavaDoc prev = rv.put(sig, m);
117       if (prev != null) { throw new AssertionError JavaDoc("replaced mapping for " + sig); }
118     }
119     return rv;
120   }
121 }
122
Popular Tags