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