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 package com.sun.org.apache.bcel.internal.generic; 22 23 import com.sun.org.apache.bcel.internal.Const; 24 import com.sun.org.apache.bcel.internal.classfile.LocalVariable; 25 26 /** 27 * This class represents a local variable within a method. It contains its 28 * scope, name and type. The generated LocalVariable object can be obtained 29 * with getLocalVariable which needs the instruction list and the constant 30 * pool as parameters. 31 * 32 * @version $Id$ 33 * @see LocalVariable 34 * @see MethodGen 35 */ 36 public class LocalVariableGen implements InstructionTargeter, NamedAndTyped, Cloneable { 37 38 private int index; 39 private String name; 40 private Type type; 41 private InstructionHandle start; 42 private InstructionHandle end; 43 private int orig_index; // never changes; used to match up with LocalVariableTypeTable entries 44 private boolean live_to_end; 45 46 47 /** 48 * Generate a local variable that with index `index'. Note that double and long 49 * variables need two indexs. Index indices have to be provided by the user. 50 * 51 * @param index index of local variable 52 * @param name its name 53 * @param type its type 54 * @param start from where the instruction is valid (null means from the start) 55 * @param end until where the instruction is valid (null means to the end) 56 */ 57 public LocalVariableGen(final int index, final String name, final Type type, final InstructionHandle start, 58 final InstructionHandle end) { 59 if ((index < 0) || (index > Const.MAX_SHORT)) { 60 throw new ClassGenException("Invalid index index: " + index); 61 } 62 this.name = name; 63 this.type = type; 64 this.index = index; 65 setStart(start); 66 setEnd(end); 67 this.orig_index = index; 68 this.live_to_end = end == null; 69 } 70 71 72 /** 73 * Generate a local variable that with index `index'. Note that double and long 74 * variables need two indexs. Index indices have to be provided by the user. 75 * 76 * @param index index of local variable 77 * @param name its name 78 * @param type its type 79 * @param start from where the instruction is valid (null means from the start) 80 * @param end until where the instruction is valid (null means to the end) 81 * @param orig_index index of local variable prior to any changes to index 82 */ 83 public LocalVariableGen(final int index, final String name, final Type type, final InstructionHandle start, 84 final InstructionHandle end, final int orig_index) { 85 this(index, name, type, start, end); 86 this.orig_index = orig_index; 87 } 88 89 90 /** 91 * Get LocalVariable object. 92 * 93 * This relies on that the instruction list has already been dumped to byte code or 94 * or that the `setPositions' methods has been called for the instruction list. 95 * 96 * Note that due to the conversion from byte code offset to InstructionHandle, 97 * it is impossible to tell the difference between a live range that ends BEFORE 98 * the last insturction of the method or a live range that ends AFTER the last 99 * instruction of the method. Hence the live_to_end flag to differentiate 100 * between these two cases. 101 * 102 * @param cp constant pool 103 */ 104 public LocalVariable getLocalVariable( final ConstantPoolGen cp ) { 105 int start_pc = 0; 106 int length = 0; 107 if ((start != null) && (end != null)) { 108 start_pc = start.getPosition(); 109 length = end.getPosition() - start_pc; 110 if ((end.getNext() == null) && live_to_end) { 111 length += end.getInstruction().getLength(); | 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 package com.sun.org.apache.bcel.internal.generic; 22 23 import com.sun.org.apache.bcel.internal.Const; 24 import com.sun.org.apache.bcel.internal.classfile.LocalVariable; 25 26 /** 27 * Represents a local variable within a method. It contains its 28 * scope, name and type. The generated LocalVariable object can be obtained 29 * with getLocalVariable which needs the instruction list and the constant 30 * pool as parameters. 31 * 32 * @see LocalVariable 33 * @see MethodGen 34 */ 35 public class LocalVariableGen implements InstructionTargeter, NamedAndTyped, Cloneable { 36 37 private int index; 38 private String name; 39 private Type type; 40 private InstructionHandle start; 41 private InstructionHandle end; 42 private int orig_index; // never changes; used to match up with LocalVariableTypeTable entries 43 private boolean live_to_end; 44 45 46 /** 47 * Generate a local variable that with index `index'. Note that double and long 48 * variables need two indexs. Index indices have to be provided by the user. 49 * 50 * @param index index of local variable 51 * @param name its name 52 * @param type its type 53 * @param start from where the instruction is valid (null means from the start) 54 * @param end until where the instruction is valid (null means to the end) 55 */ 56 public LocalVariableGen(final int index, final String name, final Type type, final InstructionHandle start, 57 final InstructionHandle end) { 58 if ((index < 0) || (index > Const.MAX_SHORT)) { 59 throw new ClassGenException("Invalid index index: " + index); 60 } 61 this.name = name; 62 this.type = type; 63 this.index = index; 64 setStart(start); 65 setEnd(end); 66 this.orig_index = index; 67 this.live_to_end = end == null; 68 } 69 70 71 /** 72 * Generates a local variable that with index `index'. Note that double and long 73 * variables need two indexs. Index indices have to be provided by the user. 74 * 75 * @param index index of local variable 76 * @param name its name 77 * @param type its type 78 * @param start from where the instruction is valid (null means from the start) 79 * @param end until where the instruction is valid (null means to the end) 80 * @param orig_index index of local variable prior to any changes to index 81 */ 82 public LocalVariableGen(final int index, final String name, final Type type, final InstructionHandle start, 83 final InstructionHandle end, final int orig_index) { 84 this(index, name, type, start, end); 85 this.orig_index = orig_index; 86 } 87 88 89 /** 90 * Gets LocalVariable object. 91 * 92 * This relies on that the instruction list has already been dumped to byte code or 93 * or that the `setPositions' methods has been called for the instruction list. 94 * 95 * Note that due to the conversion from byte code offset to InstructionHandle, 96 * it is impossible to tell the difference between a live range that ends BEFORE 97 * the last insturction of the method or a live range that ends AFTER the last 98 * instruction of the method. Hence the live_to_end flag to differentiate 99 * between these two cases. 100 * 101 * @param cp constant pool 102 */ 103 public LocalVariable getLocalVariable( final ConstantPoolGen cp ) { 104 int start_pc = 0; 105 int length = 0; 106 if ((start != null) && (end != null)) { 107 start_pc = start.getPosition(); 108 length = end.getPosition() - start_pc; 109 if ((end.getNext() == null) && live_to_end) { 110 length += end.getInstruction().getLength(); |