001: /*
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.objectweb.asm.tree;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034: import java.util.Map;
035:
036: import org.objectweb.asm.MethodVisitor;
037: import org.objectweb.asm.Opcodes;
038:
039: /**
040: * A node that represents a stack map frame. These nodes are pseudo instruction
041: * nodes in order to be inserted in an instruction list. In fact these nodes
042: * must(*) be inserted <i>just before</i> any instruction node <b>i</b> that
043: * follows an unconditionnal branch instruction such as GOTO or THROW, that is
044: * the target of a jump instruction, or that starts an exception handler block.
045: * The stack map frame types must describe the values of the local variables and
046: * of the operand stack elements <i>just before</i> <b>i</b> is executed. <br>
047: * <br> (*) this is mandatory only for classes whose version is greater than or
048: * equal to {@link Opcodes#V1_6 V1_6}.
049: *
050: * @author Eric Bruneton
051: */
052: @SuppressWarnings("unchecked")
053: public class FrameNode extends AbstractInsnNode {
054:
055: /**
056: * The type of this frame. Must be {@link Opcodes#F_NEW} for expanded
057: * frames, or {@link Opcodes#F_FULL}, {@link Opcodes#F_APPEND},
058: * {@link Opcodes#F_CHOP}, {@link Opcodes#F_SAME} or
059: * {@link Opcodes#F_APPEND}, {@link Opcodes#F_SAME1} for compressed frames.
060: */
061: public int type;
062:
063: /**
064: * The types of the local variables of this stack map frame. Elements of
065: * this list can be Integer, String or LabelNode objects (for primitive,
066: * reference and uninitialized types respectively - see
067: * {@link MethodVisitor}).
068: */
069: public List local;
070:
071: /**
072: * The types of the operand stack elements of this stack map frame. Elements
073: * of this list can be Integer, String or LabelNode objects (for primitive,
074: * reference and uninitialized types respectively - see
075: * {@link MethodVisitor}).
076: */
077: public List stack;
078:
079: private FrameNode() {
080: super (-1);
081: }
082:
083: /**
084: * Constructs a new {@link FrameNode}.
085: *
086: * @param type the type of this frame. Must be {@link Opcodes#F_NEW} for
087: * expanded frames, or {@link Opcodes#F_FULL},
088: * {@link Opcodes#F_APPEND}, {@link Opcodes#F_CHOP},
089: * {@link Opcodes#F_SAME} or {@link Opcodes#F_APPEND},
090: * {@link Opcodes#F_SAME1} for compressed frames.
091: * @param nLocal number of local variables of this stack map frame.
092: * @param local the types of the local variables of this stack map frame.
093: * Elements of this list can be Integer, String or LabelNode objects
094: * (for primitive, reference and uninitialized types respectively -
095: * see {@link MethodVisitor}).
096: * @param nStack number of operand stack elements of this stack map frame.
097: * @param stack the types of the operand stack elements of this stack map
098: * frame. Elements of this list can be Integer, String or LabelNode
099: * objects (for primitive, reference and uninitialized types
100: * respectively - see {@link MethodVisitor}).
101: */
102: public FrameNode(final int type, final int nLocal,
103: final Object[] local, final int nStack, final Object[] stack) {
104: super (-1);
105: this .type = type;
106: switch (type) {
107: case Opcodes.F_NEW:
108: case Opcodes.F_FULL:
109: this .local = asList(nLocal, local);
110: this .stack = asList(nStack, stack);
111: break;
112: case Opcodes.F_APPEND:
113: this .local = asList(nLocal, local);
114: break;
115: case Opcodes.F_CHOP:
116: this .local = asList(nLocal, new Object[nLocal]);
117: break;
118: case Opcodes.F_SAME:
119: break;
120: case Opcodes.F_SAME1:
121: this .stack = asList(1, stack);
122: break;
123: }
124: }
125:
126: public int getType() {
127: return FRAME;
128: }
129:
130: /**
131: * Makes the given visitor visit this stack map frame.
132: *
133: * @param mv a method visitor.
134: */
135: public void accept(final MethodVisitor mv) {
136: switch (type) {
137: case Opcodes.F_NEW:
138: case Opcodes.F_FULL:
139: mv.visitFrame(type, local.size(), asArray(local), stack
140: .size(), asArray(stack));
141: break;
142: case Opcodes.F_APPEND:
143: mv.visitFrame(type, local.size(), asArray(local), 0, null);
144: break;
145: case Opcodes.F_CHOP:
146: mv.visitFrame(type, local.size(), null, 0, null);
147: break;
148: case Opcodes.F_SAME:
149: mv.visitFrame(type, 0, null, 0, null);
150: break;
151: case Opcodes.F_SAME1:
152: mv.visitFrame(type, 0, null, 1, asArray(stack));
153: break;
154: }
155: }
156:
157: public AbstractInsnNode clone(final Map labels) {
158: FrameNode clone = new FrameNode();
159: clone.type = type;
160: if (local != null) {
161: clone.local = new ArrayList();
162: for (int i = 0; i < local.size(); ++i) {
163: Object l = local.get(i);
164: if (l instanceof LabelNode) {
165: l = labels.get(l);
166: }
167: clone.local.add(l);
168: }
169: }
170: if (stack != null) {
171: clone.stack = new ArrayList();
172: for (int i = 0; i < stack.size(); ++i) {
173: Object s = stack.get(i);
174: if (s instanceof LabelNode) {
175: s = labels.get(s);
176: }
177: clone.stack.add(s);
178: }
179: }
180: return clone;
181: }
182:
183: // ------------------------------------------------------------------------
184:
185: private static List asList(final int n, final Object[] o) {
186: List l = new ArrayList(n);
187: for (int i = 0; i < n; ++i) {
188: l.add(o[i]);
189: }
190: return l;
191: }
192:
193: private static Object[] asArray(final List l) {
194: Object[] objs = new Object[l.size()];
195: for (int i = 0; i < objs.length; ++i) {
196: Object o = l.get(i);
197: if (o instanceof LabelNode) {
198: o = ((LabelNode) o).getLabel();
199: }
200: objs[i] = o;
201: }
202: return objs;
203: }
204: }
|