1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 package com.sun.org.apache.bcel.internal.generic; 6 7 /* ==================================================================== 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2001 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, 26 * if any, must include the following acknowledgment: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowledgment may appear in the software itself, 30 * if and wherever such third-party acknowledgments normally appear. 31 * 32 * 4. The names "Apache" and "Apache Software Foundation" and 33 * "Apache BCEL" must not be used to endorse or promote products 34 * derived from this software without prior written permission. For 35 * written permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache", 38 * "Apache BCEL", nor may "Apache" appear in their name, without 39 * prior written permission of the Apache Software Foundation. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 */ 60 61 import com.sun.org.apache.bcel.internal.Constants; 62 import com.sun.org.apache.bcel.internal.classfile.*; 63 64 /** 65 * This class represents a local variable within a method. It contains its 66 * scope, name and type. The generated LocalVariable object can be obtained 67 * with getLocalVariable which needs the instruction list and the constant 68 * pool as parameters. 69 * 70 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 71 * @see LocalVariable 72 * @see MethodGen 73 */ 74 public class LocalVariableGen 75 implements InstructionTargeter, NamedAndTyped, Cloneable, 76 java.io.Serializable 77 { 78 private int index; 79 private String name; 80 private Type type; 81 private InstructionHandle start, end; 82 83 /** 84 * Generate a local variable that with index `index'. Note that double and long 85 * variables need two indexs. Index indices have to be provided by the user. 86 * 87 * @param index index of local variable 88 * @param name its name 89 * @param type its type 90 * @param start from where the instruction is valid (null means from the start) 91 * @param end until where the instruction is valid (null means to the end) 92 */ 93 public LocalVariableGen(int index, String name, Type type, 94 InstructionHandle start, InstructionHandle end) { 95 if((index < 0) || (index > Constants.MAX_SHORT)) 96 throw new ClassGenException("Invalid index index: " + index); 97 98 this.name = name; 99 this.type = type; 100 this.index = index; 101 setStart(start); 102 setEnd(end); 103 } 104 105 /** 106 * Get LocalVariable object. 107 * 108 * This relies on that the instruction list has already been dumped to byte code or 109 * or that the `setPositions' methods has been called for the instruction list. 110 * 111 * Note that for local variables whose scope end at the last 112 * instruction of the method's code, the JVM specification is ambiguous: 113 * both a start_pc+length ending at the last instruction and 114 * start_pc+length ending at first index beyond the end of the code are 115 * valid. 116 * 117 * @param il instruction list (byte code) which this variable belongs to 118 * @param cp constant pool 119 */ 120 public LocalVariable getLocalVariable(ConstantPoolGen cp) { 121 int start_pc = start.getPosition(); 122 int length = end.getPosition() - start_pc; 123 124 if(length > 0) 125 length += end.getInstruction().getLength(); 126 127 int name_index = cp.addUtf8(name); 128 int signature_index = cp.addUtf8(type.getSignature()); 129 130 return new LocalVariable(start_pc, length, name_index, 131 signature_index, index, cp.getConstantPool()); 132 } 133 134 public void setIndex(int index) { this.index = index; } 135 public int getIndex() { return index; } 136 public void setName(String name) { this.name = name; } 137 public String getName() { return name; } 138 public void setType(Type type) { this.type = type; } 139 public Type getType() { return type; } 140 141 public InstructionHandle getStart() { return start; } 142 public InstructionHandle getEnd() { return end; } 143 144 public void setStart(InstructionHandle start) { 145 BranchInstruction.notifyTarget(this.start, start, this); 146 this.start = start; 147 } 148 149 public void setEnd(InstructionHandle end) { 150 BranchInstruction.notifyTarget(this.end, end, this); 151 this.end = end; 152 } 153 154 /** 155 * @param old_ih old target, either start or end 156 * @param new_ih new target 157 */ 158 public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { 159 boolean targeted = false; 160 161 if(start == old_ih) { 162 targeted = true; 163 setStart(new_ih); 164 } 165 166 if(end == old_ih) { 167 targeted = true; 168 setEnd(new_ih); 169 } 170 171 if(!targeted) 172 throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " + 173 end + "}"); 174 } 175 176 /** 177 * @return true, if ih is target of this variable 178 */ 179 public boolean containsTarget(InstructionHandle ih) { 180 return (start == ih) || (end == ih); 181 } 182 183 /** 184 * We consider to local variables to be equal, if the use the same index and 185 * are valid in the same range. 186 */ 187 public boolean equals(Object o) { 188 if(!(o instanceof LocalVariableGen)) 189 return false; 190 191 LocalVariableGen l = (LocalVariableGen)o; 192 return (l.index == index) && (l.start == start) && (l.end == end); 193 } 194 195 public String toString() { 196 return "LocalVariableGen(" + name + ", " + type + ", " + start + ", " + end + ")"; 197 } 198 199 public Object clone() { 200 try { 201 return super.clone(); 202 } catch(CloneNotSupportedException e) { 203 System.err.println(e); 204 return null; 205 } 206 } 207 }