author | alanb |
Thu, 19 May 2016 13:10:08 +0100 | |
changeset 38425 | 3191fb2435f8 |
parent 37779 | 7c84df693837 |
child 39050 | 9de41b79ec7e |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
package java.lang.module; |
|
27 |
||
28 |
import java.io.DataInput; |
|
29 |
import java.io.DataInputStream; |
|
30 |
import java.io.EOFException; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.UncheckedIOException; |
|
34 |
import java.lang.module.ModuleDescriptor.Requires.Modifier; |
|
35 |
import java.nio.ByteBuffer; |
|
36 |
import java.nio.BufferUnderflowException; |
|
37 |
import java.util.Collections; |
|
38 |
import java.util.HashMap; |
|
39 |
import java.util.HashSet; |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
40 |
import java.util.LinkedHashSet; |
36511 | 41 |
import java.util.Map; |
42 |
import java.util.Set; |
|
43 |
import java.util.function.Supplier; |
|
44 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
45 |
import jdk.internal.module.ModuleHashes; |
36511 | 46 |
|
47 |
import static jdk.internal.module.ClassFileConstants.*; |
|
48 |
||
49 |
||
50 |
/** |
|
51 |
* Read module information from a {@code module-info} class file. |
|
52 |
* |
|
53 |
* @implNote The rationale for the hand-coded reader is startup performance |
|
54 |
* and fine control over the throwing of InvalidModuleDescriptorException. |
|
55 |
*/ |
|
56 |
||
57 |
final class ModuleInfo { |
|
58 |
||
59 |
// supplies the set of packages when ConcealedPackages not present |
|
60 |
private final Supplier<Set<String>> packageFinder; |
|
61 |
||
62 |
// indicates if the Hashes attribute should be parsed |
|
63 |
private final boolean parseHashes; |
|
64 |
||
65 |
// the builder, created when parsing |
|
66 |
private ModuleDescriptor.Builder builder; |
|
67 |
||
68 |
private ModuleInfo(Supplier<Set<String>> pf, boolean ph) { |
|
69 |
packageFinder = pf; |
|
70 |
parseHashes = ph; |
|
71 |
} |
|
72 |
||
73 |
private ModuleInfo(Supplier<Set<String>> pf) { |
|
74 |
this(pf, true); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Reads a {@code module-info.class} from the given input stream. |
|
79 |
* |
|
80 |
* @throws InvalidModuleDescriptorException |
|
81 |
* @throws IOException |
|
82 |
*/ |
|
83 |
public static ModuleDescriptor read(InputStream in, |
|
84 |
Supplier<Set<String>> pf) |
|
85 |
throws IOException |
|
86 |
{ |
|
87 |
try { |
|
88 |
return new ModuleInfo(pf).doRead(new DataInputStream(in)); |
|
89 |
} catch (IllegalArgumentException iae) { |
|
90 |
// IllegalArgumentException means a malformed class |
|
91 |
throw invalidModuleDescriptor(iae.getMessage()); |
|
92 |
} catch (EOFException x) { |
|
93 |
throw truncatedModuleDescriptor(); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Reads a {@code module-info.class} from the given byte buffer. |
|
99 |
* |
|
100 |
* @throws InvalidModuleDescriptorException |
|
101 |
* @throws UncheckedIOException |
|
102 |
*/ |
|
103 |
public static ModuleDescriptor read(ByteBuffer bb, |
|
104 |
Supplier<Set<String>> pf) |
|
105 |
{ |
|
106 |
try { |
|
107 |
return new ModuleInfo(pf).doRead(new DataInputWrapper(bb)); |
|
108 |
} catch (IllegalArgumentException iae) { |
|
109 |
// IllegalArgumentException means a malformed class |
|
110 |
throw invalidModuleDescriptor(iae.getMessage()); |
|
111 |
} catch (EOFException x) { |
|
112 |
throw truncatedModuleDescriptor(); |
|
113 |
} catch (IOException ioe) { |
|
114 |
throw new UncheckedIOException(ioe); |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Reads a {@code module-info.class} from the given byte buffer |
|
120 |
* but ignore the {@code Hashes} attribute. |
|
121 |
* |
|
122 |
* @throws InvalidModuleDescriptorException |
|
123 |
* @throws UncheckedIOException |
|
124 |
*/ |
|
125 |
static ModuleDescriptor readIgnoringHashes(ByteBuffer bb, |
|
126 |
Supplier<Set<String>> pf) |
|
127 |
{ |
|
128 |
try { |
|
129 |
return new ModuleInfo(pf, false).doRead(new DataInputWrapper(bb)); |
|
130 |
} catch (IllegalArgumentException iae) { |
|
131 |
throw invalidModuleDescriptor(iae.getMessage()); |
|
132 |
} catch (EOFException x) { |
|
133 |
throw truncatedModuleDescriptor(); |
|
134 |
} catch (IOException ioe) { |
|
135 |
throw new UncheckedIOException(ioe); |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Reads the input as a module-info class file. |
|
141 |
* |
|
142 |
* @throws IOException |
|
143 |
* @throws InvalidModuleDescriptorException |
|
144 |
* @throws IllegalArgumentException if thrown by the ModuleDescriptor.Builder |
|
145 |
* because an identifier is not a legal Java identifier, duplicate |
|
146 |
* exports, and many other reasons |
|
147 |
*/ |
|
148 |
private ModuleDescriptor doRead(DataInput in) throws IOException { |
|
149 |
||
150 |
int magic = in.readInt(); |
|
151 |
if (magic != 0xCAFEBABE) |
|
152 |
throw invalidModuleDescriptor("Bad magic number"); |
|
153 |
||
154 |
int minor_version = in.readUnsignedShort(); |
|
155 |
int major_version = in.readUnsignedShort(); |
|
156 |
if (major_version < 53) { |
|
38425 | 157 |
throw invalidModuleDescriptor("Must be >= 53.0"); |
36511 | 158 |
} |
159 |
||
160 |
ConstantPool cpool = new ConstantPool(in); |
|
161 |
||
162 |
int access_flags = in.readUnsignedShort(); |
|
163 |
if (access_flags != ACC_MODULE) |
|
164 |
throw invalidModuleDescriptor("access_flags should be ACC_MODULE"); |
|
165 |
||
166 |
int this_class = in.readUnsignedShort(); |
|
167 |
String mn = cpool.getClassName(this_class); |
|
168 |
int suffix = mn.indexOf("/module-info"); |
|
169 |
if (suffix < 1) |
|
170 |
throw invalidModuleDescriptor("this_class not of form name/module-info"); |
|
171 |
mn = mn.substring(0, suffix).replace('/', '.'); |
|
172 |
builder = new ModuleDescriptor.Builder(mn); |
|
173 |
||
174 |
int super_class = in.readUnsignedShort(); |
|
175 |
if (super_class > 0) |
|
176 |
throw invalidModuleDescriptor("bad #super_class"); |
|
177 |
||
178 |
int interfaces_count = in.readUnsignedShort(); |
|
179 |
if (interfaces_count > 0) |
|
180 |
throw invalidModuleDescriptor("Bad #interfaces"); |
|
181 |
||
182 |
int fields_count = in.readUnsignedShort(); |
|
183 |
if (fields_count > 0) |
|
184 |
throw invalidModuleDescriptor("Bad #fields"); |
|
185 |
||
186 |
int methods_count = in.readUnsignedShort(); |
|
187 |
if (methods_count > 0) |
|
188 |
throw invalidModuleDescriptor("Bad #methods"); |
|
189 |
||
190 |
int attributes_count = in.readUnsignedShort(); |
|
191 |
||
192 |
// the names of the attributes found in the class file |
|
193 |
Set<String> attributes = new HashSet<>(); |
|
194 |
||
195 |
for (int i = 0; i < attributes_count ; i++) { |
|
196 |
int name_index = in.readUnsignedShort(); |
|
197 |
String attribute_name = cpool.getUtf8(name_index); |
|
198 |
int length = in.readInt(); |
|
199 |
||
200 |
boolean added = attributes.add(attribute_name); |
|
201 |
if (!added && isAttributeAtMostOnce(attribute_name)) { |
|
202 |
throw invalidModuleDescriptor("More than one " |
|
203 |
+ attribute_name + " attribute"); |
|
204 |
} |
|
205 |
||
206 |
switch (attribute_name) { |
|
207 |
||
208 |
case MODULE : |
|
209 |
readModuleAttribute(mn, in, cpool); |
|
210 |
break; |
|
211 |
||
212 |
case CONCEALED_PACKAGES : |
|
213 |
readConcealedPackagesAttribute(in, cpool); |
|
214 |
break; |
|
215 |
||
216 |
case VERSION : |
|
217 |
readVersionAttribute(in, cpool); |
|
218 |
break; |
|
219 |
||
220 |
case MAIN_CLASS : |
|
221 |
readMainClassAttribute(in, cpool); |
|
222 |
break; |
|
223 |
||
224 |
case TARGET_PLATFORM : |
|
225 |
readTargetPlatformAttribute(in, cpool); |
|
226 |
break; |
|
227 |
||
228 |
case HASHES : |
|
229 |
if (parseHashes) { |
|
230 |
readHashesAttribute(in, cpool); |
|
231 |
} else { |
|
232 |
in.skipBytes(length); |
|
233 |
} |
|
234 |
break; |
|
235 |
||
236 |
default: |
|
237 |
if (isAttributeDisallowed(attribute_name)) { |
|
238 |
throw invalidModuleDescriptor(attribute_name |
|
239 |
+ " attribute not allowed"); |
|
240 |
} else { |
|
241 |
in.skipBytes(length); |
|
242 |
} |
|
243 |
||
244 |
} |
|
245 |
} |
|
246 |
||
247 |
// the Module attribute is required |
|
248 |
if (!attributes.contains(MODULE)) { |
|
249 |
throw invalidModuleDescriptor(MODULE + " attribute not found"); |
|
250 |
} |
|
251 |
||
252 |
// If the ConcealedPackages attribute is not present then the |
|
253 |
// packageFinder is used to to find any non-exported packages. |
|
254 |
if (!attributes.contains(CONCEALED_PACKAGES) && packageFinder != null) { |
|
255 |
Set<String> pkgs; |
|
256 |
try { |
|
257 |
pkgs = new HashSet<>(packageFinder.get()); |
|
258 |
} catch (UncheckedIOException x) { |
|
259 |
throw x.getCause(); |
|
260 |
} |
|
261 |
pkgs.removeAll(builder.exportedPackages()); |
|
262 |
builder.conceals(pkgs); |
|
263 |
} |
|
264 |
||
265 |
// Was the Synthetic attribute present? |
|
266 |
if (attributes.contains(SYNTHETIC)) |
|
267 |
builder.synthetic(true); |
|
268 |
||
269 |
return builder.build(); |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* Reads the Module attribute. |
|
274 |
*/ |
|
275 |
private void readModuleAttribute(String mn, DataInput in, ConstantPool cpool) |
|
276 |
throws IOException |
|
277 |
{ |
|
278 |
int requires_count = in.readUnsignedShort(); |
|
279 |
if (requires_count == 0 && !mn.equals("java.base")) { |
|
280 |
throw invalidModuleDescriptor("The requires table must have" |
|
281 |
+ " at least one entry"); |
|
282 |
} |
|
283 |
for (int i=0; i<requires_count; i++) { |
|
284 |
int index = in.readUnsignedShort(); |
|
285 |
int flags = in.readUnsignedShort(); |
|
286 |
String dn = cpool.getUtf8(index); |
|
287 |
Set<Modifier> mods; |
|
288 |
if (flags == 0) { |
|
289 |
mods = Collections.emptySet(); |
|
290 |
} else { |
|
291 |
mods = new HashSet<>(); |
|
292 |
if ((flags & ACC_PUBLIC) != 0) |
|
293 |
mods.add(Modifier.PUBLIC); |
|
294 |
if ((flags & ACC_SYNTHETIC) != 0) |
|
295 |
mods.add(Modifier.SYNTHETIC); |
|
296 |
if ((flags & ACC_MANDATED) != 0) |
|
297 |
mods.add(Modifier.MANDATED); |
|
298 |
} |
|
299 |
builder.requires(mods, dn); |
|
300 |
} |
|
301 |
||
302 |
int exports_count = in.readUnsignedShort(); |
|
303 |
if (exports_count > 0) { |
|
304 |
for (int i=0; i<exports_count; i++) { |
|
305 |
int index = in.readUnsignedShort(); |
|
306 |
String pkg = cpool.getUtf8(index).replace('/', '.'); |
|
307 |
int exports_to_count = in.readUnsignedShort(); |
|
308 |
if (exports_to_count > 0) { |
|
309 |
Set<String> targets = new HashSet<>(exports_to_count); |
|
310 |
for (int j=0; j<exports_to_count; j++) { |
|
311 |
int exports_to_index = in.readUnsignedShort(); |
|
312 |
targets.add(cpool.getUtf8(exports_to_index)); |
|
313 |
} |
|
314 |
builder.exports(pkg, targets); |
|
315 |
} else { |
|
316 |
builder.exports(pkg); |
|
317 |
} |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
int uses_count = in.readUnsignedShort(); |
|
322 |
if (uses_count > 0) { |
|
323 |
for (int i=0; i<uses_count; i++) { |
|
324 |
int index = in.readUnsignedShort(); |
|
325 |
String sn = cpool.getClassName(index).replace('/', '.'); |
|
326 |
builder.uses(sn); |
|
327 |
} |
|
328 |
} |
|
329 |
||
330 |
int provides_count = in.readUnsignedShort(); |
|
331 |
if (provides_count > 0) { |
|
332 |
Map<String, Set<String>> pm = new HashMap<>(); |
|
333 |
for (int i=0; i<provides_count; i++) { |
|
334 |
int index = in.readUnsignedShort(); |
|
335 |
int with_index = in.readUnsignedShort(); |
|
336 |
String sn = cpool.getClassName(index).replace('/', '.'); |
|
337 |
String cn = cpool.getClassName(with_index).replace('/', '.'); |
|
338 |
// computeIfAbsent |
|
339 |
Set<String> providers = pm.get(sn); |
|
340 |
if (providers == null) { |
|
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
341 |
providers = new LinkedHashSet<>(); // preserve order |
36511 | 342 |
pm.put(sn, providers); |
343 |
} |
|
344 |
providers.add(cn); |
|
345 |
} |
|
346 |
for (Map.Entry<String, Set<String>> e : pm.entrySet()) { |
|
347 |
builder.provides(e.getKey(), e.getValue()); |
|
348 |
} |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
/** |
|
353 |
* Reads the ConcealedPackages attribute |
|
354 |
*/ |
|
355 |
private void readConcealedPackagesAttribute(DataInput in, ConstantPool cpool) |
|
356 |
throws IOException |
|
357 |
{ |
|
358 |
int package_count = in.readUnsignedShort(); |
|
359 |
for (int i=0; i<package_count; i++) { |
|
360 |
int index = in.readUnsignedShort(); |
|
361 |
String pn = cpool.getUtf8(index).replace('/', '.'); |
|
362 |
builder.conceals(pn); |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Reads the Version attribute |
|
368 |
*/ |
|
369 |
private void readVersionAttribute(DataInput in, ConstantPool cpool) |
|
370 |
throws IOException |
|
371 |
{ |
|
372 |
int index = in.readUnsignedShort(); |
|
373 |
builder.version(cpool.getUtf8(index)); |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Reads the MainClass attribute |
|
378 |
*/ |
|
379 |
private void readMainClassAttribute(DataInput in, ConstantPool cpool) |
|
380 |
throws IOException |
|
381 |
{ |
|
382 |
int index = in.readUnsignedShort(); |
|
383 |
builder.mainClass(cpool.getClassName(index).replace('/', '.')); |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* Reads the TargetPlatform attribute |
|
388 |
*/ |
|
389 |
private void readTargetPlatformAttribute(DataInput in, ConstantPool cpool) |
|
390 |
throws IOException |
|
391 |
{ |
|
392 |
int name_index = in.readUnsignedShort(); |
|
393 |
if (name_index != 0) |
|
394 |
builder.osName(cpool.getUtf8(name_index)); |
|
395 |
||
396 |
int arch_index = in.readUnsignedShort(); |
|
397 |
if (arch_index != 0) |
|
398 |
builder.osArch(cpool.getUtf8(arch_index)); |
|
399 |
||
400 |
int version_index = in.readUnsignedShort(); |
|
401 |
if (version_index != 0) |
|
402 |
builder.osVersion(cpool.getUtf8(version_index)); |
|
403 |
} |
|
404 |
||
405 |
||
406 |
/** |
|
407 |
* Reads the Hashes attribute |
|
408 |
* |
|
409 |
* @apiNote For now the hash is stored in base64 as a UTF-8 string, this |
|
410 |
* should be changed to be an array of u1. |
|
411 |
*/ |
|
412 |
private void readHashesAttribute(DataInput in, ConstantPool cpool) |
|
413 |
throws IOException |
|
414 |
{ |
|
415 |
int index = in.readUnsignedShort(); |
|
416 |
String algorithm = cpool.getUtf8(index); |
|
417 |
||
418 |
int hash_count = in.readUnsignedShort(); |
|
419 |
||
420 |
Map<String, String> map = new HashMap<>(hash_count); |
|
421 |
for (int i=0; i<hash_count; i++) { |
|
422 |
index = in.readUnsignedShort(); |
|
423 |
String dn = cpool.getUtf8(index); |
|
424 |
index = in.readUnsignedShort(); |
|
425 |
String hash = cpool.getUtf8(index); |
|
426 |
map.put(dn, hash); |
|
427 |
} |
|
428 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36511
diff
changeset
|
429 |
builder.hashes(new ModuleHashes(algorithm, map)); |
36511 | 430 |
} |
431 |
||
432 |
||
433 |
/** |
|
434 |
* Returns true if the given attribute can be present at most once |
|
435 |
* in the class file. Returns false otherwise. |
|
436 |
*/ |
|
437 |
private static boolean isAttributeAtMostOnce(String name) { |
|
438 |
||
439 |
if (name.equals(MODULE) || |
|
440 |
name.equals(SOURCE_FILE) || |
|
441 |
name.equals(SDE) || |
|
442 |
name.equals(CONCEALED_PACKAGES) || |
|
443 |
name.equals(VERSION) || |
|
444 |
name.equals(MAIN_CLASS) || |
|
445 |
name.equals(TARGET_PLATFORM) || |
|
446 |
name.equals(HASHES)) |
|
447 |
return true; |
|
448 |
||
449 |
return false; |
|
450 |
} |
|
451 |
||
452 |
/** |
|
453 |
* Return true if the given attribute name is the name of a pre-defined |
|
454 |
* attribute that is not allowed in the class file. |
|
455 |
* |
|
456 |
* Except for Module, InnerClasses, Synthetic, SourceFile, SourceDebugExtension, |
|
457 |
* and Deprecated, none of the pre-defined attributes in JVMS 4.7 may appear. |
|
458 |
*/ |
|
459 |
private static boolean isAttributeDisallowed(String name) { |
|
460 |
Set<String> notAllowed = predefinedNotAllowed; |
|
461 |
if (notAllowed == null) { |
|
462 |
notAllowed = Set.of( |
|
463 |
"ConstantValue", |
|
464 |
"Code", |
|
465 |
"StackMapTable", |
|
466 |
"Exceptions", |
|
467 |
"EnclosingMethod", |
|
468 |
"Signature", |
|
469 |
"LineNumberTable", |
|
470 |
"LocalVariableTable", |
|
471 |
"LocalVariableTypeTable", |
|
472 |
"RuntimeVisibleAnnotations", |
|
473 |
"RuntimeInvisibleAnnotations", |
|
474 |
"RuntimeVisibleParameterAnnotations", |
|
475 |
"RuntimeInvisibleParameterAnnotations", |
|
476 |
"RuntimeVisibleTypeAnnotations", |
|
477 |
"RuntimeInvisibleTypeAnnotations", |
|
478 |
"AnnotationDefault", |
|
479 |
"BootstrapMethods", |
|
480 |
"MethodParameters"); |
|
481 |
predefinedNotAllowed = notAllowed; |
|
482 |
} |
|
483 |
return notAllowed.contains(name); |
|
484 |
} |
|
485 |
||
486 |
// lazily created set the pre-defined attributes that are not allowed |
|
487 |
private static volatile Set<String> predefinedNotAllowed; |
|
488 |
||
489 |
||
490 |
||
491 |
/** |
|
492 |
* The constant pool in a class file. |
|
493 |
*/ |
|
494 |
private static class ConstantPool { |
|
495 |
static final int CONSTANT_Utf8 = 1; |
|
496 |
static final int CONSTANT_Integer = 3; |
|
497 |
static final int CONSTANT_Float = 4; |
|
498 |
static final int CONSTANT_Long = 5; |
|
499 |
static final int CONSTANT_Double = 6; |
|
500 |
static final int CONSTANT_Class = 7; |
|
501 |
static final int CONSTANT_String = 8; |
|
502 |
static final int CONSTANT_Fieldref = 9; |
|
503 |
static final int CONSTANT_Methodref = 10; |
|
504 |
static final int CONSTANT_InterfaceMethodref = 11; |
|
505 |
static final int CONSTANT_NameAndType = 12; |
|
506 |
static final int CONSTANT_MethodHandle = 15; |
|
507 |
static final int CONSTANT_MethodType = 16; |
|
508 |
static final int CONSTANT_InvokeDynamic = 18; |
|
509 |
||
510 |
private static class Entry { |
|
511 |
protected Entry(int tag) { |
|
512 |
this.tag = tag; |
|
513 |
} |
|
514 |
final int tag; |
|
515 |
} |
|
516 |
||
517 |
private static class IndexEntry extends Entry { |
|
518 |
IndexEntry(int tag, int index) { |
|
519 |
super(tag); |
|
520 |
this.index = index; |
|
521 |
} |
|
522 |
final int index; |
|
523 |
} |
|
524 |
||
525 |
private static class Index2Entry extends Entry { |
|
526 |
Index2Entry(int tag, int index1, int index2) { |
|
527 |
super(tag); |
|
528 |
this.index1 = index1; |
|
529 |
this.index2 = index2; |
|
530 |
} |
|
531 |
final int index1, index2; |
|
532 |
} |
|
533 |
||
534 |
private static class ValueEntry extends Entry { |
|
535 |
ValueEntry(int tag, Object value) { |
|
536 |
super(tag); |
|
537 |
this.value = value; |
|
538 |
} |
|
539 |
final Object value; |
|
540 |
} |
|
541 |
||
542 |
final Entry[] pool; |
|
543 |
||
544 |
ConstantPool(DataInput in) throws IOException { |
|
545 |
int count = in.readUnsignedShort(); |
|
546 |
pool = new Entry[count]; |
|
547 |
||
548 |
for (int i = 1; i < count; i++) { |
|
549 |
int tag = in.readUnsignedByte(); |
|
550 |
switch (tag) { |
|
551 |
||
552 |
case CONSTANT_Utf8: |
|
553 |
String svalue = in.readUTF(); |
|
554 |
pool[i] = new ValueEntry(tag, svalue); |
|
555 |
break; |
|
556 |
||
557 |
case CONSTANT_Class: |
|
558 |
case CONSTANT_String: |
|
559 |
int index = in.readUnsignedShort(); |
|
560 |
pool[i] = new IndexEntry(tag, index); |
|
561 |
break; |
|
562 |
||
563 |
case CONSTANT_Double: |
|
564 |
double dvalue = in.readDouble(); |
|
565 |
pool[i] = new ValueEntry(tag, dvalue); |
|
566 |
i++; |
|
567 |
break; |
|
568 |
||
569 |
case CONSTANT_Fieldref: |
|
570 |
case CONSTANT_InterfaceMethodref: |
|
571 |
case CONSTANT_Methodref: |
|
572 |
case CONSTANT_InvokeDynamic: |
|
573 |
case CONSTANT_NameAndType: |
|
574 |
int index1 = in.readUnsignedShort(); |
|
575 |
int index2 = in.readUnsignedShort(); |
|
576 |
pool[i] = new Index2Entry(tag, index1, index2); |
|
577 |
break; |
|
578 |
||
579 |
case CONSTANT_MethodHandle: |
|
580 |
int refKind = in.readUnsignedByte(); |
|
581 |
index = in.readUnsignedShort(); |
|
582 |
pool[i] = new Index2Entry(tag, refKind, index); |
|
583 |
break; |
|
584 |
||
585 |
case CONSTANT_MethodType: |
|
586 |
index = in.readUnsignedShort(); |
|
587 |
pool[i] = new IndexEntry(tag, index); |
|
588 |
break; |
|
589 |
||
590 |
case CONSTANT_Float: |
|
591 |
float fvalue = in.readFloat(); |
|
592 |
pool[i] = new ValueEntry(tag, fvalue); |
|
593 |
break; |
|
594 |
||
595 |
case CONSTANT_Integer: |
|
596 |
int ivalue = in.readInt(); |
|
597 |
pool[i] = new ValueEntry(tag, ivalue); |
|
598 |
break; |
|
599 |
||
600 |
case CONSTANT_Long: |
|
601 |
long lvalue = in.readLong(); |
|
602 |
pool[i] = new ValueEntry(tag, lvalue); |
|
603 |
i++; |
|
604 |
break; |
|
605 |
||
606 |
default: |
|
607 |
throw invalidModuleDescriptor("Bad constant pool entry: " |
|
608 |
+ i); |
|
609 |
} |
|
610 |
} |
|
611 |
} |
|
612 |
||
613 |
String getClassName(int index) { |
|
614 |
checkIndex(index); |
|
615 |
Entry e = pool[index]; |
|
616 |
if (e.tag != CONSTANT_Class) { |
|
617 |
throw invalidModuleDescriptor("CONSTANT_Class expected at entry: " |
|
618 |
+ index); |
|
619 |
} |
|
620 |
return getUtf8(((IndexEntry) e).index); |
|
621 |
} |
|
622 |
||
623 |
String getUtf8(int index) { |
|
624 |
checkIndex(index); |
|
625 |
Entry e = pool[index]; |
|
626 |
if (e.tag != CONSTANT_Utf8) { |
|
627 |
throw invalidModuleDescriptor("CONSTANT_Utf8 expected at entry: " |
|
628 |
+ index); |
|
629 |
} |
|
630 |
return (String) (((ValueEntry) e).value); |
|
631 |
} |
|
632 |
||
633 |
void checkIndex(int index) { |
|
634 |
if (index < 1 || index >= pool.length) |
|
635 |
throw invalidModuleDescriptor("Index into constant pool out of range"); |
|
636 |
} |
|
637 |
} |
|
638 |
||
639 |
/** |
|
640 |
* A DataInput implementation that reads from a ByteBuffer. |
|
641 |
*/ |
|
642 |
private static class DataInputWrapper implements DataInput { |
|
643 |
private final ByteBuffer bb; |
|
644 |
||
645 |
DataInputWrapper(ByteBuffer bb) { |
|
646 |
this.bb = bb; |
|
647 |
} |
|
648 |
||
649 |
@Override |
|
650 |
public void readFully(byte b[]) throws IOException { |
|
651 |
readFully(b, 0, b.length); |
|
652 |
} |
|
653 |
||
654 |
@Override |
|
655 |
public void readFully(byte b[], int off, int len) throws IOException { |
|
656 |
try { |
|
657 |
bb.get(b, off, len); |
|
658 |
} catch (BufferUnderflowException e) { |
|
659 |
throw new EOFException(); |
|
660 |
} |
|
661 |
} |
|
662 |
||
663 |
@Override |
|
664 |
public int skipBytes(int n) { |
|
665 |
int skip = Math.min(n, bb.remaining()); |
|
666 |
bb.position(bb.position() + skip); |
|
667 |
return skip; |
|
668 |
} |
|
669 |
||
670 |
@Override |
|
671 |
public boolean readBoolean() throws IOException { |
|
672 |
try { |
|
673 |
int ch = bb.get(); |
|
674 |
return (ch != 0); |
|
675 |
} catch (BufferUnderflowException e) { |
|
676 |
throw new EOFException(); |
|
677 |
} |
|
678 |
} |
|
679 |
||
680 |
@Override |
|
681 |
public byte readByte() throws IOException { |
|
682 |
try { |
|
683 |
return bb.get(); |
|
684 |
} catch (BufferUnderflowException e) { |
|
685 |
throw new EOFException(); |
|
686 |
} |
|
687 |
} |
|
688 |
||
689 |
@Override |
|
690 |
public int readUnsignedByte() throws IOException { |
|
691 |
try { |
|
692 |
return ((int) bb.get()) & 0xff; |
|
693 |
} catch (BufferUnderflowException e) { |
|
694 |
throw new EOFException(); |
|
695 |
} |
|
696 |
} |
|
697 |
||
698 |
@Override |
|
699 |
public short readShort() throws IOException { |
|
700 |
try { |
|
701 |
return bb.getShort(); |
|
702 |
} catch (BufferUnderflowException e) { |
|
703 |
throw new EOFException(); |
|
704 |
} |
|
705 |
} |
|
706 |
||
707 |
@Override |
|
708 |
public int readUnsignedShort() throws IOException { |
|
709 |
try { |
|
710 |
return ((int) bb.getShort()) & 0xffff; |
|
711 |
} catch (BufferUnderflowException e) { |
|
712 |
throw new EOFException(); |
|
713 |
} |
|
714 |
} |
|
715 |
||
716 |
@Override |
|
717 |
public char readChar() throws IOException { |
|
718 |
try { |
|
719 |
return bb.getChar(); |
|
720 |
} catch (BufferUnderflowException e) { |
|
721 |
throw new EOFException(); |
|
722 |
} |
|
723 |
} |
|
724 |
||
725 |
@Override |
|
726 |
public int readInt() throws IOException { |
|
727 |
try { |
|
728 |
return bb.getInt(); |
|
729 |
} catch (BufferUnderflowException e) { |
|
730 |
throw new EOFException(); |
|
731 |
} |
|
732 |
} |
|
733 |
||
734 |
@Override |
|
735 |
public long readLong() throws IOException { |
|
736 |
try { |
|
737 |
return bb.getLong(); |
|
738 |
} catch (BufferUnderflowException e) { |
|
739 |
throw new EOFException(); |
|
740 |
} |
|
741 |
} |
|
742 |
||
743 |
@Override |
|
744 |
public float readFloat() throws IOException { |
|
745 |
try { |
|
746 |
return bb.getFloat(); |
|
747 |
} catch (BufferUnderflowException e) { |
|
748 |
throw new EOFException(); |
|
749 |
} |
|
750 |
} |
|
751 |
||
752 |
@Override |
|
753 |
public double readDouble() throws IOException { |
|
754 |
try { |
|
755 |
return bb.getDouble(); |
|
756 |
} catch (BufferUnderflowException e) { |
|
757 |
throw new EOFException(); |
|
758 |
} |
|
759 |
} |
|
760 |
||
761 |
@Override |
|
762 |
public String readLine() { |
|
763 |
throw new RuntimeException("not implemented"); |
|
764 |
} |
|
765 |
||
766 |
@Override |
|
767 |
public String readUTF() throws IOException { |
|
768 |
// ### Need to measure the performance and feasibility of using |
|
769 |
// the UTF-8 decoder instead. |
|
770 |
return DataInputStream.readUTF(this); |
|
771 |
} |
|
772 |
} |
|
773 |
||
774 |
/** |
|
775 |
* Returns an InvalidModuleDescriptorException with the given detail |
|
776 |
* message |
|
777 |
*/ |
|
778 |
private static InvalidModuleDescriptorException |
|
779 |
invalidModuleDescriptor(String msg) { |
|
780 |
return new InvalidModuleDescriptorException(msg); |
|
781 |
} |
|
782 |
||
783 |
/** |
|
784 |
* Returns an InvalidModuleDescriptorException with a detail message to |
|
785 |
* indicate that the class file is truncated. |
|
786 |
*/ |
|
787 |
private static InvalidModuleDescriptorException truncatedModuleDescriptor() { |
|
788 |
return invalidModuleDescriptor("Truncated module-info.class"); |
|
789 |
} |
|
790 |
||
791 |
} |