19 * limitations under the License. |
18 * limitations under the License. |
20 */ |
19 */ |
21 |
20 |
22 package com.sun.org.apache.bcel.internal.classfile; |
21 package com.sun.org.apache.bcel.internal.classfile; |
23 |
22 |
24 |
23 import java.io.DataInput; |
25 import com.sun.org.apache.bcel.internal.Constants; |
24 import java.io.DataOutputStream; |
26 import java.io.*; |
25 import java.io.IOException; |
|
26 |
|
27 import com.sun.org.apache.bcel.internal.Const; |
27 |
28 |
28 /** |
29 /** |
29 * This class represents a local variable within a method. It contains its |
30 * This class represents a local variable within a method. It contains its |
30 * scope, name, signature and index on the method's frame. |
31 * scope, name, signature and index on the method's frame. |
31 * |
32 * |
32 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> |
33 * @version $Id: LocalVariable.java 1749603 2016-06-21 20:50:19Z ggregory $ |
33 * @see LocalVariableTable |
34 * @see LocalVariableTable |
34 */ |
35 */ |
35 public final class LocalVariable |
36 public final class LocalVariable implements Cloneable, Node { |
36 implements Constants, Cloneable, Node, Serializable |
37 |
37 { |
38 private int start_pc; // Range in which the variable is valid |
38 private int start_pc; // Range in which the variable is valid |
39 private int length; |
39 private int length; |
40 private int name_index; // Index in constant pool of variable name |
40 private int name_index; // Index in constant pool of variable name |
41 private int signature_index; // Index of variable signature |
41 private int signature_index; // Index of variable signature |
42 private int index; /* Variable is `index'th local variable on |
42 private int index; /* Variable is `index'th local variable on |
43 * this method's frame. |
43 * this method's frame. |
44 */ |
44 */ |
45 private ConstantPool constant_pool; |
45 |
46 |
46 private ConstantPool constant_pool; |
47 |
47 |
48 /** |
48 /** |
49 * Initialize from another object. Note that both objects use the same |
49 * Initialize from another object. Note that both objects use the same |
50 * references (shallow copy). Use copy() for a physical copy. |
50 * references (shallow copy). Use copy() for a physical copy. |
51 */ |
51 */ |
52 public LocalVariable(final LocalVariable c) { |
52 public LocalVariable(LocalVariable c) { |
53 this(c.getStartPC(), c.getLength(), c.getNameIndex(), c.getSignatureIndex(), c.getIndex(), |
53 this(c.getStartPC(), c.getLength(), c.getNameIndex(), |
54 c.getConstantPool()); |
54 c.getSignatureIndex(), c.getIndex(), c.getConstantPool()); |
55 } |
55 } |
56 |
56 |
57 |
57 /** |
58 /** |
58 * Construct object from file stream. |
59 * Construct object from file stream. |
59 * @param file Input stream |
60 * @param file Input stream |
60 * @throws IOException |
61 * @throws IOException |
61 */ |
62 */ |
62 LocalVariable(DataInputStream file, ConstantPool constant_pool) |
63 LocalVariable(final DataInput file, final ConstantPool constant_pool) throws IOException { |
63 throws IOException |
64 this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file |
64 { |
65 .readUnsignedShort(), file.readUnsignedShort(), constant_pool); |
65 this(file.readUnsignedShort(), file.readUnsignedShort(), |
66 } |
66 file.readUnsignedShort(), file.readUnsignedShort(), |
67 |
67 file.readUnsignedShort(), constant_pool); |
68 |
68 } |
69 /** |
69 |
70 * @param start_pc Range in which the variable |
70 /** |
71 * @param length ... is valid |
71 * @param start_pc Range in which the variable |
72 * @param name_index Index in constant pool of variable name |
72 * @param length ... is valid |
73 * @param signature_index Index of variable's signature |
73 * @param name_index Index in constant pool of variable name |
74 * @param index Variable is `index'th local variable on the method's frame |
74 * @param signature_index Index of variable's signature |
75 * @param constant_pool Array of constants |
75 * @param index Variable is `index'th local variable on the method's frame |
76 */ |
76 * @param constant_pool Array of constants |
77 public LocalVariable(final int start_pc, final int length, final int name_index, final int signature_index, final int index, |
77 */ |
78 final ConstantPool constant_pool) { |
78 public LocalVariable(int start_pc, int length, int name_index, |
79 this.start_pc = start_pc; |
79 int signature_index, int index, |
80 this.length = length; |
80 ConstantPool constant_pool) |
81 this.name_index = name_index; |
81 { |
82 this.signature_index = signature_index; |
82 this.start_pc = start_pc; |
83 this.index = index; |
83 this.length = length; |
84 this.constant_pool = constant_pool; |
84 this.name_index = name_index; |
85 } |
85 this.signature_index = signature_index; |
86 |
86 this.index = index; |
87 |
87 this.constant_pool = constant_pool; |
88 /** |
88 } |
89 * Called by objects that are traversing the nodes of the tree implicitely |
89 |
90 * defined by the contents of a Java class. I.e., the hierarchy of methods, |
90 /** |
91 * fields, attributes, etc. spawns a tree of objects. |
91 * Called by objects that are traversing the nodes of the tree implicitely |
92 * |
92 * defined by the contents of a Java class. I.e., the hierarchy of methods, |
93 * @param v Visitor object |
93 * fields, attributes, etc. spawns a tree of objects. |
94 */ |
94 * |
95 @Override |
95 * @param v Visitor object |
96 public void accept( final Visitor v ) { |
96 */ |
97 v.visitLocalVariable(this); |
97 public void accept(Visitor v) { |
98 } |
98 v.visitLocalVariable(this); |
99 |
99 } |
100 |
100 |
101 /** |
101 /** |
102 * Dump local variable to file stream in binary format. |
102 * Dump local variable to file stream in binary format. |
103 * |
103 * |
104 * @param file Output file stream |
104 * @param file Output file stream |
105 * @throws IOException |
105 * @throws IOException |
106 */ |
106 */ |
107 public final void dump( final DataOutputStream file ) throws IOException { |
107 public final void dump(DataOutputStream file) throws IOException |
108 file.writeShort(start_pc); |
108 { |
109 file.writeShort(length); |
109 file.writeShort(start_pc); |
110 file.writeShort(name_index); |
110 file.writeShort(length); |
111 file.writeShort(signature_index); |
111 file.writeShort(name_index); |
112 file.writeShort(index); |
112 file.writeShort(signature_index); |
113 } |
113 file.writeShort(index); |
114 |
114 } |
115 |
115 |
116 /** |
116 /** |
117 * @return Constant pool used by this object. |
117 * @return Constant pool used by this object. |
118 */ |
118 */ |
119 public final ConstantPool getConstantPool() { |
119 public final ConstantPool getConstantPool() { return constant_pool; } |
120 return constant_pool; |
120 |
121 } |
121 /** |
122 |
122 * @return Variable is valid within getStartPC() .. getStartPC()+getLength() |
123 |
123 */ |
124 /** |
124 public final int getLength() { return length; } |
125 * @return Variable is valid within getStartPC() .. getStartPC()+getLength() |
125 |
126 */ |
126 /** |
127 public final int getLength() { |
127 * @return Variable name. |
128 return length; |
128 */ |
129 } |
129 public final String getName() { |
130 |
130 ConstantUtf8 c; |
131 |
131 |
132 /** |
132 c = (ConstantUtf8)constant_pool.getConstant(name_index, CONSTANT_Utf8); |
133 * @return Variable name. |
133 return c.getBytes(); |
134 */ |
134 } |
135 public final String getName() { |
135 |
136 ConstantUtf8 c; |
136 /** |
137 c = (ConstantUtf8) constant_pool.getConstant(name_index, Const.CONSTANT_Utf8); |
137 * @return Index in constant pool of variable name. |
138 return c.getBytes(); |
138 */ |
139 } |
139 public final int getNameIndex() { return name_index; } |
140 |
140 |
141 |
141 /** |
142 /** |
142 * @return Signature. |
143 * @return Index in constant pool of variable name. |
143 */ |
144 */ |
144 public final String getSignature() { |
145 public final int getNameIndex() { |
145 ConstantUtf8 c; |
146 return name_index; |
146 c = (ConstantUtf8)constant_pool.getConstant(signature_index, |
147 } |
147 CONSTANT_Utf8); |
148 |
148 return c.getBytes(); |
149 |
149 } |
150 /** |
150 |
151 * @return Signature. |
151 /** |
152 */ |
152 * @return Index in constant pool of variable signature. |
153 public final String getSignature() { |
153 */ |
154 ConstantUtf8 c; |
154 public final int getSignatureIndex() { return signature_index; } |
155 c = (ConstantUtf8) constant_pool.getConstant(signature_index, Const.CONSTANT_Utf8); |
155 |
156 return c.getBytes(); |
156 /** |
157 } |
157 * @return index of register where variable is stored |
158 |
158 */ |
159 |
159 public final int getIndex() { return index; } |
160 /** |
160 |
161 * @return Index in constant pool of variable signature. |
161 /** |
162 */ |
162 * @return Start of range where he variable is valid |
163 public final int getSignatureIndex() { |
163 */ |
164 return signature_index; |
164 public final int getStartPC() { return start_pc; } |
165 } |
165 |
166 |
166 /** |
167 |
167 * @param constant_pool Constant pool to be used for this object. |
168 /** |
168 */ |
169 * @return index of register where variable is stored |
169 public final void setConstantPool(ConstantPool constant_pool) { |
170 */ |
170 this.constant_pool = constant_pool; |
171 public final int getIndex() { |
171 } |
172 return index; |
172 |
173 } |
173 /** |
174 |
174 * @param length. |
175 |
175 */ |
176 /** |
176 public final void setLength(int length) { |
177 * @return Start of range where he variable is valid |
177 this.length = length; |
178 */ |
178 } |
179 public final int getStartPC() { |
179 |
180 return start_pc; |
180 /** |
181 } |
181 * @param name_index. |
182 |
182 */ |
183 |
183 public final void setNameIndex(int name_index) { |
184 /* |
184 this.name_index = name_index; |
185 * Helper method shared with LocalVariableTypeTable |
185 } |
186 */ |
186 |
187 final String toStringShared( final boolean typeTable ) { |
187 /** |
188 final String name = getName(); |
188 * @param signature_index. |
189 final String signature = Utility.signatureToString(getSignature(), false); |
189 */ |
190 final String label = "LocalVariable" + (typeTable ? "Types" : "" ); |
190 public final void setSignatureIndex(int signature_index) { |
191 return label + "(start_pc = " + start_pc + ", length = " + length + ", index = " |
191 this.signature_index = signature_index; |
192 + index + ":" + signature + " " + name + ")"; |
192 } |
193 } |
193 |
194 |
194 /** |
195 |
195 * @param index. |
196 /** |
196 */ |
197 * @param constant_pool Constant pool to be used for this object. |
197 public final void setIndex(int index) { this.index = index; } |
198 */ |
198 |
199 public final void setConstantPool( final ConstantPool constant_pool ) { |
199 /** |
200 this.constant_pool = constant_pool; |
200 * @param start_pc Specify range where the local variable is valid. |
201 } |
201 */ |
202 |
202 public final void setStartPC(int start_pc) { |
203 |
203 this.start_pc = start_pc; |
204 /** |
204 } |
205 * @param length the length of this local variable |
205 |
206 */ |
206 /** |
207 public final void setLength( final int length ) { |
207 * @return string representation. |
208 this.length = length; |
208 */ |
209 } |
209 public final String toString() { |
210 |
210 String name = getName(), signature = Utility.signatureToString(getSignature()); |
211 |
211 |
212 /** |
212 return "LocalVariable(start_pc = " + start_pc + ", length = " + length + |
213 * @param name_index the index into the constant pool for the name of this variable |
213 ", index = " + index + ":" + signature + " " + name + ")"; |
214 */ |
214 } |
215 public final void setNameIndex( final int name_index ) { // TODO unused |
215 |
216 this.name_index = name_index; |
216 /** |
217 } |
217 * @return deep copy of this object |
218 |
218 */ |
219 |
219 public LocalVariable copy() { |
220 /** |
220 try { |
221 * @param signature_index the index into the constant pool for the signature of this variable |
221 return (LocalVariable)clone(); |
222 */ |
222 } catch(CloneNotSupportedException e) {} |
223 public final void setSignatureIndex( final int signature_index ) { // TODO unused |
223 |
224 this.signature_index = signature_index; |
224 return null; |
225 } |
225 } |
226 |
|
227 |
|
228 /** |
|
229 * @param index the index in the local variable table of this variable |
|
230 */ |
|
231 public final void setIndex( final int index ) { // TODO unused |
|
232 this.index = index; |
|
233 } |
|
234 |
|
235 |
|
236 /** |
|
237 * @param start_pc Specify range where the local variable is valid. |
|
238 */ |
|
239 public final void setStartPC( final int start_pc ) { // TODO unused |
|
240 this.start_pc = start_pc; |
|
241 } |
|
242 |
|
243 |
|
244 /** |
|
245 * @return string representation. |
|
246 */ |
|
247 @Override |
|
248 public final String toString() { |
|
249 return toStringShared(false); |
|
250 } |
|
251 |
|
252 |
|
253 /** |
|
254 * @return deep copy of this object |
|
255 */ |
|
256 public LocalVariable copy() { |
|
257 try { |
|
258 return (LocalVariable) clone(); |
|
259 } catch (final CloneNotSupportedException e) { |
|
260 // TODO should this throw? |
|
261 } |
|
262 return null; |
|
263 } |
226 } |
264 } |