1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 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 a local variable within a method. It contains its 30 * scope, name, signature and index on the method's frame. 31 * 32 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 33 * @see LocalVariableTable 34 */ 35 public final class LocalVariable 36 implements Constants, Cloneable, Node, Serializable 37 { 38 private int start_pc; // Range in which the variable is valid 39 private int length; 40 private int name_index; // Index in constant pool of variable name 41 private int signature_index; // Index of variable signature 42 private int index; /* Variable is `index'th local variable on 43 * this method's frame. 44 */ 45 46 private ConstantPool constant_pool; 47 48 /** 49 * Initialize from another object. Note that both objects use the same 50 * references (shallow copy). Use copy() for a physical copy. 51 */ 52 public LocalVariable(LocalVariable c) { 53 this(c.getStartPC(), c.getLength(), c.getNameIndex(), 54 c.getSignatureIndex(), c.getIndex(), c.getConstantPool()); 55 } 56 57 /** 58 * Construct object from file stream. 59 * @param file Input stream 60 * @throws IOException 61 */ 62 LocalVariable(DataInputStream file, ConstantPool constant_pool) 63 throws IOException 64 { 65 this(file.readUnsignedShort(), file.readUnsignedShort(), 66 file.readUnsignedShort(), file.readUnsignedShort(), 67 file.readUnsignedShort(), constant_pool); 68 } 69 70 /** 71 * @param start_pc Range in which the variable 72 * @param length ... is valid 73 * @param name_index Index in constant pool of variable name 74 * @param signature_index Index of variable's signature 75 * @param index Variable is `index'th local variable on the method's frame 76 * @param constant_pool Array of constants 77 */ 78 public LocalVariable(int start_pc, int length, int name_index, 79 int signature_index, int index, 80 ConstantPool constant_pool) 81 { 82 this.start_pc = start_pc; 83 this.length = length; 84 this.name_index = name_index; 85 this.signature_index = signature_index; 86 this.index = index; 87 this.constant_pool = constant_pool; 88 } 89 90 /** 91 * Called by objects that are traversing the nodes of the tree implicitely 92 * defined by the contents of a Java class. I.e., the hierarchy of methods, 93 * fields, attributes, etc. spawns a tree of objects. 94 * 95 * @param v Visitor object 96 */ 97 public void accept(Visitor v) { 98 v.visitLocalVariable(this); 99 } 100 101 /** 102 * Dump local variable to file stream in binary format. 103 * 104 * @param file Output file stream 105 * @throws IOException 106 */ 107 public final void dump(DataOutputStream file) throws IOException 108 { 109 file.writeShort(start_pc); 110 file.writeShort(length); 111 file.writeShort(name_index); 112 file.writeShort(signature_index); 113 file.writeShort(index); 114 } 115 116 /** 117 * @return Constant pool used by this object. 118 */ 119 public final ConstantPool getConstantPool() { return constant_pool; } 120 121 /** 122 * @return Variable is valid within getStartPC() .. getStartPC()+getLength() 123 */ 124 public final int getLength() { return length; } 125 126 /** 127 * @return Variable name. 128 */ 129 public final String getName() { 130 ConstantUtf8 c; 131 132 c = (ConstantUtf8)constant_pool.getConstant(name_index, CONSTANT_Utf8); 133 return c.getBytes(); 134 } 135 136 /** 137 * @return Index in constant pool of variable name. 138 */ 139 public final int getNameIndex() { return name_index; } 140 141 /** 142 * @return Signature. 143 */ 144 public final String getSignature() { 145 ConstantUtf8 c; 146 c = (ConstantUtf8)constant_pool.getConstant(signature_index, 147 CONSTANT_Utf8); 148 return c.getBytes(); 149 } 150 151 /** 152 * @return Index in constant pool of variable signature. 153 */ 154 public final int getSignatureIndex() { return signature_index; } 155 156 /** 157 * @return index of register where variable is stored 158 */ 159 public final int getIndex() { return index; } 160 161 /** 162 * @return Start of range where he variable is valid 163 */ 164 public final int getStartPC() { return start_pc; } 165 166 /** 167 * @param constant_pool Constant pool to be used for this object. 168 */ 169 public final void setConstantPool(ConstantPool constant_pool) { 170 this.constant_pool = constant_pool; 171 } 172 173 /** 174 * @param length. 175 */ 176 public final void setLength(int length) { 177 this.length = length; 178 } 179 180 /** 181 * @param name_index. 182 */ 183 public final void setNameIndex(int name_index) { 184 this.name_index = name_index; 185 } 186 187 /** 188 * @param signature_index. 189 */ 190 public final void setSignatureIndex(int signature_index) { 191 this.signature_index = signature_index; 192 } 193 194 /** 195 * @param index. 196 */ 197 public final void setIndex(int index) { this.index = index; } 198 199 /** 200 * @param start_pc Specify range where the local variable is valid. 201 */ 202 public final void setStartPC(int start_pc) { 203 this.start_pc = start_pc; 204 } 205 206 /** 207 * @return string representation. 208 */ 209 public final String toString() { 210 String name = getName(), signature = Utility.signatureToString(getSignature()); 211 212 return "LocalVariable(start_pc = " + start_pc + ", length = " + length + 213 ", index = " + index + ":" + signature + " " + name + ")"; 214 } 215 216 /** 217 * @return deep copy of this object 218 */ 219 public LocalVariable copy() { 220 try { 221 return (LocalVariable)clone(); 222 } catch(CloneNotSupportedException e) {} 223 224 return null; 225 } 226 }