KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
35
36 import oracle.toplink.libraries.asm.Attribute;
37 import oracle.toplink.libraries.asm.ByteVector;
38 import oracle.toplink.libraries.asm.ClassReader;
39 import oracle.toplink.libraries.asm.ClassWriter;
40 import oracle.toplink.libraries.asm.Label;
41
42 /**
43  * StackMapAttribute is used by CDLC preverifier and also by javac compiller
44  * starting from J2SE 1.5. Definition is given in appendix "CLDC Byte Code
45  * Typechecker Specification" from CDLC 1.1 specification.
46  * <p>
47  * <i>Note that this implementation does not calculate StackMapFrame structures
48  * from the method bytecode. If method code is changed or generated from scratch,
49  * then developer is responsible to prepare a correct StackMapFrame structures.</i>
50  * <p>
51  * The format of the stack map in the class file is given below. In the following,
52  * <ul>
53  * <li>if the length of the method's byte code1 is 65535 or less, then <tt>uoffset</tt>
54  * represents the type u2; otherwise <tt>uoffset</tt> represents the type u4.</li>
55  * <li>If the maximum number of local variables for the method is 65535 or less,
56  * then <tt>ulocalvar</tt> represents the type u2; otherwise <tt>ulocalvar</tt>
57  * represents the type u4.</li>
58  * <li>If the maximum size of the operand stack is 65535 or less, then <tt>ustack</tt>
59  * represents the type u2; otherwise ustack represents the type u4.</li>
60  * </ul>
61  *
62  * <pre>
63  * stack_map { // attribute StackMap
64  * u2 attribute_name_index;
65  * u4 attribute_length
66  * uoffset number_of_entries;
67  * stack_map_frame entries[number_of_entries];
68  * }
69  * </pre>
70  * Each stack map frame has the following format:
71  * <pre>
72  * stack_map_frame {
73  * uoffset offset;
74  * ulocalvar number_of_locals;
75  * verification_type_info locals[number_of_locals];
76  * ustack number_of_stack_items;
77  * verification_type_info stack[number_of_stack_items];
78  * }
79  * </pre>
80  * The <tt>verification_type_info</tt> structure consists of a one-byte tag
81  * followed by zero or more bytes, giving more information about the tag.
82  * Each <tt>verification_type_info</tt> structure specifies the verification
83  * type of one or two locations.
84  * <pre>
85  * union verification_type_info {
86  * Top_variable_info;
87  * Integer_variable_info;
88  * Float_variable_info;
89  * Long_variable_info;
90  * Double_variable_info;
91  * Null_variable_info;
92  * UninitializedThis_variable_info;
93  * Object_variable_info;
94  * Uninitialized_variable_info;
95  * }
96  *
97  * Top_variable_info {
98  * u1 tag = ITEM_Top; // 0
99  * }
100  *
101  * Integer_variable_info {
102  * u1 tag = ITEM_Integer; // 1
103  * }
104  *
105  * Float_variable_info {
106  * u1 tag = ITEM_Float; // 2
107  * }
108  *
109  * Long_variable_info {
110  * u1 tag = ITEM_Long; // 4
111  * }
112  *
113  * Double_variable_info {
114  * u1 tag = ITEM_Double; // 3
115  * }
116  *
117  * Null_variable_info {
118  * u1 tag = ITEM_Null; // 5
119  * }
120  *
121  * UninitializedThis_variable_info {
122  * u1 tag = ITEM_UninitializedThis; // 6
123  * }
124  *
125  * Object_variable_info {
126  * u1 tag = ITEM_Object; // 7
127  * u2 cpool_index;
128  * }
129  *
130  * Uninitialized_variable_info {
131  * u1 tag = ITEM_Uninitialized // 8
132  * uoffset offset;
133  * }
134  * </pre>
135  *
136  * @see <a HREF="http://www.jcp.org/en/jsr/detail?id=139">JSR 139 : Connected
137  * Limited Device Configuration 1.1</a>
138  *
139  * @author Eugene Kuleshov
140  */

141
142 public class StackMapAttribute extends Attribute {
143
144   static final int MAX_SIZE = 65535;
145
146   public ArrayList JavaDoc frames = new ArrayList JavaDoc();
147
148   public StackMapAttribute () {
149     super("StackMap");
150   }
151
152   public StackMapFrame getFrame (Label label) {
153     for (int i = 0; i < frames.size(); i++) {
154       StackMapFrame frame = (StackMapFrame)frames.get(i);
155       if (frame.label == label) {
156         return frame;
157       }
158     }
159     return null;
160   }
161
162   protected Attribute read (ClassReader cr, int off, int len,
163                             char[] buf, int codeOff, Label[] labels) {
164     StackMapAttribute attr = new StackMapAttribute();
165     // note that this is not the size of Code attribute
166
int codeSize = cr.readInt(codeOff + 4);
167     int size = 0;
168     if (codeSize > MAX_SIZE) {
169       size = cr.readInt(off);
170       off += 4;
171     } else {
172       size = cr.readShort(off);
173       off += 2;
174     }
175     for (int i = 0; i < size; i++) {
176       StackMapFrame frame = new StackMapFrame();
177       off = frame.read(cr, off, buf, codeOff, labels);
178       attr.frames.add(frame);
179     }
180     return attr;
181   }
182
183   protected ByteVector write (ClassWriter cw, byte[] code,
184                               int len, int maxStack, int maxLocals) {
185     ByteVector bv = new ByteVector();
186     if ( code!=null && code.length > MAX_SIZE) {
187       bv.putInt(frames.size());
188     } else {
189       bv.putShort(frames.size());
190     }
191     for (int i = 0; i < frames.size(); i++) {
192       ((StackMapFrame)frames.get(i)).write(cw, maxStack, maxLocals, bv);
193     }
194     return bv;
195   }
196
197   protected Label[] getLabels () {
198     HashSet JavaDoc labels = new HashSet JavaDoc();
199     for (int i = 0; i < frames.size(); i++) {
200       ((StackMapFrame)frames.get(i)).getLabels(labels);
201     }
202     return (Label[])labels.toArray(new Label[labels.size()]);
203   }
204
205   public String JavaDoc toString () {
206     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("StackMap[");
207     for (int i = 0; i < frames.size(); i++) {
208       sb.append('\n').append('[').append(frames.get(i)).append(']');
209     }
210     sb.append("\n]");
211     return sb.toString();
212   }
213 }
214
Popular Tags