author | ntoda |
Thu, 31 Jul 2014 17:01:24 -0700 | |
changeset 25799 | 1afc4675dc75 |
parent 25522 | 10d789df41bb |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
16854
diff
changeset
|
2 |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.tools.java; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.DataInputStream; |
|
30 |
import java.io.DataOutputStream; |
|
31 |
import java.util.Vector; |
|
32 |
import java.util.Hashtable; |
|
33 |
||
34 |
/** |
|
35 |
* This class is used to represent a constant table once |
|
36 |
* it is read from a class file. |
|
37 |
* |
|
38 |
* WARNING: The contents of this source file are not part of any |
|
39 |
* supported API. Code that depends on them does so at its own risk: |
|
40 |
* they are subject to change or removal without notice. |
|
41 |
*/ |
|
42 |
public final |
|
43 |
class BinaryConstantPool implements Constants { |
|
44 |
private byte types[]; |
|
45 |
private Object cpool[]; |
|
46 |
||
47 |
/** |
|
48 |
* Constructor |
|
49 |
*/ |
|
50 |
BinaryConstantPool(DataInputStream in) throws IOException { |
|
51 |
// JVM 4.1 ClassFile.constant_pool_count |
|
52 |
types = new byte[in.readUnsignedShort()]; |
|
53 |
cpool = new Object[types.length]; |
|
54 |
for (int i = 1 ; i < cpool.length ; i++) { |
|
55 |
int j = i; |
|
56 |
// JVM 4.4 cp_info.tag |
|
57 |
switch(types[i] = in.readByte()) { |
|
58 |
case CONSTANT_UTF8: |
|
59 |
cpool[i] = in.readUTF(); |
|
60 |
break; |
|
61 |
||
62 |
case CONSTANT_INTEGER: |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
25186
diff
changeset
|
63 |
cpool[i] = in.readInt(); |
2 | 64 |
break; |
65 |
case CONSTANT_FLOAT: |
|
66 |
cpool[i] = new Float(in.readFloat()); |
|
67 |
break; |
|
68 |
case CONSTANT_LONG: |
|
25186
63e1a2ec30f5
8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents:
23010
diff
changeset
|
69 |
cpool[i++] = in.readLong(); |
2 | 70 |
break; |
71 |
case CONSTANT_DOUBLE: |
|
72 |
cpool[i++] = new Double(in.readDouble()); |
|
73 |
break; |
|
74 |
||
75 |
case CONSTANT_CLASS: |
|
76 |
case CONSTANT_STRING: |
|
77 |
// JVM 4.4.3 CONSTANT_String_info.string_index |
|
78 |
// or JVM 4.4.1 CONSTANT_Class_info.name_index |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
25186
diff
changeset
|
79 |
cpool[i] =in.readUnsignedShort(); |
2 | 80 |
break; |
81 |
||
82 |
case CONSTANT_FIELD: |
|
83 |
case CONSTANT_METHOD: |
|
84 |
case CONSTANT_INTERFACEMETHOD: |
|
85 |
case CONSTANT_NAMEANDTYPE: |
|
86 |
// JVM 4.4.2 CONSTANT_*ref_info.class_index & name_and_type_index |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
25186
diff
changeset
|
87 |
cpool[i] = (in.readUnsignedShort() << 16) | in.readUnsignedShort(); |
2 | 88 |
break; |
89 |
||
16854
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
90 |
case CONSTANT_METHODHANDLE: |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
91 |
cpool[i] = readBytes(in, 3); |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
92 |
break; |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
93 |
case CONSTANT_METHODTYPE: |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
94 |
cpool[i] = readBytes(in, 2); |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
95 |
break; |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
96 |
case CONSTANT_INVOKEDYNAMIC: |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
97 |
cpool[i] = readBytes(in, 4); |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
98 |
break; |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
99 |
|
2 | 100 |
case 0: |
101 |
default: |
|
102 |
throw new ClassFormatError("invalid constant type: " + (int)types[i]); |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
||
16854
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
107 |
private byte[] readBytes(DataInputStream in, int cnt) throws IOException { |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
108 |
byte[] b = new byte[cnt]; |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
109 |
in.readFully(b); |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
110 |
return b; |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
111 |
} |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
112 |
|
2 | 113 |
/** |
114 |
* get a integer |
|
115 |
*/ |
|
116 |
public int getInteger(int n) { |
|
117 |
return (n == 0) ? 0 : ((Number)cpool[n]).intValue(); |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* get a value |
|
122 |
*/ |
|
123 |
public Object getValue(int n) { |
|
124 |
return (n == 0) ? null : cpool[n]; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* get a string |
|
129 |
*/ |
|
130 |
public String getString(int n) { |
|
131 |
return (n == 0) ? null : (String)cpool[n]; |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* get an identifier |
|
136 |
*/ |
|
137 |
public Identifier getIdentifier(int n) { |
|
138 |
return (n == 0) ? null : Identifier.lookup(getString(n)); |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* get class declaration |
|
143 |
*/ |
|
144 |
public ClassDeclaration getDeclarationFromName(Environment env, int n) { |
|
145 |
return (n == 0) ? null : env.getClassDeclaration(Identifier.lookup(getString(n).replace('/','.'))); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* get class declaration |
|
150 |
*/ |
|
151 |
public ClassDeclaration getDeclaration(Environment env, int n) { |
|
152 |
return (n == 0) ? null : getDeclarationFromName(env, getInteger(n)); |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* get a type from a type signature |
|
157 |
*/ |
|
158 |
public Type getType(int n) { |
|
159 |
return Type.tType(getString(n)); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* get the type of constant given an index |
|
164 |
*/ |
|
165 |
public int getConstantType(int n) { |
|
166 |
return types[n]; |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* get the n-th constant from the constant pool |
|
171 |
*/ |
|
172 |
public Object getConstant(int n, Environment env) { |
|
173 |
int constant_type = getConstantType(n); |
|
174 |
switch (constant_type) { |
|
175 |
case CONSTANT_INTEGER: |
|
176 |
case CONSTANT_FLOAT: |
|
177 |
case CONSTANT_LONG: |
|
178 |
case CONSTANT_DOUBLE: |
|
16854
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
179 |
case CONSTANT_METHODHANDLE: |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
180 |
case CONSTANT_METHODTYPE: |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
181 |
case CONSTANT_INVOKEDYNAMIC: |
2 | 182 |
return getValue(n); |
183 |
||
184 |
case CONSTANT_CLASS: |
|
185 |
return getDeclaration(env, n); |
|
186 |
||
187 |
case CONSTANT_STRING: |
|
188 |
return getString(getInteger(n)); |
|
189 |
||
190 |
case CONSTANT_FIELD: |
|
191 |
case CONSTANT_METHOD: |
|
192 |
case CONSTANT_INTERFACEMETHOD: |
|
193 |
try { |
|
194 |
int key = getInteger(n); |
|
195 |
ClassDefinition clazz = |
|
196 |
getDeclaration(env, key >> 16).getClassDefinition(env); |
|
197 |
int name_and_type = getInteger(key & 0xFFFF); |
|
198 |
Identifier id = getIdentifier(name_and_type >> 16); |
|
199 |
Type type = getType(name_and_type & 0xFFFF); |
|
200 |
||
201 |
for (MemberDefinition field = clazz.getFirstMatch(id); |
|
202 |
field != null; |
|
203 |
field = field.getNextMatch()) { |
|
204 |
Type field_type = field.getType(); |
|
205 |
if ((constant_type == CONSTANT_FIELD) |
|
206 |
? (field_type == type) |
|
207 |
: (field_type.equalArguments(type))) |
|
208 |
return field; |
|
209 |
} |
|
210 |
} catch (ClassNotFound e) { |
|
211 |
} |
|
212 |
return null; |
|
213 |
||
214 |
default: |
|
215 |
throw new ClassFormatError("invalid constant type: " + |
|
216 |
constant_type); |
|
217 |
} |
|
218 |
} |
|
219 |
||
220 |
||
221 |
/** |
|
222 |
* Get a list of dependencies, ie: all the classes referenced in this |
|
223 |
* constant pool. |
|
224 |
*/ |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
225 |
public Vector<ClassDeclaration> getDependencies(Environment env) { |
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
226 |
Vector<ClassDeclaration> v = new Vector<>(); |
2 | 227 |
for (int i = 1 ; i < cpool.length ; i++) { |
228 |
switch(types[i]) { |
|
229 |
case CONSTANT_CLASS: |
|
230 |
v.addElement(getDeclarationFromName(env, getInteger(i))); |
|
231 |
break; |
|
232 |
} |
|
233 |
} |
|
234 |
return v; |
|
235 |
} |
|
236 |
||
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
237 |
Hashtable<Object, Integer> indexHashObject; |
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
238 |
Hashtable<Object, Integer> indexHashAscii; |
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
239 |
Vector<String> MoreStuff; |
2 | 240 |
|
241 |
/** |
|
242 |
* Find the index of an Object in the constant pool |
|
243 |
*/ |
|
244 |
public int indexObject(Object obj, Environment env) { |
|
245 |
if (indexHashObject == null) |
|
246 |
createIndexHash(env); |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
247 |
Integer result = indexHashObject.get(obj); |
2 | 248 |
if (result == null) |
249 |
throw new IndexOutOfBoundsException("Cannot find object " + obj + " of type " + |
|
250 |
obj.getClass() + " in constant pool"); |
|
251 |
return result.intValue(); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Find the index of an ascii string in the constant pool. If it's not in |
|
256 |
* the constant pool, then add it at the end. |
|
257 |
*/ |
|
258 |
public int indexString(String string, Environment env) { |
|
259 |
if (indexHashObject == null) |
|
260 |
createIndexHash(env); |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
261 |
Integer result = indexHashAscii.get(string); |
2 | 262 |
if (result == null) { |
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
263 |
if (MoreStuff == null) MoreStuff = new Vector<>(); |
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
25186
diff
changeset
|
264 |
result = cpool.length + MoreStuff.size(); |
2 | 265 |
MoreStuff.addElement(string); |
266 |
indexHashAscii.put(string, result); |
|
267 |
} |
|
268 |
return result.intValue(); |
|
269 |
} |
|
270 |
||
271 |
/** |
|
272 |
* Create a hash table of all the items in the constant pool that could |
|
273 |
* possibly be referenced from the outside. |
|
274 |
*/ |
|
275 |
||
276 |
public void createIndexHash(Environment env) { |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
277 |
indexHashObject = new Hashtable<>(); |
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
278 |
indexHashAscii = new Hashtable<>(); |
2 | 279 |
for (int i = 1; i < cpool.length; i++) { |
280 |
if (types[i] == CONSTANT_UTF8) { |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
25186
diff
changeset
|
281 |
indexHashAscii.put(cpool[i], i); |
2 | 282 |
} else { |
283 |
try { |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
25186
diff
changeset
|
284 |
indexHashObject.put(getConstant(i, env), i); |
2 | 285 |
} catch (ClassFormatError e) { } |
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
||
291 |
/** |
|
292 |
* Write out the contents of the constant pool, including any additions |
|
293 |
* that have been added. |
|
294 |
*/ |
|
295 |
public void write(DataOutputStream out, Environment env) throws IOException { |
|
296 |
int length = cpool.length; |
|
297 |
if (MoreStuff != null) |
|
298 |
length += MoreStuff.size(); |
|
299 |
out.writeShort(length); |
|
300 |
for (int i = 1 ; i < cpool.length; i++) { |
|
301 |
int type = types[i]; |
|
302 |
Object x = cpool[i]; |
|
303 |
out.writeByte(type); |
|
304 |
switch (type) { |
|
305 |
case CONSTANT_UTF8: |
|
306 |
out.writeUTF((String) x); |
|
307 |
break; |
|
308 |
case CONSTANT_INTEGER: |
|
309 |
out.writeInt(((Number)x).intValue()); |
|
310 |
break; |
|
311 |
case CONSTANT_FLOAT: |
|
312 |
out.writeFloat(((Number)x).floatValue()); |
|
313 |
break; |
|
314 |
case CONSTANT_LONG: |
|
315 |
out.writeLong(((Number)x).longValue()); |
|
316 |
i++; |
|
317 |
break; |
|
318 |
case CONSTANT_DOUBLE: |
|
319 |
out.writeDouble(((Number)x).doubleValue()); |
|
320 |
i++; |
|
321 |
break; |
|
322 |
case CONSTANT_CLASS: |
|
323 |
case CONSTANT_STRING: |
|
324 |
out.writeShort(((Number)x).intValue()); |
|
325 |
break; |
|
326 |
case CONSTANT_FIELD: |
|
327 |
case CONSTANT_METHOD: |
|
328 |
case CONSTANT_INTERFACEMETHOD: |
|
329 |
case CONSTANT_NAMEANDTYPE: { |
|
330 |
int value = ((Number)x).intValue(); |
|
331 |
out.writeShort(value >> 16); |
|
332 |
out.writeShort(value & 0xFFFF); |
|
333 |
break; |
|
334 |
} |
|
16854
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
335 |
case CONSTANT_METHODHANDLE: |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
336 |
case CONSTANT_METHODTYPE: |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
337 |
case CONSTANT_INVOKEDYNAMIC: |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
338 |
out.write((byte[])x, 0, ((byte[])x).length); |
9371f5046d02
8011805: Update sun.tools.java class file reading/writing support to include the new constant pool entries
rfield
parents:
5506
diff
changeset
|
339 |
break; |
2 | 340 |
default: |
341 |
throw new ClassFormatError("invalid constant type: " |
|
342 |
+ (int)types[i]); |
|
343 |
} |
|
344 |
} |
|
345 |
for (int i = cpool.length; i < length; i++) { |
|
25799
1afc4675dc75
8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents:
25522
diff
changeset
|
346 |
String string = MoreStuff.elementAt(i - cpool.length); |
2 | 347 |
out.writeByte(CONSTANT_UTF8); |
348 |
out.writeUTF(string); |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
} |