KickJava   Java API By Example, From Geeks To Geeks.

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


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.MethodAdapter;
8 import com.tc.asm.ClassVisitor;
9 import com.tc.asm.Label;
10 import com.tc.asm.MethodVisitor;
11 import com.tc.asm.Opcodes;
12
13 public class JavaUtilConcurrentCyclicBarrierDebugClassAdapter extends ClassAdapter implements Opcodes {
14
15   public JavaUtilConcurrentCyclicBarrierDebugClassAdapter(ClassVisitor cv) {
16     super(cv);
17   }
18
19   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
20     MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
21     if ("dowait".equals(name) && "(ZJ)I".equals(desc)) {
22       mv = new DoWaitMethodVisitor(mv);
23     }
24
25     return mv;
26   }
27
28   public void visitEnd() {
29     addDumpStateMethod();
30     super.visitEnd();
31   }
32   
33   private void addDumpStateMethod() {
34     MethodVisitor mv = super.visitMethod(ACC_PRIVATE + ACC_SYNTHETIC, "dumpState", "()V", null, null);
35     mv.visitCode();
36     Label l0 = new Label();
37     mv.visitLabel(l0);
38     mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
39     mv.visitLdcInsn("Current Status CyclicBarrier:");
40     mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
41     Label l1 = new Label();
42     mv.visitLabel(l1);
43     mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
44     mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
45     mv.visitInsn(DUP);
46     mv.visitLdcInsn("-- parties: ");
47     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
48     mv.visitVarInsn(ALOAD, 0);
49     mv.visitFieldInsn(GETFIELD, "java/util/concurrent/CyclicBarrier", "parties", "I");
50     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(I)Ljava/lang/StringBuffer;");
51     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
52     mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
53     Label l2 = new Label();
54     mv.visitLabel(l2);
55     mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
56     mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
57     mv.visitInsn(DUP);
58     mv.visitLdcInsn("-- barrierCommand: ");
59     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
60     mv.visitVarInsn(ALOAD, 0);
61     mv.visitFieldInsn(GETFIELD, "java/util/concurrent/CyclicBarrier", "barrierCommand", "Ljava/lang/Runnable;");
62     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuffer;");
63     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
64     mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
65     Label l3 = new Label();
66     mv.visitLabel(l3);
67     mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
68     mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
69     mv.visitInsn(DUP);
70     mv.visitLdcInsn("-- generation: ");
71     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
72     mv.visitVarInsn(ALOAD, 0);
73     mv.visitFieldInsn(GETFIELD, "java/util/concurrent/CyclicBarrier", "generation", "Ljava/util/concurrent/CyclicBarrier$Generation;");
74     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuffer;");
75     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
76     mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
77     Label l4 = new Label();
78     mv.visitLabel(l4);
79     mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
80     mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
81     mv.visitInsn(DUP);
82     mv.visitLdcInsn("-- trip: ");
83     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
84     mv.visitVarInsn(ALOAD, 0);
85     mv.visitFieldInsn(GETFIELD, "java/util/concurrent/CyclicBarrier", "trip", "Ljava/util/concurrent/locks/Condition;");
86     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuffer;");
87     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
88     mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
89     Label l5 = new Label();
90     mv.visitLabel(l5);
91     mv.visitInsn(RETURN);
92     Label l6 = new Label();
93     mv.visitLabel(l6);
94     mv.visitMaxs(0, 0);
95     mv.visitEnd();
96   }
97   
98   private static class DoWaitMethodVisitor extends MethodAdapter implements Opcodes {
99     private Label target;
100     
101     public DoWaitMethodVisitor(MethodVisitor mv) {
102       super(mv);
103     }
104     
105     public void visitJumpInsn(int opcode, Label label) {
106       super.visitJumpInsn(opcode, label);
107       if (IF_ACMPEQ == opcode) {
108         target = label;
109       }
110     }
111     
112     public void visitLabel(Label label) {
113       super.visitLabel(label);
114       if (label.equals(target)) {
115         mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
116         mv.visitLdcInsn("Wake up, but local generation the same as new generation.");
117         mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
118         Label l42 = new Label();
119         mv.visitLabel(l42);
120         mv.visitLineNumber(124, l42);
121         mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
122         mv.visitLdcInsn("Entering debug block.");
123         mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
124         Label l43 = new Label();
125         mv.visitLabel(l43);
126         mv.visitLineNumber(125, l43);
127         mv.visitVarInsn(ALOAD, 0);
128         mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/CyclicBarrier", "dumpState", "()V");
129         Label l44 = new Label();
130         mv.visitLabel(l44);
131         mv.visitLineNumber(126, l44);
132         mv.visitLdcInsn(new Long JavaDoc(5000L));
133         mv.visitMethodInsn(INVOKESTATIC, "java/lang/Thread", "sleep", "(J)V");
134         Label l45 = new Label();
135         mv.visitLabel(l45);
136         mv.visitLineNumber(127, l45);
137         mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
138         mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
139         mv.visitInsn(DUP);
140         mv.visitLdcInsn("Is local generation equal to new generation: ");
141         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
142         mv.visitVarInsn(ALOAD, 5);
143         mv.visitVarInsn(ALOAD, 0);
144         mv.visitFieldInsn(GETFIELD, "java/util/concurrent/CyclicBarrier", "generation", "Ljava/util/concurrent/CyclicBarrier$Generation;");
145         Label l46 = new Label();
146         mv.visitJumpInsn(IF_ACMPNE, l46);
147         mv.visitInsn(ICONST_1);
148         Label l47 = new Label();
149         mv.visitJumpInsn(GOTO, l47);
150         mv.visitLabel(l46);
151         mv.visitInsn(ICONST_0);
152         mv.visitLabel(l47);
153         mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Z)Ljava/lang/StringBuffer;");
154         mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
155         mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
156         Label l48 = new Label();
157         mv.visitLabel(l48);
158         mv.visitLineNumber(128, l48);
159         mv.visitVarInsn(ALOAD, 0);
160         mv.visitMethodInsn(INVOKESPECIAL, "java/util/concurrent/CyclicBarrier", "dumpState", "()V");
161         Label l49 = new Label();
162         mv.visitLabel(l49);
163       }
164     }
165   }
166 }
Popular Tags