KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > asm > DecompilerMethodVisitor


1 package de.loskutov.bco.asm;
2
3 import java.util.BitSet JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.objectweb.asm.AnnotationVisitor;
9 import org.objectweb.asm.Attribute;
10 import org.objectweb.asm.Label;
11 import org.objectweb.asm.MethodAdapter;
12 import org.objectweb.asm.MethodVisitor;
13 import org.objectweb.asm.Opcodes;
14 import org.objectweb.asm.tree.LabelNode;
15 import org.objectweb.asm.tree.MethodNode;
16 import org.objectweb.asm.util.AbstractVisitor;
17
18 import de.loskutov.bco.asm.CommentedClassVisitor.CommentedAnnotationVisitor;
19
20 /**
21  * @author Eric Bruneton
22  */

23
24 public class DecompilerMethodVisitor extends MethodAdapter {
25
26     private String JavaDoc owner;
27
28     private List JavaDoc text;
29
30     private MethodNode meth;
31
32     private LabelNode currentLabel;
33
34     private int currentInsn;
35
36     private Map JavaDoc lineNumbers;
37
38     private final BitSet JavaDoc modes;
39
40     public DecompilerMethodVisitor(final String JavaDoc owner, final MethodNode meth,
41         final MethodVisitor mv, BitSet JavaDoc modes) {
42         super(mv);
43         this.owner = owner;
44         this.modes = modes;
45         this.text = ((AbstractVisitor) mv).getText();
46         this.meth = meth;
47         this.lineNumbers = new HashMap JavaDoc();
48     }
49
50     public DecompiledMethod getResult(final ClassLoader JavaDoc cl) {
51         return new DecompiledMethod(owner, text, lineNumbers, meth, cl, modes);
52     }
53
54     public AnnotationVisitor visitAnnotationDefault() {
55         AnnotationVisitor annVisitor = super.visitAnnotationDefault();
56         AnnotationVisitor visitor = meth.visitAnnotationDefault();
57         if (annVisitor instanceof CommentedAnnotationVisitor) {
58             CommentedAnnotationVisitor av = (CommentedAnnotationVisitor) annVisitor;
59             av.setAnnotationVisitor(visitor);
60         }
61         return annVisitor;
62     }
63
64     public AnnotationVisitor visitAnnotation(String JavaDoc desc, boolean visible) {
65         AnnotationVisitor annVisitor = super.visitAnnotation(desc, visible);
66         AnnotationVisitor visitor = meth.visitAnnotation(desc, visible);
67         if (annVisitor instanceof CommentedAnnotationVisitor) {
68             CommentedAnnotationVisitor av = (CommentedAnnotationVisitor) annVisitor;
69             av.setAnnotationVisitor(visitor);
70         }
71         return annVisitor;
72     }
73
74     public AnnotationVisitor visitParameterAnnotation(int parameter,
75         String JavaDoc desc, boolean visible) {
76         AnnotationVisitor annVisitor = super.visitParameterAnnotation(
77             parameter, desc, visible);
78         AnnotationVisitor visitor = meth.visitParameterAnnotation(
79             parameter, desc, visible);
80         if (annVisitor instanceof CommentedAnnotationVisitor) {
81             CommentedAnnotationVisitor av = (CommentedAnnotationVisitor) annVisitor;
82             av.setAnnotationVisitor(visitor);
83         }
84         return annVisitor;
85     }
86
87     public void visitCode() {
88         super.visitCode();
89         meth.visitCode();
90     }
91
92     public void visitEnd() {
93         super.visitEnd();
94         meth.visitEnd();
95     }
96
97     public void visitAttribute(Attribute attr) {
98         super.visitAttribute(attr);
99         meth.visitAttribute(attr);
100     }
101
102     public void visitInsn(final int opcode) {
103         addIndex(opcode);
104         super.visitInsn(opcode);
105         meth.visitInsn(opcode);
106     }
107
108     public void visitIntInsn(final int opcode, final int operand) {
109         addIndex(opcode);
110         super.visitIntInsn(opcode, operand);
111         meth.visitIntInsn(opcode, operand);
112     }
113
114     public void visitVarInsn(final int opcode, final int var) {
115         addIndex(opcode);
116         super.visitVarInsn(opcode, var);
117         meth.visitVarInsn(opcode, var);
118     }
119
120     public void visitTypeInsn(final int opcode, final String JavaDoc desc) {
121         addIndex(opcode);
122         super.visitTypeInsn(opcode, desc);
123         meth.visitTypeInsn(opcode, desc);
124     }
125
126     public void visitFieldInsn(final int opcode, final String JavaDoc owner1,
127         final String JavaDoc name, final String JavaDoc desc) {
128         addIndex(opcode);
129         super.visitFieldInsn(opcode, owner1, name, desc);
130         meth.visitFieldInsn(opcode, owner1, name, desc);
131     }
132
133     public void visitMethodInsn(final int opcode, final String JavaDoc owner1,
134         final String JavaDoc name, final String JavaDoc desc) {
135         addIndex(opcode);
136         super.visitMethodInsn(opcode, owner1, name, desc);
137         meth.visitMethodInsn(opcode, owner1, name, desc);
138     }
139
140     public void visitJumpInsn(final int opcode, final Label label) {
141         addIndex(opcode);
142         super.visitJumpInsn(opcode, label);
143         meth.visitJumpInsn(opcode, label);
144     }
145
146     public void visitLabel(final Label label) {
147         super.visitLabel(label);
148         meth.visitLabel(label);
149         currentLabel = (LabelNode) meth.instructions.getLast();
150         addIndex(-1);
151     }
152
153     public void visitLdcInsn(final Object JavaDoc cst) {
154         addIndex(Opcodes.LDC);
155         super.visitLdcInsn(cst);
156         meth.visitLdcInsn(cst);
157     }
158
159     public void visitIincInsn(final int var, final int increment) {
160         addIndex(Opcodes.IINC);
161         super.visitIincInsn(var, increment);
162         meth.visitIincInsn(var, increment);
163     }
164
165     public void visitTableSwitchInsn(final int min, final int max,
166         final Label dflt, final Label[] labels) {
167         addIndex(Opcodes.TABLESWITCH);
168         super.visitTableSwitchInsn(min, max, dflt, labels);
169         meth.visitTableSwitchInsn(min, max, dflt, labels);
170     }
171
172     public void visitLookupSwitchInsn(final Label dflt, final int[] keys,
173         final Label[] labels) {
174         addIndex(Opcodes.LOOKUPSWITCH);
175         super.visitLookupSwitchInsn(dflt, keys, labels);
176         meth.visitLookupSwitchInsn(dflt, keys, labels);
177     }
178
179     public void visitMultiANewArrayInsn(final String JavaDoc desc, final int dims) {
180         addIndex(Opcodes.MULTIANEWARRAY);
181         super.visitMultiANewArrayInsn(desc, dims);
182         meth.visitMultiANewArrayInsn(desc, dims);
183     }
184
185     public void visitTryCatchBlock(final Label start, final Label end,
186         final Label handler, final String JavaDoc type) {
187         super.visitTryCatchBlock(start, end, handler, type);
188         meth.visitTryCatchBlock(start, end, handler, type);
189     }
190
191     public void visitLocalVariable(final String JavaDoc name, final String JavaDoc desc,
192         final String JavaDoc signature, final Label start, final Label end,
193         final int index) {
194         // localVariables.add(new LocalVariableNode(
195
// name, desc, signature, new LabelNode(start), new LabelNode(end), index));
196
super.visitLocalVariable(name, desc, signature, start, end, index);
197         meth.visitLocalVariable(name, desc, signature, start, end, index);
198     }
199
200     public void visitLineNumber(final int line, final Label start) {
201         lineNumbers.put(start, new Integer JavaDoc(line));
202         super.visitLineNumber(line, start);
203         meth.visitLineNumber(line, start);
204     }
205
206     public void visitMaxs(final int maxStack, final int maxLocals) {
207         super.visitMaxs(maxStack, maxLocals);
208         meth.visitMaxs(maxStack, maxLocals);
209     }
210
211     public void visitFrame(int type, int nLocal, Object JavaDoc[] local, int nStack,
212         Object JavaDoc[] stack) {
213         addIndex(-1);
214         super.visitFrame(type, nLocal, local, nStack, stack);
215         meth.visitFrame(type, nLocal, local, nStack, stack);
216     }
217
218     protected void addIndex(final int opcode) {
219         text.add(new Index(currentLabel, currentInsn++, opcode));
220     }
221 }
222
Popular Tags