KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.tc.asm.ClassAdapter;
7 import com.tc.asm.ClassVisitor;
8 import com.tc.asm.FieldVisitor;
9 import com.tc.asm.Label;
10 import com.tc.asm.MethodAdapter;
11 import com.tc.asm.MethodVisitor;
12 import com.tc.asm.Opcodes;
13
14 public class JavaUtilConcurrentFutureTaskSyncAdapter implements Opcodes {
15   public static class JavaUtilConcurrentFutureTaskSyncClassAdapter extends ClassAdapter implements Opcodes {
16     private final static String JavaDoc MANAGED_OBJECT_LOCK_FIELD_NAME = "managedLock";
17     private final static String JavaDoc PROXY_RUNNER_FIELD_NAME = "proxyRunner";
18     private final static String JavaDoc MAANGED_INNER_CANCEL_METHOD_NAME = "managedInnerCancel";
19     private final static String JavaDoc MANAGED_INNER_SET_METHOD_NAME = "managedInnerSet";
20     private final static String JavaDoc MANAGED_TRY_RELEASE_SHARED_METHOD = "managedTryReleaseShared";
21     //private final static String MANAGED_RELEASE_SHARED = "managedReleaseShared";
22

23     public JavaUtilConcurrentFutureTaskSyncClassAdapter(ClassVisitor cv) {
24       super(cv);
25     }
26
27     public FieldVisitor visitField(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, Object JavaDoc value) {
28       if ("runner".equals(name) && "Ljava/lang/Thread;".equals(desc)) {
29         access += ACC_TRANSIENT;
30       }
31       return super.visitField(access, name, desc, signature, value);
32     }
33
34     public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
35       MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
36       if ("<init>".equals(name)) {
37         mv = new Constructor(mv);
38       } else if ("innerGet".equals(name) && "()Ljava/lang/Object;".equals(desc)) {
39         rewriteInnerGetMethod(mv);
40       } else if ("innerGet".equals(name) && "(J)Ljava/lang/Object;".equals(desc)) {
41         rewriteTimedInnerGetMethod(mv);
42       } else if ("innerSet".equals(name)) {
43         rewriteInnerSetMethod(mv);
44       } else if ("innerSetException".equals(name)) {
45         rewriteInnerSetExceptionMethod(mv);
46       } else if ("innerCancel".equals(name)) {
47         rewriteInnerCancelMethod(mv);
48       } else if ("innerIsDone".equals(name)) {
49         rewriteInnerIsDoneMethod(mv);
50       } else if ("innerRun".equals(name)) {
51         rewriteInnerRunMethod(mv);
52       } else if ("tryReleaseShared".equals(name)) {
53         rewriteTryReleaseSharedMethod(mv);
54       }
55
56       return mv;
57     }
58
59     public void visitEnd() {
60       addManagedLockField();
61       addProxyRunnerField();
62       addManagedInnerCancelMethod();
63       addManagedTryReleaseSharedMethod();
64       //addManagedReleaseSharedMethod();
65
//addTryManagedReleaseMethod();
66
addManagedInnerSetMethod();
67       super.visitEnd();
68     }
69
70     private void addManagedLockField() {
71       cv.visitField(ACC_PRIVATE + ACC_SYNTHETIC, MANAGED_OBJECT_LOCK_FIELD_NAME, "Ljava/lang/Object;", null, null);
72     }
73
74     private void addProxyRunnerField() {
75       cv.visitField(ACC_PRIVATE + ACC_VOLATILE + ACC_SYNTHETIC, PROXY_RUNNER_FIELD_NAME, "Ljava/lang/Object;", null, null);
76     }
77
78     private void addManagedInnerSetMethod() {
79       MethodVisitor mv = cv.visitMethod(0, MANAGED_INNER_SET_METHOD_NAME, "(Ljava/lang/Object;)V", null, null);
80       mv.visitCode();
81       Label l0 = new Label();
82       mv.visitLabel(l0);
83       mv.visitVarInsn(ALOAD, 0);
84       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
85       mv.visitVarInsn(ISTORE, 2);
86       Label l1 = new Label();
87       mv.visitLabel(l1);
88       mv.visitVarInsn(ALOAD, 0);
89       mv.visitVarInsn(ILOAD, 2);
90       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "ranOrCancelled", "(I)Z");
91       Label l2 = new Label();
92       mv.visitJumpInsn(IFEQ, l2);
93       mv.visitInsn(RETURN);
94       mv.visitLabel(l2);
95       mv.visitVarInsn(ALOAD, 0);
96       mv.visitInsn(ICONST_2);
97       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "setState", "(I)V");
98       Label l3 = new Label();
99       mv.visitLabel(l3);
100       mv.visitVarInsn(ALOAD, 0);
101       mv.visitVarInsn(ALOAD, 1);
102       mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "result", "Ljava/lang/Object;");
103       Label l4 = new Label();
104       mv.visitLabel(l4);
105       mv.visitVarInsn(ALOAD, 0);
106       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "managedTryReleaseShared", "()Z");
107       mv.visitInsn(POP);
108       Label l5 = new Label();
109       mv.visitLabel(l5);
110       mv.visitVarInsn(ALOAD, 0);
111       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
112       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask", "done", "()V");
113       Label l6 = new Label();
114       mv.visitLabel(l6);
115       mv.visitInsn(RETURN);
116       Label l7 = new Label();
117       mv.visitLabel(l7);
118       mv.visitMaxs(0, 0);
119       mv.visitEnd();
120     }
121
122     private void rewriteTryReleaseSharedMethod(MethodVisitor mv) {
123       mv.visitCode();
124       Label l0 = new Label();
125       mv.visitLabel(l0);
126       mv.visitVarInsn(ALOAD, 0);
127       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "managedTryReleaseShared", "()Z");
128       mv.visitInsn(POP);
129       Label l1 = new Label();
130       mv.visitLabel(l1);
131       mv.visitInsn(ICONST_1);
132       mv.visitInsn(IRETURN);
133       Label l2 = new Label();
134       mv.visitLabel(l2);
135       mv.visitMaxs(0, 0);
136       mv.visitEnd();
137     }
138
139     private void rewriteInnerRunMethod(MethodVisitor mv) {
140       mv.visitCode();
141       Label l0 = new Label();
142       Label l1 = new Label();
143       Label l2 = new Label();
144       mv.visitTryCatchBlock(l0, l1, l2, null);
145       Label l3 = new Label();
146       mv.visitTryCatchBlock(l2, l3, l2, null);
147       Label l4 = new Label();
148       Label l5 = new Label();
149       Label l6 = new Label();
150       mv.visitTryCatchBlock(l4, l5, l6, null);
151       Label l7 = new Label();
152       mv.visitTryCatchBlock(l6, l7, l6, null);
153       Label l8 = new Label();
154       Label l9 = new Label();
155       Label l10 = new Label();
156       mv.visitTryCatchBlock(l8, l9, l10, null);
157       Label l11 = new Label();
158       mv.visitTryCatchBlock(l10, l11, l10, null);
159       Label l12 = new Label();
160       Label l13 = new Label();
161       mv.visitTryCatchBlock(l12, l13, l13, "java/lang/Throwable");
162       Label l14 = new Label();
163       Label l15 = new Label();
164       mv.visitTryCatchBlock(l14, l15, l15, "java/lang/Throwable");
165       Label l16 = new Label();
166       mv.visitLabel(l16);
167       mv.visitVarInsn(ALOAD, 0);
168       mv.visitInsn(ICONST_0);
169       mv.visitInsn(ICONST_1);
170       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "compareAndSetState", "(II)Z");
171       Label l17 = new Label();
172       mv.visitJumpInsn(IFNE, l17);
173       mv.visitInsn(RETURN);
174       mv.visitLabel(l17);
175       mv.visitVarInsn(ALOAD, 0);
176       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
177       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "isManaged", "(Ljava/lang/Object;)Z");
178       mv.visitJumpInsn(IFEQ, l14);
179       mv.visitLabel(l12);
180       mv.visitVarInsn(ALOAD, 0);
181       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;");
182       mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "runner", "Ljava/lang/Thread;");
183       Label l18 = new Label();
184       mv.visitLabel(l18);
185       mv.visitInsn(ICONST_0);
186       mv.visitVarInsn(ISTORE, 1);
187       Label l19 = new Label();
188       mv.visitLabel(l19);
189       mv.visitVarInsn(ALOAD, 0);
190       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
191       mv.visitInsn(DUP);
192       mv.visitVarInsn(ASTORE, 2);
193       mv.visitInsn(MONITORENTER);
194       mv.visitLabel(l0);
195       mv.visitVarInsn(ALOAD, 0);
196       mv.visitVarInsn(ALOAD, 0);
197       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "runner", "Ljava/lang/Thread;");
198       mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Thread", "toString", "()Ljava/lang/String;");
199       mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "proxyRunner", "Ljava/lang/Object;");
200       Label l20 = new Label();
201       mv.visitLabel(l20);
202       /*mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
203       mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
204       mv.visitInsn(DUP);
205       mv.visitLdcInsn("InnerRun state1: ");
206       mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V");
207       mv.visitVarInsn(ALOAD, 0);
208       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
209       mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;");
210       mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
211       mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
212
213       Label l21 = new Label();
214       mv.visitLabel(l21);
215       mv.visitLdcInsn(new Long(10L));
216       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "sleep", "(J)V");
217       Label l22 = new Label();
218       mv.visitLabel(l22);
219       mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
220       mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
221       mv.visitInsn(DUP);
222       mv.visitLdcInsn("InnerRun state2: ");
223       mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V");
224       mv.visitVarInsn(ALOAD, 0);
225       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
226       mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;");
227       mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
228       mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
229       */

230       Label l23 = new Label();
231       mv.visitLabel(l23);
232       mv.visitVarInsn(ALOAD, 0);
233       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
234       mv.visitInsn(ICONST_1);
235       Label l24 = new Label();
236       mv.visitJumpInsn(IF_ICMPNE, l24);
237       mv.visitInsn(ICONST_1);
238       Label l25 = new Label();
239       mv.visitJumpInsn(GOTO, l25);
240       mv.visitLabel(l24);
241       mv.visitInsn(ICONST_0);
242       mv.visitLabel(l25);
243       mv.visitVarInsn(ISTORE, 1);
244       Label l26 = new Label();
245       mv.visitLabel(l26);
246       mv.visitVarInsn(ALOAD, 2);
247       mv.visitInsn(MONITOREXIT);
248       mv.visitLabel(l1);
249       Label l27 = new Label();
250       mv.visitJumpInsn(GOTO, l27);
251       mv.visitLabel(l2);
252       mv.visitVarInsn(ALOAD, 2);
253       mv.visitInsn(MONITOREXIT);
254       mv.visitLabel(l3);
255       mv.visitInsn(ATHROW);
256       mv.visitLabel(l27);
257       mv.visitVarInsn(ILOAD, 1);
258       Label l28 = new Label();
259       mv.visitJumpInsn(IFEQ, l28);
260       Label l29 = new Label();
261       mv.visitLabel(l29);
262       mv.visitVarInsn(ALOAD, 0);
263       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "callable", "Ljava/util/concurrent/Callable;");
264       mv.visitMethodInsn(INVOKEINTERFACE, "java/util/concurrent/Callable", "call", "()Ljava/lang/Object;");
265       mv.visitVarInsn(ASTORE, 2);
266       Label l30 = new Label();
267       mv.visitLabel(l30);
268       mv.visitVarInsn(ALOAD, 0);
269       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
270       mv.visitInsn(DUP);
271       mv.visitVarInsn(ASTORE, 3);
272       mv.visitInsn(MONITORENTER);
273       mv.visitLabel(l4);
274       mv.visitVarInsn(ALOAD, 0);
275       mv.visitVarInsn(ALOAD, 2);
276       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "managedInnerSet", "(Ljava/lang/Object;)V");
277       Label l31 = new Label();
278       mv.visitLabel(l31);
279       mv.visitVarInsn(ALOAD, 3);
280       mv.visitInsn(MONITOREXIT);
281       mv.visitLabel(l5);
282       Label l32 = new Label();
283       mv.visitJumpInsn(GOTO, l32);
284       mv.visitLabel(l6);
285       mv.visitVarInsn(ALOAD, 3);
286       mv.visitInsn(MONITOREXIT);
287       mv.visitLabel(l7);
288       mv.visitInsn(ATHROW);
289       mv.visitJumpInsn(GOTO, l32);
290       mv.visitLabel(l28);
291       mv.visitVarInsn(ALOAD, 0);
292       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
293       mv.visitInsn(DUP);
294       mv.visitVarInsn(ASTORE, 2);
295       mv.visitInsn(MONITORENTER);
296       mv.visitLabel(l8);
297       mv.visitVarInsn(ALOAD, 0);
298       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "managedTryReleaseShared", "()Z");
299       mv.visitInsn(POP);
300       Label l33 = new Label();
301       mv.visitLabel(l33);
302       mv.visitVarInsn(ALOAD, 2);
303       mv.visitInsn(MONITOREXIT);
304       mv.visitLabel(l9);
305       mv.visitJumpInsn(GOTO, l32);
306       mv.visitLabel(l10);
307       mv.visitVarInsn(ALOAD, 2);
308       mv.visitInsn(MONITOREXIT);
309       mv.visitLabel(l11);
310       mv.visitInsn(ATHROW);
311       mv.visitJumpInsn(GOTO, l32);
312       mv.visitLabel(l13);
313       mv.visitVarInsn(ASTORE, 1);
314       Label l34 = new Label();
315       mv.visitLabel(l34);
316       /*mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
317       mv.visitLdcInsn("Catching Throwable in innerRun.");
318       mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
319       */

320       Label l35 = new Label();
321       mv.visitLabel(l35);
322       mv.visitVarInsn(ALOAD, 0);
323       mv.visitVarInsn(ALOAD, 1);
324       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "innerSetException", "(Ljava/lang/Throwable;)V");
325       Label l36 = new Label();
326       mv.visitLabel(l36);
327       mv.visitJumpInsn(GOTO, l32);
328       mv.visitLabel(l14);
329       mv.visitVarInsn(ALOAD, 0);
330       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;");
331       mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "runner", "Ljava/lang/Thread;");
332       Label l37 = new Label();
333       mv.visitLabel(l37);
334       mv.visitVarInsn(ALOAD, 0);
335       mv.visitVarInsn(ALOAD, 0);
336       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "callable", "Ljava/util/concurrent/Callable;");
337       mv.visitMethodInsn(INVOKEINTERFACE, "java/util/concurrent/Callable", "call", "()Ljava/lang/Object;");
338       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "innerSet", "(Ljava/lang/Object;)V");
339       mv.visitJumpInsn(GOTO, l32);
340       mv.visitLabel(l15);
341       mv.visitVarInsn(ASTORE, 1);
342       Label l38 = new Label();
343       mv.visitLabel(l38);
344       mv.visitVarInsn(ALOAD, 0);
345       mv.visitVarInsn(ALOAD, 1);
346       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "innerSetException", "(Ljava/lang/Throwable;)V");
347       mv.visitLabel(l32);
348       mv.visitInsn(RETURN);
349       Label l39 = new Label();
350       mv.visitLabel(l39);
351       mv.visitMaxs(0, 0);
352       mv.visitEnd();
353     }
354
355     private void rewriteInnerIsDoneMethod(MethodVisitor mv) {
356       mv.visitCode();
357       Label l0 = new Label();
358       Label l1 = new Label();
359       Label l2 = new Label();
360       mv.visitTryCatchBlock(l0, l1, l2, null);
361       Label l3 = new Label();
362       mv.visitTryCatchBlock(l2, l3, l2, null);
363       Label l4 = new Label();
364       mv.visitLabel(l4);
365       mv.visitVarInsn(ALOAD, 0);
366       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
367       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "isManaged", "(Ljava/lang/Object;)Z");
368       Label l5 = new Label();
369       mv.visitJumpInsn(IFEQ, l5);
370       Label l6 = new Label();
371       mv.visitLabel(l6);
372       mv.visitVarInsn(ALOAD, 0);
373       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
374       mv.visitInsn(DUP);
375       mv.visitVarInsn(ASTORE, 1);
376       mv.visitInsn(MONITORENTER);
377       mv.visitLabel(l0);
378       mv.visitVarInsn(ALOAD, 0);
379       mv.visitVarInsn(ALOAD, 0);
380       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
381       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "ranOrCancelled", "(I)Z");
382       Label l7 = new Label();
383       mv.visitJumpInsn(IFEQ, l7);
384       mv.visitVarInsn(ALOAD, 0);
385       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "proxyRunner", "Ljava/lang/Object;");
386       mv.visitJumpInsn(IFNONNULL, l7);
387       mv.visitInsn(ICONST_1);
388       Label l8 = new Label();
389       mv.visitJumpInsn(GOTO, l8);
390       mv.visitLabel(l7);
391       mv.visitInsn(ICONST_0);
392       mv.visitLabel(l8);
393       mv.visitVarInsn(ALOAD, 1);
394       mv.visitInsn(MONITOREXIT);
395       mv.visitLabel(l1);
396       mv.visitInsn(IRETURN);
397       mv.visitLabel(l2);
398       mv.visitVarInsn(ALOAD, 1);
399       mv.visitInsn(MONITOREXIT);
400       mv.visitLabel(l3);
401       mv.visitInsn(ATHROW);
402       mv.visitLabel(l5);
403       mv.visitVarInsn(ALOAD, 0);
404       mv.visitVarInsn(ALOAD, 0);
405       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
406       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "ranOrCancelled", "(I)Z");
407       Label l9 = new Label();
408       mv.visitJumpInsn(IFEQ, l9);
409       mv.visitVarInsn(ALOAD, 0);
410       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "runner", "Ljava/lang/Thread;");
411       mv.visitJumpInsn(IFNONNULL, l9);
412       mv.visitInsn(ICONST_1);
413       mv.visitInsn(IRETURN);
414       mv.visitLabel(l9);
415       mv.visitInsn(ICONST_0);
416       mv.visitInsn(IRETURN);
417       Label l10 = new Label();
418       mv.visitLabel(l10);
419       mv.visitMaxs(0, 0);
420       mv.visitEnd();
421     }
422
423     private void rewriteInnerSetExceptionMethod(MethodVisitor mv) {
424       mv.visitCode();
425       Label l0 = new Label();
426       Label l1 = new Label();
427       Label l2 = new Label();
428       mv.visitTryCatchBlock(l0, l1, l2, null);
429       Label l3 = new Label();
430       Label l4 = new Label();
431       mv.visitTryCatchBlock(l3, l4, l2, null);
432       Label l5 = new Label();
433       mv.visitTryCatchBlock(l2, l5, l2, null);
434       Label l6 = new Label();
435       Label l7 = new Label();
436       Label l8 = new Label();
437       mv.visitTryCatchBlock(l6, l7, l8, null);
438       Label l9 = new Label();
439       mv.visitTryCatchBlock(l8, l9, l8, null);
440       Label l10 = new Label();
441       mv.visitLabel(l10);
442       mv.visitVarInsn(ALOAD, 0);
443       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
444       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "isManaged", "(Ljava/lang/Object;)Z");
445       Label l11 = new Label();
446       mv.visitJumpInsn(IFEQ, l11);
447       Label l12 = new Label();
448       mv.visitLabel(l12);
449       mv.visitVarInsn(ALOAD, 0);
450       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
451       mv.visitInsn(DUP);
452       mv.visitVarInsn(ASTORE, 2);
453       mv.visitInsn(MONITORENTER);
454       mv.visitLabel(l0);
455       mv.visitVarInsn(ALOAD, 0);
456       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
457       mv.visitVarInsn(ISTORE, 3);
458       Label l13 = new Label();
459       mv.visitLabel(l13);
460       mv.visitVarInsn(ALOAD, 0);
461       mv.visitVarInsn(ILOAD, 3);
462       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "ranOrCancelled", "(I)Z");
463       mv.visitJumpInsn(IFEQ, l3);
464       mv.visitVarInsn(ALOAD, 2);
465       mv.visitInsn(MONITOREXIT);
466       mv.visitLabel(l1);
467       mv.visitInsn(RETURN);
468       mv.visitLabel(l3);
469       mv.visitVarInsn(ALOAD, 0);
470       mv.visitInsn(ICONST_2);
471       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "setState", "(I)V");
472       Label l14 = new Label();
473       mv.visitLabel(l14);
474       mv.visitVarInsn(ALOAD, 0);
475       mv.visitVarInsn(ALOAD, 1);
476       mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "exception", "Ljava/lang/Throwable;");
477       Label l15 = new Label();
478       mv.visitLabel(l15);
479       mv.visitVarInsn(ALOAD, 0);
480       mv.visitInsn(ACONST_NULL);
481       mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "result", "Ljava/lang/Object;");
482       Label l16 = new Label();
483       mv.visitLabel(l16);
484       mv.visitVarInsn(ALOAD, 0);
485       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "managedTryReleaseShared", "()Z");
486       mv.visitInsn(POP);
487       Label l17 = new Label();
488       mv.visitLabel(l17);
489       mv.visitVarInsn(ALOAD, 0);
490       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
491       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask", "done", "()V");
492       Label l18 = new Label();
493       mv.visitLabel(l18);
494       mv.visitVarInsn(ALOAD, 2);
495       mv.visitInsn(MONITOREXIT);
496       mv.visitLabel(l4);
497       Label l19 = new Label();
498       mv.visitJumpInsn(GOTO, l19);
499       mv.visitLabel(l2);
500       mv.visitVarInsn(ALOAD, 2);
501       mv.visitInsn(MONITOREXIT);
502       mv.visitLabel(l5);
503       mv.visitInsn(ATHROW);
504       Label l20 = new Label();
505       mv.visitLabel(l20);
506       mv.visitJumpInsn(GOTO, l19);
507       mv.visitLabel(l11);
508       mv.visitVarInsn(ALOAD, 0);
509       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
510       mv.visitVarInsn(ISTORE, 2);
511       Label l21 = new Label();
512       mv.visitLabel(l21);
513       mv.visitVarInsn(ALOAD, 0);
514       mv.visitVarInsn(ILOAD, 2);
515       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "ranOrCancelled", "(I)Z");
516       Label l22 = new Label();
517       mv.visitJumpInsn(IFEQ, l22);
518       mv.visitInsn(RETURN);
519       mv.visitLabel(l22);
520       mv.visitVarInsn(ALOAD, 0);
521       mv.visitVarInsn(ILOAD, 2);
522       mv.visitInsn(ICONST_2);
523       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "compareAndSetState", "(II)Z");
524       mv.visitJumpInsn(IFEQ, l11);
525       Label l23 = new Label();
526       mv.visitJumpInsn(GOTO, l23);
527       Label l24 = new Label();
528       mv.visitLabel(l24);
529       mv.visitJumpInsn(GOTO, l11);
530       mv.visitLabel(l23);
531       mv.visitVarInsn(ALOAD, 0);
532       mv.visitVarInsn(ALOAD, 1);
533       mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "exception", "Ljava/lang/Throwable;");
534       Label l25 = new Label();
535       mv.visitLabel(l25);
536       mv.visitVarInsn(ALOAD, 0);
537       mv.visitInsn(ACONST_NULL);
538       mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "result", "Ljava/lang/Object;");
539       Label l26 = new Label();
540       mv.visitLabel(l26);
541       mv.visitVarInsn(ALOAD, 0);
542       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
543       mv.visitInsn(DUP);
544       mv.visitVarInsn(ASTORE, 2);
545       mv.visitInsn(MONITORENTER);
546       mv.visitLabel(l6);
547       mv.visitVarInsn(ALOAD, 0);
548       mv.visitInsn(ICONST_0);
549       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "releaseShared", "(I)Z");
550       mv.visitInsn(POP);
551       Label l27 = new Label();
552       mv.visitLabel(l27);
553       mv.visitVarInsn(ALOAD, 2);
554       mv.visitInsn(MONITOREXIT);
555       mv.visitLabel(l7);
556       Label l28 = new Label();
557       mv.visitJumpInsn(GOTO, l28);
558       mv.visitLabel(l8);
559       mv.visitVarInsn(ALOAD, 2);
560       mv.visitInsn(MONITOREXIT);
561       mv.visitLabel(l9);
562       mv.visitInsn(ATHROW);
563       mv.visitLabel(l28);
564       mv.visitVarInsn(ALOAD, 0);
565       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
566       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask", "done", "()V");
567       mv.visitLabel(l19);
568       mv.visitInsn(RETURN);
569       Label l29 = new Label();
570       mv.visitLabel(l29);
571       mv.visitMaxs(0, 0);
572       mv.visitEnd();
573     }
574
575     private void rewriteInnerSetMethod(MethodVisitor mv) {
576       mv.visitCode();
577       Label l0 = new Label();
578       Label l1 = new Label();
579       Label l2 = new Label();
580       mv.visitTryCatchBlock(l0, l1, l2, null);
581       Label l3 = new Label();
582       mv.visitTryCatchBlock(l2, l3, l2, null);
583       Label l4 = new Label();
584       Label l5 = new Label();
585       Label l6 = new Label();
586       mv.visitTryCatchBlock(l4, l5, l6, null);
587       Label l7 = new Label();
588       mv.visitTryCatchBlock(l6, l7, l6, null);
589       Label l8 = new Label();
590       mv.visitLabel(l8);
591       mv.visitVarInsn(ALOAD, 0);
592       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
593       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "isManaged", "(Ljava/lang/Object;)Z");
594       Label l9 = new Label();
595       mv.visitJumpInsn(IFEQ, l9);
596       Label l10 = new Label();
597       mv.visitLabel(l10);
598       mv.visitVarInsn(ALOAD, 0);
599       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
600       mv.visitInsn(DUP);
601       mv.visitVarInsn(ASTORE, 2);
602       mv.visitInsn(MONITORENTER);
603       mv.visitLabel(l0);
604       mv.visitVarInsn(ALOAD, 0);
605       mv.visitVarInsn(ALOAD, 1);
606       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "managedInnerSet", "(Ljava/lang/Object;)V");
607       Label l11 = new Label();
608       mv.visitLabel(l11);
609       mv.visitVarInsn(ALOAD, 2);
610       mv.visitInsn(MONITOREXIT);
611       mv.visitLabel(l1);
612       Label l12 = new Label();
613       mv.visitJumpInsn(GOTO, l12);
614       mv.visitLabel(l2);
615       mv.visitVarInsn(ALOAD, 2);
616       mv.visitInsn(MONITOREXIT);
617       mv.visitLabel(l3);
618       mv.visitInsn(ATHROW);
619       mv.visitJumpInsn(GOTO, l12);
620       mv.visitLabel(l9);
621       mv.visitVarInsn(ALOAD, 0);
622       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
623       mv.visitVarInsn(ISTORE, 2);
624       Label l13 = new Label();
625       mv.visitLabel(l13);
626       mv.visitVarInsn(ALOAD, 0);
627       mv.visitVarInsn(ILOAD, 2);
628       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "ranOrCancelled", "(I)Z");
629       Label l14 = new Label();
630       mv.visitJumpInsn(IFEQ, l14);
631       mv.visitInsn(RETURN);
632       mv.visitLabel(l14);
633       mv.visitVarInsn(ALOAD, 0);
634       mv.visitVarInsn(ILOAD, 2);
635       mv.visitInsn(ICONST_2);
636       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "compareAndSetState", "(II)Z");
637       mv.visitJumpInsn(IFEQ, l9);
638       Label l15 = new Label();
639       mv.visitJumpInsn(GOTO, l15);
640       Label l16 = new Label();
641       mv.visitLabel(l16);
642       mv.visitJumpInsn(GOTO, l9);
643       mv.visitLabel(l15);
644       mv.visitVarInsn(ALOAD, 0);
645       mv.visitVarInsn(ALOAD, 1);
646       mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "result", "Ljava/lang/Object;");
647       Label l17 = new Label();
648       mv.visitLabel(l17);
649       mv.visitVarInsn(ALOAD, 0);
650       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
651       mv.visitInsn(DUP);
652       mv.visitVarInsn(ASTORE, 2);
653       mv.visitInsn(MONITORENTER);
654       mv.visitLabel(l4);
655       mv.visitVarInsn(ALOAD, 0);
656       mv.visitInsn(ICONST_0);
657       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "releaseShared", "(I)Z");
658       mv.visitInsn(POP);
659       Label l18 = new Label();
660       mv.visitLabel(l18);
661       mv.visitVarInsn(ALOAD, 2);
662       mv.visitInsn(MONITOREXIT);
663       mv.visitLabel(l5);
664       Label l19 = new Label();
665       mv.visitJumpInsn(GOTO, l19);
666       mv.visitLabel(l6);
667       mv.visitVarInsn(ALOAD, 2);
668       mv.visitInsn(MONITOREXIT);
669       mv.visitLabel(l7);
670       mv.visitInsn(ATHROW);
671       mv.visitLabel(l19);
672       mv.visitVarInsn(ALOAD, 0);
673       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
674       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask", "done", "()V");
675       mv.visitLabel(l12);
676       mv.visitInsn(RETURN);
677       Label l20 = new Label();
678       mv.visitLabel(l20);
679       mv.visitMaxs(0, 0);
680       mv.visitEnd();
681     }
682
683     private void rewriteTimedInnerGetMethod(MethodVisitor mv) {
684       mv.visitCode();
685       Label l0 = new Label();
686       Label l1 = new Label();
687       Label l2 = new Label();
688       mv.visitTryCatchBlock(l0, l1, l2, null);
689       Label l3 = new Label();
690       mv.visitTryCatchBlock(l2, l3, l2, null);
691       Label l4 = new Label();
692       mv.visitLabel(l4);
693       mv.visitVarInsn(ALOAD, 0);
694       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
695       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "isManaged", "(Ljava/lang/Object;)Z");
696       Label l5 = new Label();
697       mv.visitJumpInsn(IFEQ, l5);
698       Label l6 = new Label();
699       mv.visitLabel(l6);
700       mv.visitVarInsn(ALOAD, 0);
701       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
702       mv.visitInsn(DUP);
703       mv.visitVarInsn(ASTORE, 3);
704       mv.visitInsn(MONITORENTER);
705       mv.visitLabel(l0);
706       mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J");
707       mv.visitVarInsn(LSTORE, 4);
708       Label l7 = new Label();
709       mv.visitLabel(l7);
710       Label l8 = new Label();
711       mv.visitJumpInsn(GOTO, l8);
712       Label l9 = new Label();
713       mv.visitLabel(l9);
714       mv.visitFieldInsn(GETSTATIC, "java/util/concurrent/TimeUnit", "NANOSECONDS", "Ljava/util/concurrent/TimeUnit;");
715       mv.visitVarInsn(ALOAD, 0);
716       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
717       mv.visitVarInsn(LLOAD, 1);
718       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/TimeUnit", "timedWait", "(Ljava/lang/Object;J)V");
719       Label l10 = new Label();
720       mv.visitLabel(l10);
721       mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J");
722       mv.visitVarInsn(LSTORE, 6);
723       Label l11 = new Label();
724       mv.visitLabel(l11);
725       mv.visitVarInsn(LLOAD, 1);
726       mv.visitVarInsn(LLOAD, 6);
727       mv.visitVarInsn(LLOAD, 4);
728       mv.visitInsn(LSUB);
729       mv.visitInsn(LSUB);
730       mv.visitVarInsn(LSTORE, 1);
731       mv.visitLabel(l8);
732       mv.visitVarInsn(ALOAD, 0);
733       mv.visitInsn(ICONST_0);
734       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "tryAcquireShared", "(I)I");
735       Label l12 = new Label();
736       mv.visitJumpInsn(IFGE, l12);
737       mv.visitVarInsn(LLOAD, 1);
738       mv.visitInsn(LCONST_0);
739       mv.visitInsn(LCMP);
740       mv.visitJumpInsn(IFGT, l9);
741       mv.visitLabel(l12);
742       mv.visitVarInsn(ALOAD, 0);
743       mv.visitInsn(ICONST_0);
744       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "tryAcquireShared", "(I)I");
745       Label l13 = new Label();
746       mv.visitJumpInsn(IFGE, l13);
747       mv.visitTypeInsn(NEW, "java/util/concurrent/TimeoutException");
748       mv.visitInsn(DUP);
749       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/TimeoutException", "<init>", "()V");
750       mv.visitInsn(ATHROW);
751       mv.visitLabel(l13);
752       mv.visitVarInsn(ALOAD, 3);
753       mv.visitInsn(MONITOREXIT);
754       mv.visitLabel(l1);
755       Label l14 = new Label();
756       mv.visitJumpInsn(GOTO, l14);
757       mv.visitLabel(l2);
758       mv.visitVarInsn(ALOAD, 3);
759       mv.visitInsn(MONITOREXIT);
760       mv.visitLabel(l3);
761       mv.visitInsn(ATHROW);
762       Label l15 = new Label();
763       mv.visitLabel(l15);
764       mv.visitJumpInsn(GOTO, l14);
765       mv.visitLabel(l5);
766       mv.visitVarInsn(ALOAD, 0);
767       mv.visitInsn(ICONST_0);
768       mv.visitVarInsn(LLOAD, 1);
769       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "tryAcquireSharedNanos", "(IJ)Z");
770       mv.visitJumpInsn(IFNE, l14);
771       mv.visitTypeInsn(NEW, "java/util/concurrent/TimeoutException");
772       mv.visitInsn(DUP);
773       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/TimeoutException", "<init>", "()V");
774       mv.visitInsn(ATHROW);
775       mv.visitLabel(l14);
776       mv.visitVarInsn(ALOAD, 0);
777       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
778       mv.visitInsn(ICONST_4);
779       Label l16 = new Label();
780       mv.visitJumpInsn(IF_ICMPNE, l16);
781       mv.visitTypeInsn(NEW, "java/util/concurrent/CancellationException");
782       mv.visitInsn(DUP);
783       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/CancellationException", "<init>", "()V");
784       mv.visitInsn(ATHROW);
785       mv.visitLabel(l16);
786       mv.visitVarInsn(ALOAD, 0);
787       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "exception", "Ljava/lang/Throwable;");
788       Label l17 = new Label();
789       mv.visitJumpInsn(IFNULL, l17);
790       mv.visitTypeInsn(NEW, "java/util/concurrent/ExecutionException");
791       mv.visitInsn(DUP);
792       mv.visitVarInsn(ALOAD, 0);
793       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "exception", "Ljava/lang/Throwable;");
794       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/ExecutionException", "<init>", "(Ljava/lang/Throwable;)V");
795       mv.visitInsn(ATHROW);
796       mv.visitLabel(l17);
797       mv.visitVarInsn(ALOAD, 0);
798       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "result", "Ljava/lang/Object;");
799       mv.visitInsn(ARETURN);
800       Label l18 = new Label();
801       mv.visitLabel(l18);
802       mv.visitMaxs(0, 0);
803       mv.visitEnd();
804     }
805
806     private void rewriteInnerGetMethod(MethodVisitor mv) {
807       mv.visitCode();
808       Label l0 = new Label();
809       Label l1 = new Label();
810       Label l2 = new Label();
811       mv.visitTryCatchBlock(l0, l1, l2, null);
812       Label l3 = new Label();
813       mv.visitTryCatchBlock(l2, l3, l2, null);
814       Label l4 = new Label();
815       mv.visitLabel(l4);
816       mv.visitVarInsn(ALOAD, 0);
817       mv
818           .visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0",
819                           "Ljava/util/concurrent/FutureTask;");
820       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/ManagerUtil", "isManaged", "(Ljava/lang/Object;)Z");
821       Label l5 = new Label();
822       mv.visitJumpInsn(IFEQ, l5);
823       Label l6 = new Label();
824       mv.visitLabel(l6);
825       mv.visitVarInsn(ALOAD, 0);
826       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", MANAGED_OBJECT_LOCK_FIELD_NAME,
827                         "Ljava/lang/Object;");
828       mv.visitInsn(DUP);
829       mv.visitVarInsn(ASTORE, 1);
830       mv.visitInsn(MONITORENTER);
831       mv.visitLabel(l0);
832       Label l7 = new Label();
833       mv.visitJumpInsn(GOTO, l7);
834       Label l8 = new Label();
835       mv.visitLabel(l8);
836       mv.visitVarInsn(ALOAD, 0);
837       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", MANAGED_OBJECT_LOCK_FIELD_NAME,
838                         "Ljava/lang/Object;");
839       mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "wait", "()V");
840       mv.visitLabel(l7);
841       mv.visitVarInsn(ALOAD, 0);
842       mv.visitInsn(ICONST_0);
843       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "tryAcquireShared", "(I)I");
844       mv.visitJumpInsn(IFLT, l8);
845       Label l9 = new Label();
846       mv.visitLabel(l9);
847       mv.visitVarInsn(ALOAD, 1);
848       mv.visitInsn(MONITOREXIT);
849       mv.visitLabel(l1);
850       Label l10 = new Label();
851       mv.visitJumpInsn(GOTO, l10);
852       mv.visitLabel(l2);
853       mv.visitVarInsn(ALOAD, 1);
854       mv.visitInsn(MONITOREXIT);
855       mv.visitLabel(l3);
856       mv.visitInsn(ATHROW);
857       mv.visitJumpInsn(GOTO, l10);
858       mv.visitLabel(l5);
859       mv.visitVarInsn(ALOAD, 0);
860       mv.visitInsn(ICONST_0);
861       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "acquireSharedInterruptibly", "(I)V");
862       mv.visitLabel(l10);
863       mv.visitVarInsn(ALOAD, 0);
864       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
865       mv.visitInsn(ICONST_4);
866       Label l11 = new Label();
867       mv.visitJumpInsn(IF_ICMPNE, l11);
868       mv.visitTypeInsn(NEW, "java/util/concurrent/CancellationException");
869       mv.visitInsn(DUP);
870       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/CancellationException", "<init>", "()V");
871       mv.visitInsn(ATHROW);
872       mv.visitLabel(l11);
873       mv.visitVarInsn(ALOAD, 0);
874       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "exception", "Ljava/lang/Throwable;");
875       Label l12 = new Label();
876       mv.visitJumpInsn(IFNULL, l12);
877       mv.visitTypeInsn(NEW, "java/util/concurrent/ExecutionException");
878       mv.visitInsn(DUP);
879       mv.visitVarInsn(ALOAD, 0);
880       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "exception", "Ljava/lang/Throwable;");
881       mv
882           .visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/ExecutionException", "<init>",
883                            "(Ljava/lang/Throwable;)V");
884       mv.visitInsn(ATHROW);
885       mv.visitLabel(l12);
886       mv.visitVarInsn(ALOAD, 0);
887       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "result", "Ljava/lang/Object;");
888       mv.visitInsn(ARETURN);
889       Label l13 = new Label();
890       mv.visitLabel(l13);
891       mv.visitMaxs(0, 0);
892       mv.visitEnd();
893     }
894
895     private void rewriteInnerCancelMethod(MethodVisitor mv) {
896       mv.visitCode();
897       Label l0 = new Label();
898       Label l1 = new Label();
899       Label l2 = new Label();
900       mv.visitTryCatchBlock(l0, l1, l2, null);
901       Label l3 = new Label();
902       mv.visitTryCatchBlock(l2, l3, l2, null);
903       Label l4 = new Label();
904       Label l5 = new Label();
905       Label l6 = new Label();
906       mv.visitTryCatchBlock(l4, l5, l6, null);
907       Label l7 = new Label();
908       mv.visitTryCatchBlock(l6, l7, l6, null);
909       Label l8 = new Label();
910       mv.visitLabel(l8);
911       mv.visitInsn(ICONST_M1);
912       mv.visitVarInsn(ISTORE, 2);
913       Label l9 = new Label();
914       mv.visitLabel(l9);
915       mv.visitVarInsn(ALOAD, 0);
916       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
917       mv.visitInsn(DUP);
918       mv.visitVarInsn(ASTORE, 3);
919       mv.visitInsn(MONITORENTER);
920       mv.visitLabel(l0);
921       mv.visitVarInsn(ALOAD, 0);
922       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "getState", "()I");
923       mv.visitVarInsn(ISTORE, 2);
924       Label l10 = new Label();
925       mv.visitLabel(l10);
926       mv.visitVarInsn(ALOAD, 3);
927       mv.visitInsn(MONITOREXIT);
928       mv.visitLabel(l1);
929       Label l11 = new Label();
930       mv.visitJumpInsn(GOTO, l11);
931       mv.visitLabel(l2);
932       mv.visitVarInsn(ALOAD, 3);
933       mv.visitInsn(MONITOREXIT);
934       mv.visitLabel(l3);
935       mv.visitInsn(ATHROW);
936       mv.visitLabel(l11);
937       mv.visitVarInsn(ALOAD, 0);
938       mv.visitVarInsn(ILOAD, 2);
939       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "ranOrCancelled", "(I)Z");
940       Label l12 = new Label();
941       mv.visitJumpInsn(IFEQ, l12);
942       mv.visitInsn(ICONST_0);
943       mv.visitInsn(IRETURN);
944       mv.visitLabel(l12);
945       mv.visitVarInsn(ALOAD, 0);
946       mv.visitVarInsn(ILOAD, 2);
947       mv.visitInsn(ICONST_4);
948       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "compareAndSetState", "(II)Z");
949       mv.visitJumpInsn(IFEQ, l8);
950       Label l13 = new Label();
951       mv.visitJumpInsn(GOTO, l13);
952       Label l14 = new Label();
953       mv.visitLabel(l14);
954       mv.visitJumpInsn(GOTO, l8);
955       mv.visitLabel(l13);
956       mv.visitVarInsn(ALOAD, 0);
957       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "managedLock", "Ljava/lang/Object;");
958       mv.visitInsn(DUP);
959       mv.visitVarInsn(ASTORE, 2);
960       mv.visitInsn(MONITORENTER);
961       mv.visitLabel(l4);
962       mv.visitVarInsn(ILOAD, 1);
963       Label l15 = new Label();
964       mv.visitJumpInsn(IFEQ, l15);
965       Label l16 = new Label();
966       mv.visitLabel(l16);
967       mv.visitVarInsn(ALOAD, 0);
968       mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/FutureTask$Sync", "managedInnerCancel", "()V");
969       mv.visitLabel(l15);
970       mv.visitVarInsn(ALOAD, 0);
971       mv.visitInsn(ICONST_0);
972       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask$Sync", "releaseShared", "(I)Z");
973       mv.visitInsn(POP);
974       Label l17 = new Label();
975       mv.visitLabel(l17);
976       mv.visitVarInsn(ALOAD, 2);
977       mv.visitInsn(MONITOREXIT);
978       mv.visitLabel(l5);
979       Label l18 = new Label();
980       mv.visitJumpInsn(GOTO, l18);
981       mv.visitLabel(l6);
982       mv.visitVarInsn(ALOAD, 2);
983       mv.visitInsn(MONITOREXIT);
984       mv.visitLabel(l7);
985       mv.visitInsn(ATHROW);
986       mv.visitLabel(l18);
987       mv.visitVarInsn(ALOAD, 0);
988       mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "this$0", "Ljava/util/concurrent/FutureTask;");
989       mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/concurrent/FutureTask", "done", "()V");
990       Label l19 = new Label();
991       mv.visitLabel(l19);
992       mv.visitInsn(ICONST_1);
993       mv.visitInsn(IRETURN);
994       Label l20 = new Label();
995       mv.visitLabel(l20);
996       mv.visitMaxs(0, 0);
997       mv.visitEnd();
998     }
999
1000    private void addManagedTryReleaseSharedMethod() {
1001      MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, MANAGED_TRY_RELEASE_SHARED_METHOD, "()Z", null, null);
1002      mv.visitCode();
1003      Label l0 = new Label();
1004      mv.visitLabel(l0);
1005      mv.visitVarInsn(ALOAD, 0);
1006      mv.visitInsn(ACONST_NULL);
1007      mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", "runner", "Ljava/lang/Thread;");
1008      Label l1 = new Label();
1009      mv.visitLabel(l1);
1010      mv.visitVarInsn(ALOAD, 0);
1011      mv.visitInsn(ACONST_NULL);
1012      mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", PROXY_RUNNER_FIELD_NAME, "Ljava/lang/Object;");
1013      Label l2 = new Label();
1014      mv.visitLabel(l2);
1015      mv.visitVarInsn(ALOAD, 0);
1016      mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", MANAGED_OBJECT_LOCK_FIELD_NAME, "Ljava/lang/Object;");
1017      mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "notifyAll", "()V");
1018      Label l3 = new Label();
1019      mv.visitLabel(l3);
1020      mv.visitInsn(ICONST_1);
1021      mv.visitInsn(IRETURN);
1022      Label l4 = new Label();
1023      mv.visitLabel(l4);
1024      mv.visitMaxs(0, 0);
1025      mv.visitEnd();
1026    }
1027
1028    private void addManagedInnerCancelMethod() {
1029      MethodVisitor mv = cv.visitMethod(ACC_PRIVATE + ACC_SYNTHETIC, MAANGED_INNER_CANCEL_METHOD_NAME, "()V", null, null);
1030      mv.visitCode();
1031      Label l0 = new Label();
1032      mv.visitLabel(l0);
1033      /*mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
1034      mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
1035      mv.visitInsn(DUP);
1036      mv.visitLdcInsn("Running managedInnerCancel: ");
1037      mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V");
1038      mv.visitVarInsn(ALOAD, 0);
1039      mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "runner", "Ljava/lang/Thread;");
1040      mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;");
1041      mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
1042      mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
1043
1044      Label l1 = new Label();
1045      mv.visitLabel(l1);
1046      mv.visitTypeInsn(NEW, "java/lang/Exception");
1047      mv.visitInsn(DUP);
1048      mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Exception", "<init>", "()V");
1049      mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
1050      mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "(Ljava/io/PrintStream;)V");
1051      */

1052      Label l2 = new Label();
1053      mv.visitLabel(l2);
1054      mv.visitVarInsn(ALOAD, 0);
1055      mv.visitFieldInsn(GETFIELD, "java/util/concurrent/FutureTask$Sync", "runner", "Ljava/lang/Thread;");
1056      mv.visitVarInsn(ASTORE, 1);
1057      Label l3 = new Label();
1058      mv.visitLabel(l3);
1059      mv.visitVarInsn(ALOAD, 1);
1060      Label l4 = new Label();
1061      mv.visitJumpInsn(IFNULL, l4);
1062      Label l5 = new Label();
1063      mv.visitLabel(l5);
1064      mv.visitVarInsn(ALOAD, 1);
1065      mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Thread", "interrupt", "()V");
1066      mv.visitLabel(l4);
1067      mv.visitInsn(RETURN);
1068      Label l6 = new Label();
1069      mv.visitLabel(l6);
1070      mv.visitMaxs(0, 0);
1071      mv.visitEnd();
1072    }
1073
1074    private static class Constructor extends MethodAdapter implements Opcodes {
1075      public Constructor(MethodVisitor mv) {
1076        super(mv);
1077      }
1078
1079      public void visitFieldInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
1080        super.visitFieldInsn(opcode, owner, name, desc);
1081        if (PUTFIELD == opcode && "java/util/concurrent/FutureTask$Sync".equals(owner) && "callable".equals(name)
1082            && "Ljava/util/concurrent/Callable;".equals(desc)) {
1083          mv.visitVarInsn(ALOAD, 0);
1084          mv.visitTypeInsn(NEW, "java/lang/Object");
1085          mv.visitInsn(DUP);
1086          mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
1087          mv.visitFieldInsn(PUTFIELD, "java/util/concurrent/FutureTask$Sync", MANAGED_OBJECT_LOCK_FIELD_NAME,
1088                            "Ljava/lang/Object;");
1089
1090        }
1091      }
1092    }
1093  }
1094}
Popular Tags