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