KickJava   Java API By Example, From Geeks To Geeks.

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


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.commons.LocalVariablesSorter;
12 import com.tcclient.util.HashtableEntrySetWrapper;
13
14 public class HashtableAdapter {
15
16   public static class EntrySetAdapter extends AbstractMethodAdapter {
17
18     public MethodVisitor adapt(ClassVisitor classVisitor) {
19       MethodVisitor mv = classVisitor.visitMethod(access, methodName, description, signature, exceptions);
20       return new Adapter(mv);
21     }
22
23     public boolean doesOriginalNeedAdapting() {
24       return true;
25     }
26
27     public static class Adapter extends MethodAdapter implements Opcodes {
28
29       public Adapter(MethodVisitor mv) {
30         super(mv);
31       }
32
33       public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
34         if ((INVOKESTATIC == opcode) && ("java/util/Collections".equals(owner)) && ("synchronizedSet".equals(name))) {
35           mv.visitInsn(POP);
36           mv.visitVarInsn(ASTORE, 1);
37           mv.visitTypeInsn(NEW, HashtableEntrySetWrapper.CLASS);
38           mv.visitInsn(DUP);
39           mv.visitVarInsn(ALOAD, 0);
40           mv.visitVarInsn(ALOAD, 1);
41           mv.visitMethodInsn(INVOKESPECIAL, HashtableEntrySetWrapper.CLASS, "<init>",
42                              "(Ljava/util/Map;Ljava/util/Set;)V");
43           mv.visitVarInsn(ALOAD, 0);
44         }
45
46         super.visitMethodInsn(opcode, owner, name, desc);
47       }
48
49     }
50
51   }
52
53   public static class KeySetAdapter extends AbstractMethodAdapter {
54
55     public MethodVisitor adapt(ClassVisitor classVisitor) {
56       MethodVisitor mv = classVisitor.visitMethod(access, methodName, description, signature, exceptions);
57       return new Adapter(mv);
58     }
59
60     public boolean doesOriginalNeedAdapting() {
61       return true;
62     }
63
64     public static class Adapter extends MethodAdapter implements Opcodes {
65
66       public Adapter(MethodVisitor mv) {
67         super(mv);
68       }
69
70       public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
71         if ((INVOKESTATIC == opcode) && ("java/util/Collections".equals(owner)) && ("synchronizedSet".equals(name))) {
72           mv.visitInsn(POP);
73           mv.visitVarInsn(ASTORE, 1);
74           mv.visitTypeInsn(NEW, "com/tc/util/HashtableKeySetWrapper");
75           mv.visitInsn(DUP);
76           mv.visitVarInsn(ALOAD, 0);
77           mv.visitVarInsn(ALOAD, 1);
78           mv.visitMethodInsn(INVOKESPECIAL, "com/tc/util/HashtableKeySetWrapper", "<init>",
79                              "(Ljava/util/Hashtable;Ljava/util/Set;)V");
80           mv.visitVarInsn(ALOAD, 0);
81         }
82
83         super.visitMethodInsn(opcode, owner, name, desc);
84       }
85
86     }
87
88   }
89
90   public static class ValuesAdapter extends AbstractMethodAdapter {
91
92     public MethodVisitor adapt(ClassVisitor classVisitor) {
93       MethodVisitor mv = classVisitor.visitMethod(access, methodName, description, signature, exceptions);
94       return new Adapter(mv);
95     }
96
97     public boolean doesOriginalNeedAdapting() {
98       return true;
99     }
100
101     public static class Adapter extends MethodAdapter implements Opcodes {
102
103       public Adapter(MethodVisitor mv) {
104         super(mv);
105       }
106
107       public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
108         if ((INVOKESTATIC == opcode) && ("java/util/Collections".equals(owner))
109             && ("synchronizedCollection".equals(name))) {
110           mv.visitInsn(POP);
111           mv.visitVarInsn(ASTORE, 1);
112           mv.visitTypeInsn(NEW, "com/tc/util/HashtableValuesWrapper");
113           mv.visitInsn(DUP);
114           mv.visitVarInsn(ALOAD, 0);
115           mv.visitVarInsn(ALOAD, 1);
116           mv.visitMethodInsn(INVOKESPECIAL, "com/tc/util/HashtableValuesWrapper", "<init>",
117                              "(Ljava/util/Hashtable;Ljava/util/Collection;)V");
118           mv.visitVarInsn(ALOAD, 0);
119         }
120
121         super.visitMethodInsn(opcode, owner, name, desc);
122       }
123
124     }
125   }
126
127   public static class IteratorAdapter extends AbstractMethodAdapter {
128     public MethodVisitor adapt(ClassVisitor classVisitor) {
129       MethodVisitor mv = classVisitor.visitMethod(access, methodName, description, signature, exceptions);
130       return new Adapter(access, description, mv);
131     }
132
133     public boolean doesOriginalNeedAdapting() {
134       return false;
135     }
136
137     public static class Adapter extends LocalVariablesSorter implements Opcodes {
138
139       public Adapter(final int access, final String JavaDoc desc, final MethodVisitor mv) {
140         super(access, desc, mv);
141       }
142
143       public void visitInsn(final int opcode) {
144         if (MONITORENTER == opcode) {
145           int newVarIndex = newLocal(1);
146           mv.visitInsn(DUP);
147           mv.visitInsn(DUP);
148           mv.visitVarInsn(ASTORE, newVarIndex);
149           mv.visitInsn(ICONST_2);
150           mv.visitVarInsn(ALOAD, newVarIndex);
151
152           mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "monitorEnter",
153                              "(Ljava/lang/Object;ILcom/tc/object/bytecode/Manager;)V");
154         }
155         super.visitInsn(opcode);
156       }
157     }
158
159   }
160 }
161
Popular Tags