KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > generic > LocalVariableGen


1 package com.sun.org.apache.bcel.internal.generic;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import com.sun.org.apache.bcel.internal.Constants;
58 import com.sun.org.apache.bcel.internal.classfile.*;
59
60 /**
61  * This class represents a local variable within a method. It contains its
62  * scope, name and type. The generated LocalVariable object can be obtained
63  * with getLocalVariable which needs the instruction list and the constant
64  * pool as parameters.
65  *
66  * @version $Id: LocalVariableGen.java,v 1.1.1.1 2001/10/29 20:00:23 jvanzyl Exp $
67  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
68  * @see LocalVariable
69  * @see MethodGen
70  */

71 public class LocalVariableGen implements InstructionTargeter, NamedAndTyped, Cloneable JavaDoc {
72   private int index;
73   private String JavaDoc name;
74   private Type type;
75   private InstructionHandle start, end;
76
77   /**
78    * Generate a local variable that with index `index'. Note that double and long
79    * variables need two indexs. Index indices have to be provided by the user.
80    *
81    * @param index index of local variable
82    * @param name its name
83    * @param type its type
84    * @param start from where the instruction is valid (null means from the start)
85    * @param end until where the instruction is valid (null means to the end)
86    */

87   public LocalVariableGen(int index, String JavaDoc name, Type type,
88               InstructionHandle start, InstructionHandle end) {
89     if((index < 0) || (index > Constants.MAX_SHORT))
90       throw new ClassGenException("Invalid index index: " + index);
91     
92     this.name = name;
93     this.type = type;
94     this.index = index;
95     setStart(start);
96     setEnd(end);
97   }
98
99   /**
100    * Get LocalVariable object.
101    *
102    * This relies on that the instruction list has already been dumped to byte code or
103    * or that the `setPositions' methods has been called for the instruction list.
104    *
105    * @param il instruction list (byte code) which this variable belongs to
106    * @param cp constant pool
107    */

108   public LocalVariable getLocalVariable(ConstantPoolGen cp) {
109     int start_pc = start.getPosition();
110     int length = end.getPosition() - start_pc;
111     int name_index = cp.addUtf8(name);
112     int signature_index = cp.addUtf8(type.getSignature());
113
114     return new LocalVariable(start_pc, length, name_index,
115                  signature_index, index, cp.getConstantPool());
116   }
117
118   public void setIndex(int index) { this.index = index; }
119   public int getIndex() { return index; }
120   public void setName(String JavaDoc name) { this.name = name; }
121   public String JavaDoc getName() { return name; }
122   public void setType(Type type) { this.type = type; }
123   public Type getType() { return type; }
124
125   public InstructionHandle getStart() { return start; }
126   public InstructionHandle getEnd() { return end; }
127
128   public void setStart(InstructionHandle start) {
129     BranchInstruction.notifyTarget(this.start, start, this);
130     this.start = start;
131   }
132
133   public void setEnd(InstructionHandle end) {
134     BranchInstruction.notifyTarget(this.end, end, this);
135     this.end = end;
136   }
137
138   /**
139    * @param old_ih old target, either start or end
140    * @param new_ih new target
141    */

142   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
143     boolean targeted = false;
144
145     if(start == old_ih) {
146       targeted = true;
147       setStart(new_ih);
148     }
149
150     if(end == old_ih) {
151       targeted = true;
152       setEnd(new_ih);
153     }
154
155     if(!targeted)
156       throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
157                   end + "}");
158   }
159
160   /**
161    * @return true, if ih is target of this variable
162    */

163   public boolean containsTarget(InstructionHandle ih) {
164     return (start == ih) || (end == ih);
165   }
166
167   /**
168    * We consider to local variables to be equal, if the use the same index and
169    * are valid in the same range.
170    */

171   public boolean equals(Object JavaDoc o) {
172     if(!(o instanceof LocalVariableGen))
173       return false;
174
175     LocalVariableGen l = (LocalVariableGen)o;
176     return (l.index == index) && (l.start == start) && (l.end == end);
177   }
178
179   public String JavaDoc toString() {
180     return "LocalVariableGen(" + name + ", " + type + ", " + start + ", " + end + ")";
181   }
182
183   public Object JavaDoc clone() {
184     try {
185       return super.clone();
186     } catch(CloneNotSupportedException JavaDoc e) {
187       System.err.println(e);
188       return null;
189     }
190   }
191 }
192
Popular Tags