KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > attrs > StackMapFrame


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
31 package oracle.toplink.libraries.asm.attrs;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Set JavaDoc;
37
38 import oracle.toplink.libraries.asm.ByteVector;
39 import oracle.toplink.libraries.asm.ClassReader;
40 import oracle.toplink.libraries.asm.ClassWriter;
41 import oracle.toplink.libraries.asm.Label;
42
43
44 /**
45  * StackMapFrame is used by {@link StackMapAttribute} to hold state of the stack
46  * and local variables for a single execution branch.
47  *
48  * <i>Note that Long and Double types are represented by two entries in locals
49  * and stack. Second entry sohould be always of type Top.</i>
50  *
51  * @see <a HREF="http://www.jcp.org/en/jsr/detail?id=139">JSR 139 : Connected
52  * Limited Device Configuration 1.1</a>
53  *
54  * @author Eugene Kuleshov
55  */

56
57 public class StackMapFrame {
58
59   public Label label;
60
61   public List JavaDoc locals = new ArrayList JavaDoc();
62
63   public List JavaDoc stack = new ArrayList JavaDoc();
64
65   public int read (ClassReader cr,
66                    int off, char[] buf, int codeOff, Label[] labels) {
67     int n = cr.readUnsignedShort(off);
68     off += 2;
69     if (labels[n] == null) {
70       labels[n] = new Label();
71     }
72     label = labels[n];
73     off = readTypeInfo(cr, off, locals, labels, buf,
74                        cr.readUnsignedShort(codeOff + 2)); // maxLocals
75
off = readTypeInfo(cr, off, stack, labels, buf,
76                        cr.readUnsignedShort(codeOff)); // maxStack
77
return off;
78   }
79
80   public void write (ClassWriter cw,
81                      int maxStack, int maxLocals, ByteVector bv) {
82     bv.putShort(label.getOffset());
83     writeTypeInfo(bv, cw, locals, maxLocals);
84     writeTypeInfo(bv, cw, stack, maxStack);
85   }
86
87   public void getLabels (Set JavaDoc labels) {
88     labels.add(label);
89     getTypeInfoLabels(labels, locals);
90     getTypeInfoLabels(labels, stack);
91   }
92
93   private void getTypeInfoLabels (Set JavaDoc labels, List JavaDoc info) {
94     for (Iterator JavaDoc it = info.iterator(); it.hasNext();) {
95       StackMapType typeInfo = (StackMapType)it.next();
96       if (typeInfo.getType() == StackMapType.ITEM_Uninitialized) {
97         labels.add(typeInfo.getLabel());
98       }
99     }
100   }
101
102   private int readTypeInfo (ClassReader cr, int off,
103                             List JavaDoc info, Label[] labels, char[] buf, int max) {
104     int n = 0;
105     if (max > StackMapAttribute.MAX_SIZE) {
106       n = cr.readInt(off);
107       off += 4;
108     } else {
109       n = cr.readUnsignedShort(off);
110       off += 2;
111     }
112     for (int j = 0; j < n; j++) {
113       int itemType = cr.readByte(off++);
114       StackMapType typeInfo = StackMapType.getTypeInfo(itemType);
115       info.add(typeInfo);
116       switch (itemType) {
117         // case StackMapType.ITEM_Long: //
118
// case StackMapType.ITEM_Double: //
119
// info.add(StackMapType.getTypeInfo(StackMapType.ITEM_Top));
120
// break;
121

122         case StackMapType.ITEM_Object: //
123
typeInfo.setObject(cr.readClass(off, buf));
124           off += 2;
125           break;
126
127         case StackMapType.ITEM_Uninitialized: //
128
int o = cr.readUnsignedShort(off);
129           off += 2;
130           if (labels[o] == null) {
131             labels[o] = new Label();
132           }
133           typeInfo.setLabel(labels[o]);
134           break;
135       }
136     }
137     return off;
138   }
139
140   private void writeTypeInfo (ByteVector bv,
141                               ClassWriter cw, List JavaDoc info, int max) {
142     if (max > StackMapAttribute.MAX_SIZE) {
143       bv.putInt(info.size());
144     } else {
145       bv.putShort(info.size());
146     }
147     for (int j = 0; j < info.size(); j++) {
148       StackMapType typeInfo = (StackMapType)info.get(j);
149       bv.putByte(typeInfo.getType());
150       switch (typeInfo.getType()) {
151         case StackMapType.ITEM_Object: //
152
bv.putShort(cw.newClass(typeInfo.getObject()));
153           break;
154
155         case StackMapType.ITEM_Uninitialized: //
156
bv.putShort(typeInfo.getLabel().getOffset());
157           break;
158           
159       }
160     }
161   }
162
163   public String JavaDoc toString () {
164     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Frame:L");
165     sb.append(System.identityHashCode(label));
166     sb.append(" locals").append(locals);
167     sb.append(" stack").append(stack);
168     return sb.toString();
169   }
170 }
171
Popular Tags