1 17 package org.apache.bcel.classfile; 18 19 import java.io.DataInputStream ; 20 import java.io.DataOutputStream ; 21 import java.io.IOException ; 22 import org.apache.bcel.Constants; 23 24 33 public class LocalVariableTable extends Attribute { 34 35 private int local_variable_table_length; private LocalVariable[] local_variable_table; 38 39 43 public LocalVariableTable(LocalVariableTable c) { 44 this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(), c.getConstantPool()); 45 } 46 47 48 54 public LocalVariableTable(int name_index, int length, LocalVariable[] local_variable_table, 55 ConstantPool constant_pool) { 56 super(Constants.ATTR_LOCAL_VARIABLE_TABLE, name_index, length, constant_pool); 57 setLocalVariableTable(local_variable_table); 58 } 59 60 61 69 LocalVariableTable(int name_index, int length, DataInputStream file, ConstantPool constant_pool) 70 throws IOException { 71 this(name_index, length, (LocalVariable[]) null, constant_pool); 72 local_variable_table_length = (file.readUnsignedShort()); 73 local_variable_table = new LocalVariable[local_variable_table_length]; 74 for (int i = 0; i < local_variable_table_length; i++) { 75 local_variable_table[i] = new LocalVariable(file, constant_pool); 76 } 77 } 78 79 80 87 public void accept( Visitor v ) { 88 v.visitLocalVariableTable(this); 89 } 90 91 92 98 public final void dump( DataOutputStream file ) throws IOException { 99 super.dump(file); 100 file.writeShort(local_variable_table_length); 101 for (int i = 0; i < local_variable_table_length; i++) { 102 local_variable_table[i].dump(file); 103 } 104 } 105 106 107 110 public final LocalVariable[] getLocalVariableTable() { 111 return local_variable_table; 112 } 113 114 115 125 public final LocalVariable getLocalVariable( int index ) { 126 for (int i = 0; i < local_variable_table_length; i++) { 127 if (local_variable_table[i].getIndex() == index) { 128 return local_variable_table[i]; 129 } 130 } 131 return null; 132 } 133 134 135 143 public final LocalVariable getLocalVariable( int index, int pc ) { 144 for (int i = 0; i < local_variable_table_length; i++) { 145 if (local_variable_table[i].getIndex() == index) { 146 int start_pc = local_variable_table[i].getStartPC(); 147 int end_pc = start_pc + local_variable_table[i].getLength(); 148 if ((pc >= start_pc) && (pc < end_pc)) { 149 return local_variable_table[i]; 150 } 151 } 152 } 153 return null; 154 } 155 156 157 public final void setLocalVariableTable( LocalVariable[] local_variable_table ) { 158 this.local_variable_table = local_variable_table; 159 local_variable_table_length = (local_variable_table == null) 160 ? 0 161 : local_variable_table.length; 162 } 163 164 165 168 public final String toString() { 169 StringBuffer buf = new StringBuffer (""); 170 for (int i = 0; i < local_variable_table_length; i++) { 171 buf.append(local_variable_table[i].toString()); 172 if (i < local_variable_table_length - 1) { 173 buf.append('\n'); 174 } 175 } 176 return buf.toString(); 177 } 178 179 180 183 public Attribute copy( ConstantPool _constant_pool ) { 184 LocalVariableTable c = (LocalVariableTable) clone(); 185 c.local_variable_table = new LocalVariable[local_variable_table_length]; 186 for (int i = 0; i < local_variable_table_length; i++) { 187 c.local_variable_table[i] = local_variable_table[i].copy(); 188 } 189 c.constant_pool = _constant_pool; 190 return c; 191 } 192 193 194 public final int getTableLength() { 195 return local_variable_table_length; 196 } 197 } 198 | Popular Tags |