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 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 25 import com.sun.org.apache.bcel.internal.Constants; 26 import java.io.*; 27 28 /** 29 * This class represents colection of local variables in a 30 * method. This attribute is contained in the <em>Code</em> attribute. 31 * 32 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 33 * @see Code 34 * @see LocalVariable 35 */ 36 public class LocalVariableTable extends Attribute { 37 private int local_variable_table_length; // Table of local 38 private LocalVariable[] local_variable_table; // variables 39 40 /** 41 * Initialize from another object. Note that both objects use the same 42 * references (shallow copy). Use copy() for a physical copy. 43 */ 44 public LocalVariableTable(LocalVariableTable c) { 45 this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(), 46 c.getConstantPool()); 47 } 48 49 /** 50 * @param name_index Index in constant pool to `LocalVariableTable' 51 * @param length Content length in bytes 52 * @param local_variable_table Table of local variables 53 * @param constant_pool Array of constants 54 */ 55 public LocalVariableTable(int name_index, int length, 56 LocalVariable[] local_variable_table, 57 ConstantPool constant_pool) 58 { 59 super(Constants.ATTR_LOCAL_VARIABLE_TABLE, name_index, length, constant_pool); 60 setLocalVariableTable(local_variable_table); 61 } 62 63 /** 64 * Construct object from file stream. 65 * @param name_index Index in constant pool 66 * @param length Content length in bytes 67 * @param file Input stream 68 * @param constant_pool Array of constants 69 * @throws IOException 70 */ 71 LocalVariableTable(int name_index, int length, DataInputStream file, 72 ConstantPool constant_pool) throws IOException 73 { 74 this(name_index, length, (LocalVariable[])null, constant_pool); 75 76 local_variable_table_length = (file.readUnsignedShort()); 77 local_variable_table = new LocalVariable[local_variable_table_length]; 78 79 for(int i=0; i < local_variable_table_length; i++) 80 local_variable_table[i] = new LocalVariable(file, constant_pool); 81 } 82 83 /** 84 * Called by objects that are traversing the nodes of the tree implicitely 85 * defined by the contents of a Java class. I.e., the hierarchy of methods, 86 * fields, attributes, etc. spawns a tree of objects. 87 * 88 * @param v Visitor object 89 */ 90 public void accept(Visitor v) { 91 v.visitLocalVariableTable(this); 92 } 93 94 /** 95 * Dump local variable table attribute to file stream in binary format. 96 * 97 * @param file Output file stream 98 * @throws IOException 99 */ 100 public final void dump(DataOutputStream file) throws IOException 101 { 102 super.dump(file); 103 file.writeShort(local_variable_table_length); 104 for(int i=0; i < local_variable_table_length; i++) 105 local_variable_table[i].dump(file); 106 } 107 108 /** 109 * @return Array of local variables of method. 110 */ 111 public final LocalVariable[] getLocalVariableTable() { 112 return local_variable_table; 113 } 114 115 /** @return first matching variable using index 116 */ 117 public final LocalVariable getLocalVariable(int index) { 118 for(int i=0; i < local_variable_table_length; i++) 119 if(local_variable_table[i].getIndex() == index) 120 return local_variable_table[i]; 121 122 return null; 123 } 124 125 public final void setLocalVariableTable(LocalVariable[] local_variable_table) 126 { 127 this.local_variable_table = local_variable_table; 128 local_variable_table_length = (local_variable_table == null)? 0 : 129 local_variable_table.length; 130 } 131 132 /** 133 * @return String representation. 134 */ 135 public final String toString() { 136 StringBuffer buf = new StringBuffer(""); 137 138 for(int i=0; i < local_variable_table_length; i++) { 139 buf.append(local_variable_table[i].toString()); 140 141 if(i < local_variable_table_length - 1) 142 buf.append('\n'); 143 } 144 145 return buf.toString(); 146 } 147 148 /** 149 * @return deep copy of this attribute 150 */ 151 public Attribute copy(ConstantPool constant_pool) { 152 LocalVariableTable c = (LocalVariableTable)clone(); 153 154 c.local_variable_table = new LocalVariable[local_variable_table_length]; 155 for(int i=0; i < local_variable_table_length; i++) 156 c.local_variable_table[i] = local_variable_table[i].copy(); 157 158 c.constant_pool = constant_pool; 159 return c; 160 } 161 162 public final int getTableLength() { return local_variable_table_length; } 163 } | 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 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 import java.io.DataInput; 25 import java.io.DataOutputStream; 26 import java.io.IOException; 27 28 import com.sun.org.apache.bcel.internal.Const; 29 30 /** 31 * This class represents colection of local variables in a 32 * method. This attribute is contained in the <em>Code</em> attribute. 33 * 34 * @version $Id: LocalVariableTable.java 1749603 2016-06-21 20:50:19Z ggregory $ 35 * @see Code 36 * @see LocalVariable 37 */ 38 public class LocalVariableTable extends Attribute { 39 40 private LocalVariable[] local_variable_table; // variables 41 42 43 /** 44 * Initialize from another object. Note that both objects use the same 45 * references (shallow copy). Use copy() for a physical copy. 46 */ 47 public LocalVariableTable(final LocalVariableTable c) { 48 this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(), c.getConstantPool()); 49 } 50 51 52 /** 53 * @param name_index Index in constant pool to `LocalVariableTable' 54 * @param length Content length in bytes 55 * @param local_variable_table Table of local variables 56 * @param constant_pool Array of constants 57 */ 58 public LocalVariableTable(final int name_index, final int length, final LocalVariable[] local_variable_table, 59 final ConstantPool constant_pool) { 60 super(Const.ATTR_LOCAL_VARIABLE_TABLE, name_index, length, constant_pool); 61 this.local_variable_table = local_variable_table; 62 } 63 64 65 /** 66 * Construct object from input stream. 67 * @param name_index Index in constant pool 68 * @param length Content length in bytes 69 * @param input Input stream 70 * @param constant_pool Array of constants 71 * @throws IOException 72 */ 73 LocalVariableTable(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) 74 throws IOException { 75 this(name_index, length, (LocalVariable[]) null, constant_pool); 76 final int local_variable_table_length = input.readUnsignedShort(); 77 local_variable_table = new LocalVariable[local_variable_table_length]; 78 for (int i = 0; i < local_variable_table_length; i++) { 79 local_variable_table[i] = new LocalVariable(input, constant_pool); 80 } 81 } 82 83 84 /** 85 * Called by objects that are traversing the nodes of the tree implicitely 86 * defined by the contents of a Java class. I.e., the hierarchy of methods, 87 * fields, attributes, etc. spawns a tree of objects. 88 * 89 * @param v Visitor object 90 */ 91 @Override 92 public void accept( final Visitor v ) { 93 v.visitLocalVariableTable(this); 94 } 95 96 97 /** 98 * Dump local variable table attribute to file stream in binary format. 99 * 100 * @param file Output file stream 101 * @throws IOException 102 */ 103 @Override 104 public final void dump( final DataOutputStream file ) throws IOException { 105 super.dump(file); 106 file.writeShort(local_variable_table.length); 107 for (final LocalVariable variable : local_variable_table) { 108 variable.dump(file); 109 } 110 } 111 112 113 /** 114 * @return Array of local variables of method. 115 */ 116 public final LocalVariable[] getLocalVariableTable() { 117 return local_variable_table; 118 } 119 120 121 /** 122 * 123 * @param index the variable slot 124 * 125 * @return the first LocalVariable that matches the slot or null if not found 126 * 127 * @deprecated since 5.2 because multiple variables can share the 128 * same slot, use getLocalVariable(int index, int pc) instead. 129 */ 130 @java.lang.Deprecated 131 public final LocalVariable getLocalVariable( final int index ) { 132 for (final LocalVariable variable : local_variable_table) { 133 if (variable.getIndex() == index) { 134 return variable; 135 } 136 } 137 return null; 138 } 139 140 141 /** 142 * 143 * @param index the variable slot 144 * @param pc the current pc that this variable is alive 145 * 146 * @return the LocalVariable that matches or null if not found 147 */ 148 public final LocalVariable getLocalVariable( final int index, final int pc ) { 149 for (final LocalVariable variable : local_variable_table) { 150 if (variable.getIndex() == index) { 151 final int start_pc = variable.getStartPC(); 152 final int end_pc = start_pc + variable.getLength(); 153 if ((pc >= start_pc) && (pc <= end_pc)) { 154 return variable; 155 } 156 } 157 } 158 return null; 159 } 160 161 162 public final void setLocalVariableTable( final LocalVariable[] local_variable_table ) { 163 this.local_variable_table = local_variable_table; 164 } 165 166 167 /** 168 * @return String representation. 169 */ 170 @Override 171 public final String toString() { 172 final StringBuilder buf = new StringBuilder(); 173 for (int i = 0; i < local_variable_table.length; i++) { 174 buf.append(local_variable_table[i]); 175 if (i < local_variable_table.length - 1) { 176 buf.append('\n'); 177 } 178 } 179 return buf.toString(); 180 } 181 182 183 /** 184 * @return deep copy of this attribute 185 */ 186 @Override 187 public Attribute copy( final ConstantPool _constant_pool ) { 188 final LocalVariableTable c = (LocalVariableTable) clone(); 189 c.local_variable_table = new LocalVariable[local_variable_table.length]; 190 for (int i = 0; i < local_variable_table.length; i++) { 191 c.local_variable_table[i] = local_variable_table[i].copy(); 192 } 193 c.setConstantPool(_constant_pool); 194 return c; 195 } 196 197 198 public final int getTableLength() { 199 return local_variable_table == null ? 0 : local_variable_table.length; 200 } 201 } |