KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > tree > FrameNode


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 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 package org.objectweb.asm.tree;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.objectweb.asm.MethodVisitor;
37 import org.objectweb.asm.Opcodes;
38
39 /**
40  * A node that represents a stack map frame. These nodes are pseudo instruction
41  * nodes in order to be inserted in an instruction list. In fact these nodes
42  * must(*) be inserted <i>just before</i> any instruction node <b>i</b> that
43  * follows an unconditionnal branch instruction such as GOTO or THROW, that is
44  * the target of a jump instruction, or that starts an exception handler block.
45  * The stack map frame types must describe the values of the local variables and
46  * of the operand stack elements <i>just before</i> <b>i</b> is executed. <br>
47  * <br> (*) this is mandatory only for classes whose version is greater than or
48  * equal to {@link Opcodes#V1_6 V1_6}.
49  *
50  * @author Eric Bruneton
51  */

52 public class FrameNode extends AbstractInsnNode {
53
54     /**
55      * The type of this frame. Must be {@link Opcodes#F_NEW} for expanded
56      * frames, or {@link Opcodes#F_FULL}, {@link Opcodes#F_APPEND},
57      * {@link Opcodes#F_CHOP}, {@link Opcodes#F_SAME} or
58      * {@link Opcodes#F_APPEND}, {@link Opcodes#F_SAME1} for compressed frames.
59      */

60     public int type;
61
62     /**
63      * The types of the local variables of this stack map frame. Elements of
64      * this list can be Integer, String or LabelNode objects (for primitive,
65      * reference and uninitialized types respectively - see
66      * {@link MethodVisitor}).
67      */

68     public List JavaDoc local;
69
70     /**
71      * The types of the operand stack elements of this stack map frame. Elements
72      * of this list can be Integer, String or LabelNode objects (for primitive,
73      * reference and uninitialized types respectively - see
74      * {@link MethodVisitor}).
75      */

76     public List JavaDoc stack;
77
78     private FrameNode() {
79         super(-1);
80     }
81
82     /**
83      * Constructs a new {@link FrameNode}.
84      *
85      * @param type the type of this frame. Must be {@link Opcodes#F_NEW} for
86      * expanded frames, or {@link Opcodes#F_FULL},
87      * {@link Opcodes#F_APPEND}, {@link Opcodes#F_CHOP},
88      * {@link Opcodes#F_SAME} or {@link Opcodes#F_APPEND},
89      * {@link Opcodes#F_SAME1} for compressed frames.
90      * @param nLocal number of local variables of this stack map frame.
91      * @param local the types of the local variables of this stack map frame.
92      * Elements of this list can be Integer, String or LabelNode objects
93      * (for primitive, reference and uninitialized types respectively -
94      * see {@link MethodVisitor}).
95      * @param nStack number of operand stack elements of this stack map frame.
96      * @param stack the types of the operand stack elements of this stack map
97      * frame. Elements of this list can be Integer, String or LabelNode
98      * objects (for primitive, reference and uninitialized types
99      * respectively - see {@link MethodVisitor}).
100      */

101     public FrameNode(
102         final int type,
103         final int nLocal,
104         final Object JavaDoc[] local,
105         final int nStack,
106         final Object JavaDoc[] stack)
107     {
108         super(-1);
109         this.type = type;
110         switch (type) {
111             case Opcodes.F_NEW:
112             case Opcodes.F_FULL:
113                 this.local = asList(nLocal, local);
114                 this.stack = asList(nStack, stack);
115                 break;
116             case Opcodes.F_APPEND:
117                 this.local = asList(nLocal, local);
118                 break;
119             case Opcodes.F_CHOP:
120                 this.local = asList(nLocal, new Object JavaDoc[nLocal]);
121                 break;
122             case Opcodes.F_SAME:
123                 break;
124             case Opcodes.F_SAME1:
125                 this.stack = asList(1, stack);
126                 break;
127         }
128     }
129
130     public int getType() {
131         return FRAME;
132     }
133
134     /**
135      * Makes the given visitor visit this stack map frame.
136      *
137      * @param mv a method visitor.
138      */

139     public void accept(final MethodVisitor mv) {
140         switch (type) {
141             case Opcodes.F_NEW:
142             case Opcodes.F_FULL:
143                 mv.visitFrame(type,
144                         local.size(),
145                         asArray(local),
146                         stack.size(),
147                         asArray(stack));
148                 break;
149             case Opcodes.F_APPEND:
150                 mv.visitFrame(type, local.size(), asArray(local), 0, null);
151                 break;
152             case Opcodes.F_CHOP:
153                 mv.visitFrame(type, local.size(), null, 0, null);
154                 break;
155             case Opcodes.F_SAME:
156                 mv.visitFrame(type, 0, null, 0, null);
157                 break;
158             case Opcodes.F_SAME1:
159                 mv.visitFrame(type, 0, null, 1, asArray(stack));
160                 break;
161         }
162     }
163
164     public AbstractInsnNode clone(final Map JavaDoc labels) {
165         FrameNode clone = new FrameNode();
166         clone.type = type;
167         if (local != null) {
168             clone.local = new ArrayList JavaDoc();
169             for (int i = 0; i < local.size(); ++i) {
170                 Object JavaDoc l = local.get(i);
171                 if (l instanceof LabelNode) {
172                     l = labels.get(l);
173                 }
174                 clone.local.add(l);
175             }
176         }
177         if (stack != null) {
178             clone.stack = new ArrayList JavaDoc();
179             for (int i = 0; i < stack.size(); ++i) {
180                 Object JavaDoc s = stack.get(i);
181                 if (s instanceof LabelNode) {
182                     s = labels.get(s);
183                 }
184                 clone.stack.add(s);
185             }
186         }
187         return clone;
188     }
189
190     // ------------------------------------------------------------------------
191

192     private static List JavaDoc asList(final int n, final Object JavaDoc[] o) {
193         List JavaDoc l = new ArrayList JavaDoc(n);
194         for (int i = 0; i < n; ++i) {
195             l.add(o[i]);
196         }
197         return l;
198     }
199
200     private static Object JavaDoc[] asArray(final List JavaDoc l) {
201         Object JavaDoc[] objs = new Object JavaDoc[l.size()];
202         for (int i = 0; i < objs.length; ++i) {
203             Object JavaDoc o = l.get(i);
204             if (o instanceof LabelNode) {
205                 o = ((LabelNode) o).getLabel();
206             }
207             objs[i] = o;
208         }
209         return objs;
210     }
211 }
212
Popular Tags