001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.bcel.classfile; 019 020import java.io.DataInput; 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024import org.apache.bcel.Const; 025import org.apache.bcel.Constants; 026 027/** 028 * This class represents a local variable within a method. It contains its 029 * scope, name, signature and index on the method's frame. 030 * 031 * @version $Id: LocalVariable.java 1749603 2016-06-21 20:50:19Z ggregory $ 032 * @see LocalVariableTable 033 */ 034public final class LocalVariable implements Cloneable, Node, Constants { 035 036 private int start_pc; // Range in which the variable is valid 037 private int length; 038 private int name_index; // Index in constant pool of variable name 039 private int signature_index; // Index of variable signature 040 private int index; /* Variable is `index'th local variable on 041 * this method's frame. 042 */ 043 private ConstantPool constant_pool; 044 045 046 /** 047 * Initialize from another object. Note that both objects use the same 048 * references (shallow copy). Use copy() for a physical copy. 049 */ 050 public LocalVariable(final LocalVariable c) { 051 this(c.getStartPC(), c.getLength(), c.getNameIndex(), c.getSignatureIndex(), c.getIndex(), 052 c.getConstantPool()); 053 } 054 055 056 /** 057 * Construct object from file stream. 058 * @param file Input stream 059 * @throws IOException 060 */ 061 LocalVariable(final DataInput file, final ConstantPool constant_pool) throws IOException { 062 this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file 063 .readUnsignedShort(), file.readUnsignedShort(), constant_pool); 064 } 065 066 067 /** 068 * @param start_pc Range in which the variable 069 * @param length ... is valid 070 * @param name_index Index in constant pool of variable name 071 * @param signature_index Index of variable's signature 072 * @param index Variable is `index'th local variable on the method's frame 073 * @param constant_pool Array of constants 074 */ 075 public LocalVariable(final int start_pc, final int length, final int name_index, final int signature_index, final int index, 076 final ConstantPool constant_pool) { 077 this.start_pc = start_pc; 078 this.length = length; 079 this.name_index = name_index; 080 this.signature_index = signature_index; 081 this.index = index; 082 this.constant_pool = constant_pool; 083 } 084 085 086 /** 087 * Called by objects that are traversing the nodes of the tree implicitely 088 * defined by the contents of a Java class. I.e., the hierarchy of methods, 089 * fields, attributes, etc. spawns a tree of objects. 090 * 091 * @param v Visitor object 092 */ 093 @Override 094 public void accept( final Visitor v ) { 095 v.visitLocalVariable(this); 096 } 097 098 099 /** 100 * Dump local variable to file stream in binary format. 101 * 102 * @param file Output file stream 103 * @throws IOException 104 */ 105 public final void dump( final DataOutputStream file ) throws IOException { 106 file.writeShort(start_pc); 107 file.writeShort(length); 108 file.writeShort(name_index); 109 file.writeShort(signature_index); 110 file.writeShort(index); 111 } 112 113 114 /** 115 * @return Constant pool used by this object. 116 */ 117 public final ConstantPool getConstantPool() { 118 return constant_pool; 119 } 120 121 122 /** 123 * @return Variable is valid within getStartPC() .. getStartPC()+getLength() 124 */ 125 public final int getLength() { 126 return length; 127 } 128 129 130 /** 131 * @return Variable name. 132 */ 133 public final String getName() { 134 ConstantUtf8 c; 135 c = (ConstantUtf8) constant_pool.getConstant(name_index, Const.CONSTANT_Utf8); 136 return c.getBytes(); 137 } 138 139 140 /** 141 * @return Index in constant pool of variable name. 142 */ 143 public final int getNameIndex() { 144 return name_index; 145 } 146 147 148 /** 149 * @return Signature. 150 */ 151 public final String getSignature() { 152 ConstantUtf8 c; 153 c = (ConstantUtf8) constant_pool.getConstant(signature_index, Const.CONSTANT_Utf8); 154 return c.getBytes(); 155 } 156 157 158 /** 159 * @return Index in constant pool of variable signature. 160 */ 161 public final int getSignatureIndex() { 162 return signature_index; 163 } 164 165 166 /** 167 * @return index of register where variable is stored 168 */ 169 public final int getIndex() { 170 return index; 171 } 172 173 174 /** 175 * @return Start of range where he variable is valid 176 */ 177 public final int getStartPC() { 178 return start_pc; 179 } 180 181 182 /* 183 * Helper method shared with LocalVariableTypeTable 184 */ 185 final String toStringShared( final boolean typeTable ) { 186 final String name = getName(); 187 final String signature = Utility.signatureToString(getSignature(), false); 188 final String label = "LocalVariable" + (typeTable ? "Types" : "" ); 189 return label + "(start_pc = " + start_pc + ", length = " + length + ", index = " 190 + index + ":" + signature + " " + name + ")"; 191 } 192 193 194 /** 195 * @param constant_pool Constant pool to be used for this object. 196 */ 197 public final void setConstantPool( final ConstantPool constant_pool ) { 198 this.constant_pool = constant_pool; 199 } 200 201 202 /** 203 * @param length the length of this local variable 204 */ 205 public final void setLength( final int length ) { 206 this.length = length; 207 } 208 209 210 /** 211 * @param name_index the index into the constant pool for the name of this variable 212 */ 213 public final void setNameIndex( final int name_index ) { // TODO unused 214 this.name_index = name_index; 215 } 216 217 218 /** 219 * @param signature_index the index into the constant pool for the signature of this variable 220 */ 221 public final void setSignatureIndex( final int signature_index ) { // TODO unused 222 this.signature_index = signature_index; 223 } 224 225 226 /** 227 * @param index the index in the local variable table of this variable 228 */ 229 public final void setIndex( final int index ) { // TODO unused 230 this.index = index; 231 } 232 233 234 /** 235 * @param start_pc Specify range where the local variable is valid. 236 */ 237 public final void setStartPC( final int start_pc ) { // TODO unused 238 this.start_pc = start_pc; 239 } 240 241 242 /** 243 * @return string representation. 244 */ 245 @Override 246 public final String toString() { 247 return toStringShared(false); 248 } 249 250 251 /** 252 * @return deep copy of this object 253 */ 254 public LocalVariable copy() { 255 try { 256 return (LocalVariable) clone(); 257 } catch (final CloneNotSupportedException e) { 258 // TODO should this throw? 259 } 260 return null; 261 } 262}