KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.List JavaDoc;
36
37 import oracle.toplink.libraries.asm.Attribute;
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  * The LocalVariableTypeTable attribute is an optional variable-length attribute of a Code
45  * attribute. It may be used by debuggers to determine the value of a given
46  * local variable during the execution of a method. If LocalVariableTypeTable attributes
47  * are present in the attributes table of a given Code attribute, then they may appear in
48  * any order. There may be no more than one LocalVariableTypeTable attribute per local
49  * variable in the Code attribute.
50  *
51  * The LocalVariableTypeTable attribute differs from the LocalVariableTable attribute in that
52  * it provides signature information rather than descriptor information. This difference
53  * is only significant for variables whose type is a generic reference type. Such
54  * variables will appear in both tables, while variables of other types will appear only
55  * in LocalVariableTable.
56  * <p>
57  * The LocalVariableTypeTable attribute has the following format:
58  * <pre>
59  * LocalVariableTypeTable_attribute {
60  * u2 attribute_name_index;
61  * u4 attribute_length;
62  * u2 local_variable_type_table_length;
63  * {
64  * u2 start_pc;
65  * u2 length;
66  * u2 name_index;
67  * u2 signature_index;
68  * u2 index;
69  * } local_variable_type_table[local_variable_type_table_length];
70  * }
71  * </pre>
72  *
73  * The items of the LocalVariableTypeTable_attribute structure are as follows:
74  * <dl>
75  * <dt>attribute_name_index</dt>
76  * <dd>The value of the attribute_name_index item must be a valid index
77  * into the constant_pool table a121. The constant_pool entry at that
78  * index must be a CONSTANT_Utf8_info (4.5.7) a122 structure
79  * representing the string "LocalVariableTypeTable" a123.</dd>
80  *
81  * <dt>attribute_length</dt>
82  * <dd>The value of the attribute_length item indicates the length of the
83  * attribute, excluding the initial six bytes.</dd>
84  *
85  * <dt>local_variable_table_length</dt>
86  * <dd>The value of the local_variable_table_length item indicates the
87  * number of entries in the local_variable_table array.</dd>
88  *
89  * <dt>local_variable_table[]</dd>
90  * <dd>Each entry in the local_variable_table array indicates a range of code
91  * array offsets within which a local variable has a value. It also
92  * indicates the index into the local variable array of the current
93  * frame at which that local variable can be found. Each entry must
94  * contain the following five items:</dd>
95  * <dl>
96  * <dt>start_pc, length</dt>
97  * <dd>The given local variable must have a value at indices into the
98  * code array in the interval [start_pc, start_pc+length), that is,
99  * between start_pc and start_pc+length exclusive. The value of
100  * start_pc must be a valid index into the code array of this Code
101  * attribute and must be the index of the opcode of an
102  * instructiona124. The value of start_pc+length must either be a
103  * valid index into the code array of this Code attribute and be
104  * the index of the opcode of an instruction, or it must be the
105  * first index beyond the end of that code array a125.</dd>
106  *
107  * <dt>name_index, signature_index</dt>
108  * <dd>The value of the name_index item must be a valid index into
109  * the constant_pool table a127. The constant_pool entry at that
110  * index must contain a CONSTANT_Utf8_info structure
111  * a128 representing a valid unqualified name denoting a local variablea128.
112  * Careful here - do we want any restrictions at all?</dd>
113  * <p>
114  * The value of the signature_index item must be a valid index
115  * into the constant_pool table. The constant_pool entry at that index
116  * must contain a CONSTANT_Utf8_info structure
117  * representing a field type signature encoding the type
118  * of a local variable in the source program.</dd>
119  *
120  * <dt>index</dt>
121  * <dd>The given local variable must be at index in the local variable
122  * array of the current frame. If the local variable at index is of
123  * type double or long, it occupies both index and index+1.</dd>
124  * </dl>
125  * </dl>
126  *
127  * @author Eugene Kuleshov
128  */

129 public class LocalVariableTypeTableAttribute extends Attribute {
130
131   protected List JavaDoc types = new ArrayList JavaDoc();
132   
133   
134   public LocalVariableTypeTableAttribute() {
135     super( "LocalVariableTypeTable");
136   }
137
138   public List JavaDoc getTypes() {
139     return types;
140   }
141   
142   protected Label[] getLabels() {
143     HashSet JavaDoc labels = new HashSet JavaDoc();
144     for (int i = 0; i < types.size(); i++) {
145       LocalVariableType t = (LocalVariableType)types.get(i);
146       labels.add( t.getStart());
147       labels.add( t.getEnd());
148     }
149     return ( Label[]) labels.toArray( new Label[ labels.size()]);
150   }
151   
152   protected Attribute read( ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
153     int localVariableTypeTableLength = cr.readUnsignedShort(off);
154     off += 2;
155     LocalVariableTypeTableAttribute attr = new LocalVariableTypeTableAttribute();
156     for( int i = 0; i < localVariableTypeTableLength; i++) {
157       LocalVariableType t = new LocalVariableType();
158       int start = cr.readUnsignedShort( off);
159       int length = cr.readUnsignedShort( off + 2);
160       t.start = getLabel( labels, start);
161       t.end = getLabel( labels, start + length);
162       t.name = cr.readUTF8( off + 4, buf);
163       t.signature = cr.readUTF8( off + 6, buf);
164       t.index = cr.readUnsignedShort( off + 8);
165       off += 10;
166       types.add(t);
167     }
168     return attr;
169   }
170
171   protected ByteVector write( ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
172     ByteVector bv = new ByteVector();
173     bv.putShort( types.size());
174     for( int i = 0; i < types.size(); i++) {
175       LocalVariableType t = ( LocalVariableType) types.get(i);
176       int startOffset = t.getStart().getOffset();
177       bv.putShort( startOffset);
178       bv.putShort( t.getEnd().getOffset()-startOffset);
179       bv.putUTF8( t.getName());
180       bv.putUTF8( t.getSignature());
181       bv.putShort( t.getIndex());
182     }
183     return bv;
184   }
185
186   private Label getLabel( Label[] labels, int offset) {
187     Label label = labels[ offset];
188     if( label==null) {
189       label = new Label();
190       labels[ offset] = label;
191     }
192     return label;
193   }
194
195   public String JavaDoc toString() {
196     StringBuffer JavaDoc sb = new StringBuffer JavaDoc("LocalVariableTypeTable[");
197     for (int i = 0; i < types.size(); i++) {
198       sb.append('\n').append('[').append(types.get(i)).append(']');
199     }
200     sb.append("\n]");
201     return sb.toString();
202   }
203   
204 }
205
206
Popular Tags