author | joehw |
Sun, 13 Aug 2017 21:10:40 -0700 | |
changeset 46174 | 5611d2529b49 |
parent 44797 | 8b3b3b911b8a |
permissions | -rw-r--r-- |
6 | 1 |
/* |
46174 | 2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
6 | 3 |
*/ |
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
4 |
/* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
5 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* the License. You may obtain a copy of the License at |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
13 |
* |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
14 |
* Unless required by applicable law or agreed to in writing, software |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
17 |
* See the License for the specific language governing permissions and |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
18 |
* limitations under the License. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
19 |
*/ |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
20 |
|
6 | 21 |
package com.sun.org.apache.bcel.internal.classfile; |
22 |
||
46174 | 23 |
import java.io.DataInput; |
24 |
import java.io.DataOutputStream; |
|
25 |
import java.io.IOException; |
|
6 | 26 |
|
46174 | 27 |
import com.sun.org.apache.bcel.internal.Const; |
6 | 28 |
|
29 |
/** |
|
30 |
* This class represents a local variable within a method. It contains its |
|
31 |
* scope, name, signature and index on the method's frame. |
|
32 |
* |
|
46174 | 33 |
* @version $Id: LocalVariable.java 1749603 2016-06-21 20:50:19Z ggregory $ |
6 | 34 |
* @see LocalVariableTable |
35 |
*/ |
|
46174 | 36 |
public final class LocalVariable implements Cloneable, Node { |
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 |
private ConstantPool constant_pool; |
|
6 | 46 |
|
46174 | 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(final LocalVariable c) { |
|
53 |
this(c.getStartPC(), c.getLength(), c.getNameIndex(), c.getSignatureIndex(), c.getIndex(), |
|
54 |
c.getConstantPool()); |
|
55 |
} |
|
56 |
||
6 | 57 |
|
46174 | 58 |
/** |
59 |
* Construct object from file stream. |
|
60 |
* @param file Input stream |
|
61 |
* @throws IOException |
|
62 |
*/ |
|
63 |
LocalVariable(final DataInput file, final ConstantPool constant_pool) throws IOException { |
|
64 |
this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file |
|
65 |
.readUnsignedShort(), file.readUnsignedShort(), constant_pool); |
|
66 |
} |
|
67 |
||
6 | 68 |
|
46174 | 69 |
/** |
70 |
* @param start_pc Range in which the variable |
|
71 |
* @param length ... is valid |
|
72 |
* @param name_index Index in constant pool of variable name |
|
73 |
* @param signature_index Index of variable's signature |
|
74 |
* @param index Variable is `index'th local variable on the method's frame |
|
75 |
* @param constant_pool Array of constants |
|
76 |
*/ |
|
77 |
public LocalVariable(final int start_pc, final int length, final int name_index, final int signature_index, final int index, |
|
78 |
final ConstantPool constant_pool) { |
|
79 |
this.start_pc = start_pc; |
|
80 |
this.length = length; |
|
81 |
this.name_index = name_index; |
|
82 |
this.signature_index = signature_index; |
|
83 |
this.index = index; |
|
84 |
this.constant_pool = constant_pool; |
|
85 |
} |
|
86 |
||
6 | 87 |
|
46174 | 88 |
/** |
89 |
* Called by objects that are traversing the nodes of the tree implicitely |
|
90 |
* defined by the contents of a Java class. I.e., the hierarchy of methods, |
|
91 |
* fields, attributes, etc. spawns a tree of objects. |
|
92 |
* |
|
93 |
* @param v Visitor object |
|
94 |
*/ |
|
95 |
@Override |
|
96 |
public void accept( final Visitor v ) { |
|
97 |
v.visitLocalVariable(this); |
|
98 |
} |
|
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( final DataOutputStream file ) throws IOException { |
|
108 |
file.writeShort(start_pc); |
|
109 |
file.writeShort(length); |
|
110 |
file.writeShort(name_index); |
|
111 |
file.writeShort(signature_index); |
|
112 |
file.writeShort(index); |
|
113 |
} |
|
114 |
||
6 | 115 |
|
46174 | 116 |
/** |
117 |
* @return Constant pool used by this object. |
|
118 |
*/ |
|
119 |
public final ConstantPool getConstantPool() { |
|
120 |
return constant_pool; |
|
121 |
} |
|
122 |
||
123 |
||
124 |
/** |
|
125 |
* @return Variable is valid within getStartPC() .. getStartPC()+getLength() |
|
126 |
*/ |
|
127 |
public final int getLength() { |
|
128 |
return length; |
|
129 |
} |
|
130 |
||
6 | 131 |
|
46174 | 132 |
/** |
133 |
* @return Variable name. |
|
134 |
*/ |
|
135 |
public final String getName() { |
|
136 |
ConstantUtf8 c; |
|
137 |
c = (ConstantUtf8) constant_pool.getConstant(name_index, Const.CONSTANT_Utf8); |
|
138 |
return c.getBytes(); |
|
139 |
} |
|
6 | 140 |
|
141 |
||
46174 | 142 |
/** |
143 |
* @return Index in constant pool of variable name. |
|
144 |
*/ |
|
145 |
public final int getNameIndex() { |
|
146 |
return name_index; |
|
147 |
} |
|
148 |
||
6 | 149 |
|
46174 | 150 |
/** |
151 |
* @return Signature. |
|
152 |
*/ |
|
153 |
public final String getSignature() { |
|
154 |
ConstantUtf8 c; |
|
155 |
c = (ConstantUtf8) constant_pool.getConstant(signature_index, Const.CONSTANT_Utf8); |
|
156 |
return c.getBytes(); |
|
157 |
} |
|
158 |
||
6 | 159 |
|
46174 | 160 |
/** |
161 |
* @return Index in constant pool of variable signature. |
|
162 |
*/ |
|
163 |
public final int getSignatureIndex() { |
|
164 |
return signature_index; |
|
165 |
} |
|
166 |
||
6 | 167 |
|
46174 | 168 |
/** |
169 |
* @return index of register where variable is stored |
|
170 |
*/ |
|
171 |
public final int getIndex() { |
|
172 |
return index; |
|
173 |
} |
|
174 |
||
6 | 175 |
|
46174 | 176 |
/** |
177 |
* @return Start of range where he variable is valid |
|
178 |
*/ |
|
179 |
public final int getStartPC() { |
|
180 |
return start_pc; |
|
181 |
} |
|
6 | 182 |
|
183 |
||
46174 | 184 |
/* |
185 |
* Helper method shared with LocalVariableTypeTable |
|
186 |
*/ |
|
187 |
final String toStringShared( final boolean typeTable ) { |
|
188 |
final String name = getName(); |
|
189 |
final String signature = Utility.signatureToString(getSignature(), false); |
|
190 |
final String label = "LocalVariable" + (typeTable ? "Types" : "" ); |
|
191 |
return label + "(start_pc = " + start_pc + ", length = " + length + ", index = " |
|
192 |
+ index + ":" + signature + " " + name + ")"; |
|
193 |
} |
|
6 | 194 |
|
195 |
||
46174 | 196 |
/** |
197 |
* @param constant_pool Constant pool to be used for this object. |
|
198 |
*/ |
|
199 |
public final void setConstantPool( final ConstantPool constant_pool ) { |
|
200 |
this.constant_pool = constant_pool; |
|
201 |
} |
|
202 |
||
6 | 203 |
|
46174 | 204 |
/** |
205 |
* @param length the length of this local variable |
|
206 |
*/ |
|
207 |
public final void setLength( final int length ) { |
|
208 |
this.length = length; |
|
209 |
} |
|
210 |
||
211 |
||
212 |
/** |
|
213 |
* @param name_index the index into the constant pool for the name of this variable |
|
214 |
*/ |
|
215 |
public final void setNameIndex( final int name_index ) { // TODO unused |
|
216 |
this.name_index = name_index; |
|
217 |
} |
|
6 | 218 |
|
46174 | 219 |
|
220 |
/** |
|
221 |
* @param signature_index the index into the constant pool for the signature of this variable |
|
222 |
*/ |
|
223 |
public final void setSignatureIndex( final int signature_index ) { // TODO unused |
|
224 |
this.signature_index = signature_index; |
|
225 |
} |
|
6 | 226 |
|
227 |
||
46174 | 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 |
} |
|
6 | 234 |
|
235 |
||
46174 | 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 |
||
6 | 243 |
|
46174 | 244 |
/** |
245 |
* @return string representation. |
|
246 |
*/ |
|
247 |
@Override |
|
248 |
public final String toString() { |
|
249 |
return toStringShared(false); |
|
250 |
} |
|
251 |
||
6 | 252 |
|
46174 | 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 |
} |
|
6 | 264 |
} |