KickJava   Java API By Example, From Geeks To Geeks.

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


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.object.bytecode;
6
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.SerializationUtil;
13
14 public class JavaUtilConcurrentLinkedBlockingQueueAdapter implements Opcodes {
15
16   public static class PutAdapter extends AbstractMethodAdapter {
17
18     public MethodVisitor adapt(ClassVisitor classVisitor) {
19       return new PutMethodAdapter(visitOriginal(classVisitor), SerializationUtil.QUEUE_PUT_SIGNATURE);
20     }
21
22     public boolean doesOriginalNeedAdapting() {
23       return false;
24     }
25   }
26
27   public static class ClearAdapter extends AbstractMethodAdapter {
28     public MethodVisitor adapt(ClassVisitor classVisitor) {
29       return new ClearMethodAdapter(visitOriginal(classVisitor), SerializationUtil.CLEAR_SIGNATURE);
30     }
31
32     public boolean doesOriginalNeedAdapting() {
33       return false;
34     }
35   }
36
37   public static class RemoveFirstNAdapter extends AbstractMethodAdapter {
38     public MethodVisitor adapt(ClassVisitor classVisitor) {
39       return new RemoveFirstNMethodAdapter(visitOriginal(classVisitor), SerializationUtil.REMOVE_FIRST_N_SIGNATURE);
40     }
41
42     public boolean doesOriginalNeedAdapting() {
43       return false;
44     }
45   }
46
47   public static class TakeAdapter extends AbstractMethodAdapter {
48
49     public MethodVisitor adapt(ClassVisitor classVisitor) {
50       return new TakeMethodAdapter(visitOriginal(classVisitor), SerializationUtil.TAKE_SIGNATURE);
51     }
52
53     public boolean doesOriginalNeedAdapting() {
54       return false;
55     }
56   }
57
58   private static class ClearMethodAdapter extends MethodAdapter implements Opcodes {
59     private final String JavaDoc invokeMethodSignature;
60
61     public ClearMethodAdapter(MethodVisitor mv, String JavaDoc invokeMethodSignature) {
62       super(mv);
63       this.invokeMethodSignature = invokeMethodSignature;
64     }
65
66     public void visitFieldInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
67       super.visitFieldInsn(opcode, owner, name, desc);
68       if (PUTFIELD == opcode && "next".equals(name)) {
69         addLogicalInvokeMethodCall();
70       }
71     }
72
73     private void addLogicalInvokeMethodCall() {
74       Label notManaged = new Label();
75       addCheckedManagedCode(mv, notManaged);
76       ByteCodeUtil.pushThis(mv);
77
78       mv.visitLdcInsn(invokeMethodSignature);
79
80       mv.visitLdcInsn(new Integer JavaDoc(0));
81       mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
82       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "logicalInvoke",
83                          "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V");
84       mv.visitLabel(notManaged);
85     }
86   }
87
88   private static class RemoveFirstNMethodAdapter extends MethodAdapter implements Opcodes {
89     private final String JavaDoc invokeMethodSignature;
90
91     public RemoveFirstNMethodAdapter(MethodVisitor mv, String JavaDoc invokeMethodSignature) {
92       super(mv);
93       this.invokeMethodSignature = invokeMethodSignature;
94     }
95
96     public void visitFieldInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
97       super.visitFieldInsn(opcode, owner, name, desc);
98       if (PUTFIELD == opcode && "next".equals(name) && "Ljava/util/concurrent/LinkedBlockingQueue$Node;".equals(desc)) {
99         addLogicalInvokeMethodCall();
100       }
101     }
102
103     private void addLogicalInvokeMethodCall() {
104       Label notManaged = new Label();
105       addCheckedManagedCode(mv, notManaged);
106       ByteCodeUtil.pushThis(mv);
107
108       mv.visitLdcInsn(invokeMethodSignature);
109
110       mv.visitLdcInsn(new Integer JavaDoc(1));
111       mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
112
113       int count = 0;
114       mv.visitInsn(DUP);
115       mv.visitLdcInsn(new Integer JavaDoc(count++));
116       mv.visitTypeInsn(NEW, "java/lang/Integer");
117       mv.visitInsn(DUP);
118       mv.visitVarInsn(ILOAD, 3);
119       mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Integer", "<init>", "(I)V");
120       mv.visitInsn(AASTORE);
121
122       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "logicalInvoke",
123                          "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V");
124       mv.visitLabel(notManaged);
125     }
126   }
127
128   private static class PutMethodAdapter extends MethodAdapter implements Opcodes {
129     private final String JavaDoc invokeMethodSignature;
130
131     public PutMethodAdapter(MethodVisitor mv, String JavaDoc invokeMethodSignature) {
132       super(mv);
133       this.invokeMethodSignature = invokeMethodSignature;
134     }
135     
136     /**
137      * Changing the while (count.get() == capacity) condition to
138      * while (count.get() >= capacity) due to the non-blocking version of put().
139      */

140     public void visitJumpInsn(int opcode, Label label) {
141       if (IF_ICMPEQ == opcode) {
142         opcode = IF_ICMPGE;
143       }
144       super.visitJumpInsn(opcode, label);
145     }
146
147     public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
148       super.visitMethodInsn(opcode, owner, name, desc);
149       if ("insert".equals(name) && "(Ljava/lang/Object;)V".equals(desc)) {
150         addLogicalInvokeMethodCall();
151       }
152     }
153
154     private void addLogicalInvokeMethodCall() {
155       Label notManaged = new Label();
156       addCheckedManagedCode(mv, notManaged);
157       ByteCodeUtil.pushThis(mv);
158       ByteCodeUtil.pushThis(mv);
159       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/LinkedBlockingQueue", "putLock",
160                         "Ljava/util/concurrent/locks/ReentrantLock;");
161
162       mv.visitLdcInsn(invokeMethodSignature);
163
164       mv.visitLdcInsn(new Integer JavaDoc(1));
165       mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
166       mv.visitInsn(DUP);
167       int count = 0;
168       mv.visitLdcInsn(new Integer JavaDoc(count++));
169       mv.visitVarInsn(ALOAD, 1);
170       mv.visitInsn(AASTORE);
171       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "logicalInvokeWithTransaction",
172                          "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V");
173       mv.visitLabel(notManaged);
174     }
175   }
176
177   private static class TakeMethodAdapter extends MethodAdapter implements Opcodes {
178     private boolean visitExtract = false;
179     private final String JavaDoc invokeMethodSignature;
180
181     public TakeMethodAdapter(MethodVisitor mv, String JavaDoc invokeMethodSignature) {
182       super(mv);
183       this.invokeMethodSignature = invokeMethodSignature;
184     }
185
186     public void visitJumpInsn(int opcode, Label label) {
187       if (IFEQ == opcode) {
188         opcode = IFLE;
189       }
190       super.visitJumpInsn(opcode, label);
191     }
192
193     public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
194       super.visitMethodInsn(opcode, owner, name, desc);
195       if ("extract".equals(name) && "()Ljava/lang/Object;".equals(desc)) {
196         visitExtract = true;
197       }
198     }
199
200     public void visitVarInsn(int opcode, int var) {
201       if (ASTORE == opcode && visitExtract) {
202         super.visitVarInsn(opcode, var);
203         addLogicalInvokeMethodCall();
204         visitExtract = false;
205       } else {
206         super.visitVarInsn(opcode, var);
207       }
208     }
209
210     private void addLogicalInvokeMethodCall() {
211       Label notManaged = new Label();
212       addCheckedManagedCode(mv, notManaged);
213       ByteCodeUtil.pushThis(mv);
214       ByteCodeUtil.pushThis(mv);
215       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/LinkedBlockingQueue", "takeLock",
216                         "Ljava/util/concurrent/locks/ReentrantLock;");
217       mv.visitLdcInsn(invokeMethodSignature);
218
219       mv.visitLdcInsn(new Integer JavaDoc(0));
220       mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
221       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "logicalInvokeWithTransaction",
222                          "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V");
223       mv.visitLabel(notManaged);
224     }
225   }
226
227   private static void addCheckedManagedCode(MethodVisitor mv, Label notManaged) {
228     ByteCodeUtil.pushThis(mv);
229     mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "isManaged", "(Ljava/lang/Object;)Z");
230     mv.visitJumpInsn(IFEQ, notManaged);
231   }
232 }
233
Popular Tags