KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > asm > tree > TreeCodeAdapter


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000,2002,2003 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Contact: Eric.Bruneton@rd.francetelecom.com
31  *
32  * Author: Eric Bruneton
33  */

34
35 package org.logicalcobwebs.asm.tree;
36
37 import org.logicalcobwebs.asm.CodeAdapter;
38 import org.logicalcobwebs.asm.Label;
39 import org.logicalcobwebs.asm.Attribute;
40
41 /**
42  * A {@link CodeAdapter CodeAdapter} that constructs a tree representation of
43  * the methods it vists. Each <tt>visit</tt><i>XXX</i> method of this class
44  * constructs an <i>XXX</i><tt>Node</tt> and adds it to the {@link #methodNode
45  * methodNode} node.
46  */

47
48 public class TreeCodeAdapter extends CodeAdapter {
49
50   /**
51    * A tree representation of the method that is being visited by this visitor.
52    */

53
54   public MethodNode methodNode;
55
56   /**
57    * Constructs a new {@link TreeCodeAdapter TreeCodeAdapter} object.
58    *
59    * @param methodNode the method node to be used to store the tree
60    * representation constructed by this code visitor.
61    */

62
63   public TreeCodeAdapter (final MethodNode methodNode) {
64     super(null);
65     this.methodNode = methodNode;
66   }
67
68   public void visitInsn (final int opcode) {
69     AbstractInsnNode n = new InsnNode(opcode);
70     methodNode.instructions.add(n);
71   }
72
73   public void visitIntInsn (final int opcode, final int operand) {
74     AbstractInsnNode n = new IntInsnNode(opcode, operand);
75     methodNode.instructions.add(n);
76   }
77
78   public void visitVarInsn (final int opcode, final int var) {
79     AbstractInsnNode n = new VarInsnNode(opcode, var);
80     methodNode.instructions.add(n);
81   }
82
83   public void visitTypeInsn (final int opcode, final String JavaDoc desc) {
84     AbstractInsnNode n = new TypeInsnNode(opcode, desc);
85     methodNode.instructions.add(n);
86   }
87
88   public void visitFieldInsn (
89     final int opcode,
90     final String JavaDoc owner,
91     final String JavaDoc name,
92     final String JavaDoc desc)
93   {
94     AbstractInsnNode n = new FieldInsnNode(opcode, owner, name, desc);
95     methodNode.instructions.add(n);
96   }
97
98   public void visitMethodInsn (
99     final int opcode,
100     final String JavaDoc owner,
101     final String JavaDoc name,
102     final String JavaDoc desc)
103   {
104     AbstractInsnNode n = new MethodInsnNode(opcode, owner, name, desc);
105     methodNode.instructions.add(n);
106   }
107
108   public void visitJumpInsn (final int opcode, final Label label) {
109     AbstractInsnNode n = new JumpInsnNode(opcode, label);
110     methodNode.instructions.add(n);
111   }
112
113   public void visitLabel (final Label label) {
114     methodNode.instructions.add(label);
115   }
116
117   public void visitLdcInsn (final Object JavaDoc cst) {
118     AbstractInsnNode n = new LdcInsnNode(cst);
119     methodNode.instructions.add(n);
120   }
121
122   public void visitIincInsn (final int var, final int increment) {
123     AbstractInsnNode n = new IincInsnNode(var, increment);
124     methodNode.instructions.add(n);
125   }
126
127   public void visitTableSwitchInsn (
128     final int min,
129     final int max,
130     final Label dflt,
131     final Label labels[])
132   {
133     AbstractInsnNode n = new TableSwitchInsnNode(min, max, dflt, labels);
134     methodNode.instructions.add(n);
135   }
136
137   public void visitLookupSwitchInsn (
138     final Label dflt,
139     final int keys[],
140     final Label labels[])
141   {
142     AbstractInsnNode n = new LookupSwitchInsnNode(dflt, keys, labels);
143     methodNode.instructions.add(n);
144   }
145
146   public void visitMultiANewArrayInsn (final String JavaDoc desc, final int dims) {
147     AbstractInsnNode n = new MultiANewArrayInsnNode(desc, dims);
148     methodNode.instructions.add(n);
149   }
150
151   public void visitTryCatchBlock (
152     final Label start,
153     final Label end,
154     final Label handler,
155     final String JavaDoc type)
156   {
157     TryCatchBlockNode n = new TryCatchBlockNode(start, end, handler, type);
158     methodNode.tryCatchBlocks.add(n);
159   }
160
161   public void visitMaxs (final int maxStack, final int maxLocals) {
162     methodNode.maxStack = maxStack;
163     methodNode.maxLocals = maxLocals;
164   }
165
166   public void visitLocalVariable (
167     final String JavaDoc name,
168     final String JavaDoc desc,
169     final Label start,
170     final Label end,
171     final int index)
172   {
173     LocalVariableNode n = new LocalVariableNode(name, desc, start, end, index);
174     methodNode.localVariables.add(n);
175   }
176
177   public void visitLineNumber (final int line, final Label start) {
178     LineNumberNode n = new LineNumberNode(line, start);
179     methodNode.lineNumbers.add(n);
180   }
181
182   public void visitAttribute (final Attribute attr) {
183     attr.next = methodNode.attrs;
184     methodNode.attrs = attr;
185   }
186 }
187
Popular Tags