1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 package com.sun.org.apache.bcel.internal.classfile;
22 
23 import java.io.DataInput;
24 import java.io.DataOutputStream;
25 import java.io.IOException;
26 import com.sun.org.apache.bcel.internal.Const;
27 
28 // The new table is used when generic types are about...
29 //LocalVariableTable_attribute {
30 //       u2 attribute_name_index;
31 //       u4 attribute_length;
32 //       u2 local_variable_table_length;
33 //       {  u2 start_pc;
34 //          u2 length;
35 //          u2 name_index;
36 //          u2 descriptor_index;
37 //          u2 index;
38 //       } local_variable_table[local_variable_table_length];
39 //     }
40 //LocalVariableTypeTable_attribute {
41 //    u2 attribute_name_index;
42 //    u4 attribute_length;
43 //    u2 local_variable_type_table_length;
44 //    {
45 //      u2 start_pc;
46 //      u2 length;
47 //      u2 name_index;
48 //      u2 signature_index;
49 //      u2 index;
50 //    } local_variable_type_table[local_variable_type_table_length];
51 //  }
52 // J5TODO: Needs some testing !
53 /**
54  * @since 6.0
55  */
56 public class LocalVariableTypeTable extends Attribute {
57 
58     private LocalVariable[] local_variable_type_table;        // variables
59 
LocalVariableTypeTable(final LocalVariableTypeTable c)60     public LocalVariableTypeTable(final LocalVariableTypeTable c) {
61         this(c.getNameIndex(), c.getLength(), c.getLocalVariableTypeTable(), c.getConstantPool());
62     }
63 
LocalVariableTypeTable(final int name_index, final int length, final LocalVariable[] local_variable_table, final ConstantPool constant_pool)64     public LocalVariableTypeTable(final int name_index, final int length,
65             final LocalVariable[] local_variable_table, final ConstantPool constant_pool) {
66         super(Const.ATTR_LOCAL_VARIABLE_TYPE_TABLE, name_index, length, constant_pool);
67         this.local_variable_type_table = local_variable_table;
68     }
69 
LocalVariableTypeTable(final int nameIdx, final int len, final DataInput input, final ConstantPool cpool)70     LocalVariableTypeTable(final int nameIdx, final int len, final DataInput input,
71             final ConstantPool cpool) throws IOException {
72         this(nameIdx, len, (LocalVariable[]) null, cpool);
73 
74         final int local_variable_type_table_length = input.readUnsignedShort();
75         local_variable_type_table = new LocalVariable[local_variable_type_table_length];
76 
77         for (int i = 0; i < local_variable_type_table_length; i++) {
78             local_variable_type_table[i] = new LocalVariable(input, cpool);
79         }
80     }
81 
82     @Override
accept(final Visitor v)83     public void accept(final Visitor v) {
84         v.visitLocalVariableTypeTable(this);
85     }
86 
87     @Override
dump(final DataOutputStream file)88     public final void dump(final DataOutputStream file) throws IOException {
89         super.dump(file);
90         file.writeShort(local_variable_type_table.length);
91         for (final LocalVariable variable : local_variable_type_table) {
92             variable.dump(file);
93         }
94     }
95 
getLocalVariableTypeTable()96     public final LocalVariable[] getLocalVariableTypeTable() {
97         return local_variable_type_table;
98     }
99 
getLocalVariable(final int index)100     public final LocalVariable getLocalVariable(final int index) {
101         for (final LocalVariable variable : local_variable_type_table) {
102             if (variable.getIndex() == index) {
103                 return variable;
104             }
105         }
106 
107         return null;
108     }
109 
setLocalVariableTable(final LocalVariable[] local_variable_table)110     public final void setLocalVariableTable(final LocalVariable[] local_variable_table) {
111         this.local_variable_type_table = local_variable_table;
112     }
113 
114     /**
115      * @return String representation.
116      */
117     @Override
toString()118     public final String toString() {
119         final StringBuilder buf = new StringBuilder();
120 
121         for (int i = 0; i < local_variable_type_table.length; i++) {
122             buf.append(local_variable_type_table[i].toStringShared(true));
123 
124             if (i < local_variable_type_table.length - 1) {
125                 buf.append('\n');
126             }
127         }
128 
129         return buf.toString();
130     }
131 
132     /**
133      * @return deep copy of this attribute
134      */
135     @Override
copy(final ConstantPool constant_pool)136     public Attribute copy(final ConstantPool constant_pool) {
137         final LocalVariableTypeTable c = (LocalVariableTypeTable) clone();
138 
139         c.local_variable_type_table = new LocalVariable[local_variable_type_table.length];
140         for (int i = 0; i < local_variable_type_table.length; i++) {
141             c.local_variable_type_table[i] = local_variable_type_table[i].copy();
142         }
143 
144         c.setConstantPool(constant_pool);
145         return c;
146     }
147 
getTableLength()148     public final int getTableLength() {
149         return local_variable_type_table == null ? 0 : local_variable_type_table.length;
150     }
151 }
152