KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > TreeMapAdapter


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.object.bytecode;
5
6
7 import com.tc.asm.ClassVisitor;
8 import com.tc.asm.MethodAdapter;
9 import com.tc.asm.MethodVisitor;
10 import com.tc.asm.Opcodes;
11 import com.tc.asm.Type;
12 import com.tc.object.SerializationUtil;
13 import com.tcclient.util.MapEntrySetWrapper;
14
15 public class TreeMapAdapter {
16
17   public static class EntrySetAdapter extends AbstractMethodAdapter {
18
19     public MethodVisitor adapt(ClassVisitor classVisitor) {
20       return new Adapter(visitOriginal(classVisitor));
21     }
22
23     public boolean doesOriginalNeedAdapting() {
24       return false;
25     }
26
27     private class Adapter extends MethodAdapter implements Opcodes {
28       public Adapter(MethodVisitor mv) {
29         super(mv);
30       }
31
32       public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
33         super.visitMethodInsn(opcode, owner, name, desc);
34
35         if ((opcode == INVOKESPECIAL) && "<init>".equals(name) & "java/util/TreeMap$3".equals(owner)) {
36           mv.visitVarInsn(ASTORE, 1);
37           mv.visitTypeInsn(NEW, MapEntrySetWrapper.CLASS_SLASH);
38           mv.visitInsn(DUP);
39           mv.visitVarInsn(ALOAD, 0);
40           mv.visitVarInsn(ALOAD, 1);
41           mv.visitMethodInsn(INVOKESPECIAL, MapEntrySetWrapper.CLASS_SLASH, "<init>",
42                              "(Ljava/util/Map;Ljava/util/Set;)V");
43         }
44       }
45     }
46
47   }
48
49   public static class DeleteEntryAdapter extends AbstractMethodAdapter {
50
51     public MethodVisitor adapt(ClassVisitor classVisitor) {
52       return new Adapter(visitOriginal(classVisitor));
53     }
54
55     public boolean doesOriginalNeedAdapting() {
56       return false;
57     }
58
59     private class Adapter extends MethodAdapter implements Opcodes {
60       public Adapter(MethodVisitor mv) {
61         super(mv);
62
63         mv.visitVarInsn(ALOAD, 0);
64         mv.visitLdcInsn(SerializationUtil.REMOVE_KEY_SIGNATURE);
65         mv.visitLdcInsn(new Integer JavaDoc(1));
66         mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
67         mv.visitInsn(DUP);
68         mv.visitLdcInsn(new Integer JavaDoc(0));
69         mv.visitVarInsn(ALOAD, 1);
70         mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/TreeMap$Entry", "getKey", "()Ljava/lang/Object;");
71         mv.visitInsn(AASTORE);
72         managerHelper.callManagerMethod("logicalInvoke", mv);
73       }
74     }
75   }
76
77   public static class PutAdapter extends AbstractMethodAdapter {
78
79     public MethodVisitor adapt(ClassVisitor classVisitor) {
80       return new Adapter(visitOriginal(classVisitor));
81     }
82     
83     protected MethodVisitor visitOriginal(ClassVisitor classVisitor) {
84       MethodVisitor mv = super.visitOriginal(classVisitor);
85       mv.visitVarInsn(ALOAD, 0);
86       managerHelper.callManagerMethod("checkWriteAccess", mv);
87
88       return mv;
89     }
90
91     public boolean doesOriginalNeedAdapting() {
92       return false;
93     }
94
95     private class Adapter extends MethodAdapter implements Opcodes {
96
97       public Adapter(MethodVisitor mv) {
98         super(mv);
99       }
100
101       public void visitMethodInsn(int opcode, String JavaDoc className, String JavaDoc method, String JavaDoc desc) {
102         super.visitMethodInsn(opcode, className, method, desc);
103         
104         if ((INVOKESPECIAL == opcode) && "<init>".equals(method) && "java/util/TreeMap$Entry".equals(className)) {
105           mv.visitVarInsn(ALOAD, 0);
106           mv.visitLdcInsn(methodName + description);
107           ByteCodeUtil.createParametersToArrayByteCode(mv, Type.getArgumentTypes(description));
108           managerHelper.callManagerMethod("logicalInvoke", mv);
109         }
110
111         if ((INVOKEVIRTUAL == opcode) && "setValue".equals(method) && "java/util/TreeMap$Entry".equals(className)) {
112           mv.visitInsn(DUP);
113           mv.visitVarInsn(ALOAD, 0);
114           mv.visitLdcInsn(methodName + description);
115           mv.visitLdcInsn(new Integer JavaDoc(2));
116           mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
117           mv.visitInsn(DUP);
118           mv.visitInsn(DUP);
119           mv.visitLdcInsn(new Integer JavaDoc(0));
120           mv.visitVarInsn(ALOAD, 3);
121           mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/TreeMap$Entry", "getKey", "()Ljava/lang/Object;");
122           mv.visitInsn(AASTORE);
123           mv.visitLdcInsn(new Integer JavaDoc(1));
124           mv.visitVarInsn(ALOAD, 2);
125           mv.visitInsn(AASTORE);
126           managerHelper.callManagerMethod("logicalInvoke", mv);
127         }
128
129       }
130     }
131   }
132 }
Popular Tags