Repository navigation
Expand file tree
/
Copy pathjava-class-reader.js
More file actions
870 lines (720 loc) · 29 KB
/
Copy pathjava-class-reader.js
File metadata and controls
870 lines (720 loc) · 29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/*!
* https://github.com/leonardosnt/java-class-tools
*
* Copyright (C) 2017 leonardosnt
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/
import ByteBuffer from 'bytebuffer';
import ConstantType from './constant-type';
/**
* All objects (structure) follow Jvm8 specification.
*
* @see https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html
*/
class JavaClassFileReader {
/**
* Read and parse class file contents.
*
* @param {(Uint8Array|Buffer|ArrayBuffer)} source - Class file contents.
* @return {ClassFile}
* @see {@link https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.1}
*/
read(source) {
if (source == undefined) {
throw TypeError('source cannot be null or undefined');
}
if (typeof source === 'string') {
return this.readFromFile(source);
}
this.buf = ByteBuffer.wrap(source);
this.classFile = new (function ClassFile(){});
// Read magic
if (this.buf.readUint32() !== 0xCAFEBABE) {
throw Error('Invalid MAGIC value');
}
this.classFile.minor_version = this.buf.readUint16();
this.classFile.major_version = this.buf.readUint16();
this.classFile.constant_pool_count = this.buf.readUint16();
this.classFile.constant_pool = this._readConstantPool(this.classFile.constant_pool_count - 1);
this.classFile.access_flags = this.buf.readUint16();
this.classFile.this_class = this.buf.readUint16();
this.classFile.super_class = this.buf.readUint16();
this.classFile.interfaces_count = this.buf.readUint16();
this.classFile.interfaces = this._readInterfaces(this.classFile.interfaces_count);
this.classFile.fields_count = this.buf.readUint16();
this.classFile.fields = this._readMemberInfoArray(this.classFile.fields_count);
this.classFile.methods_count = this.buf.readUint16();
this.classFile.methods = this._readMemberInfoArray(this.classFile.methods_count);
this.classFile.attributes_count = this.buf.readUint16();
this.classFile.attributes = this._readAttributeInfoArray(this.classFile.attributes_count);
const classFile = this.classFile;
// Dispose
delete this.buf;
delete this.classFile;
return classFile;
}
readFromFile(path) {
if (typeof process !== undefined) {
const fs = require('fs');
return this.read(fs.readFileSync(path));
} else {
throw Error('readFromFile is not supported in the browser.');
}
}
/**
* Read an array of field_info or method_info structures.
*
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.6
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.5
*/
_readMemberInfoArray(count) {
const members = new Array(count);
for (let i = 0; i < count; i++) {
const memberInfo = {
access_flags: this.buf.readUint16(),
name_index: this.buf.readUint16(),
descriptor_index: this.buf.readUint16(),
attributes_count: this.buf.readUint16()
};
memberInfo.attributes = this._readAttributeInfoArray(memberInfo.attributes_count);
members[i] = memberInfo;
}
return members;
}
_readAttributeInfoArray(attributes_count) {
const attributes = new Array(attributes_count);
for (let i = 0; i < attributes_count; i++) {
attributes[i] = this._readAttributeInfo();
}
return attributes;
}
/**
* Reads the "verification_type_info" structure.
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.4
*/
_readVerificationTypeInfo() {
const type_info = {
tag: this.buf.readUint8()
};
if (type_info.tag === 7) {
type_info.cpool_index = this.buf.readUint16();
} else if (type_info.tag === 8) {
type_info.offset = this.buf.readUint16();
}
return type_info;
}
/**
* Reads the "type_annotation" structure
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.20
*/
_readTypeAnnotation() {
const type_annotation = {
target_type: this.buf.readUint8(),
target_info: {}
};
// Reads the "target_info" union.
// https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.20-400
switch (type_annotation.target_type) {
// type_parameter_target
case 0x00: case 0x01:
type_annotation.target_info.type_parameter_index = this.buf.readUint8();
break;
// supertype_target
case 0x10:
type_annotation.target_info.supertype_index = this.buf.readUint16();
break;
// type_parameter_bound_target
case 0x11: case 0x12:
type_annotation.target_info.type_parameter_index = this.buf.readUint8();
type_annotation.target_info.bound_index = this.buf.readUint8();
break;
// empty_target
case 0x13: case 0x14: case 0x15:
// empty
break;
// formal_parameter_target
case 0x16:
type_annotation.target_info.formal_parameter_index = this.buf.readUint8();
break;
// throws_target
case 0x17:
type_annotation.target_info.throws_type_index = this.buf.readUint16();
break;
// localvar_target
case 0x40: case 0x41: {
type_annotation.target_info.table_length = this.buf.readUint16();
type_annotation.target_info.table = new Array(type_annotation.target_info.table_length);
for (let i = 0; i < type_annotation.target_info.table_length; i++) {
const table_entry = {
start_pc: this.buf.readUint16(),
length: this.buf.readUint16(),
index: this.buf.readUint16()
};
type_annotation.target_info.table[i] = table_entry;
}
break;
}
// catch_target
case 0x42:
type_annotation.target_info.exception_table_index = this.buf.readUint16();
break;
// offset_target
case 0x43: case 0x44: case 0x45: case 0x46:
type_annotation.target_info.offset = this.buf.readUint16();
break;
// type_argument_target
case 0x47: case 0x48: case 0x49: case 0x4A: case 0x4B:
type_annotation.target_info.offset = this.buf.readUint16();
type_annotation.target_info.type_argument_index = this.buf.readUint8();
break;
default:
throw Error(`Unexpected target_type: ${type_annotation.target_type}`);
}
// Reads "type_path" structure
type_annotation.type_path = { path_length: this.buf.readUint8() };
type_annotation.type_path.path = new Array(type_annotation.type_path.path_length);
for (let i = 0; i < type_annotation.type_path.path_length; i++) {
type_annotation.type_path.path[i] = {
type_path_kind: this.buf.readUint8(),
type_argument_index: this.buf.readUint8()
};
}
type_annotation.type_index = this.buf.readUint16();
type_annotation.num_element_value_pairs = this.buf.readUint16();
type_annotation.element_value_pairs = new Array(type_annotation.num_element_value_pairs);
for (let i = 0; i < type_annotation.num_element_value_pairs; i++) {
type_annotation.element_value_pairs[i] = {
element_name_index: this.buf.readUint16(),
element_value: this._readElementValue()
};
}
return type_annotation;
}
/**
* Reads the "attribute_info" structure
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7
*/
_readAttributeInfo() {
const attribute = {
attribute_name_index: this.buf.readUint16(),
attribute_length: this.buf.readUint32()
};
const attributeNameBytes = this.classFile.constant_pool[attribute.attribute_name_index].bytes;
const attributeName = String.fromCharCode.apply(null, attributeNameBytes);
switch (attributeName) {
case 'Deprecated':
case 'Synthetic':
break;
case 'RuntimeInvisibleAnnotations':
case 'RuntimeVisibleAnnotations': {
attribute.num_annotations = this.buf.readUint16();
attribute.annotations = new Array(attribute.num_annotations);
for (let i = 0; i < attribute.num_annotations; i++) {
attribute.annotations[i] = this._readAttributeAnnotation();
}
break;
}
case 'InnerClasses': {
attribute.number_of_classes = this.buf.readUint16();
attribute.classes = new Array(attribute.number_of_classes);
for (let i = 0; i < attribute.number_of_classes; i++) {
attribute.classes[i] = {
inner_class_info_index: this.buf.readUint16(),
outer_class_info_index: this.buf.readUint16(),
inner_name_index: this.buf.readUint16(),
inner_class_access_flags: this.buf.readUint16()
};
}
break;
}
case 'LocalVariableTable': {
attribute.local_variable_table_length = this.buf.readUint16();
attribute.local_variable_table = new Array(attribute.local_variable_table_length);
for (let i = 0; i < attribute.local_variable_table_length; i++) {
attribute.local_variable_table[i] = {
start_pc: this.buf.readUint16(),
length: this.buf.readUint16(),
name_index: this.buf.readUint16(),
descriptor_index: this.buf.readUint16(),
index: this.buf.readUint16()
};
}
break;
}
case 'LocalVariableTypeTable': {
attribute.local_variable_type_table_length = this.buf.readUint16();
attribute.local_variable_type_table = new Array(attribute.local_variable_type_table_length);
for (let i = 0; i < attribute.local_variable_type_table_length; i++) {
attribute.local_variable_type_table[i] = {
start_pc: this.buf.readUint16(),
length: this.buf.readUint16(),
name_index: this.buf.readUint16(),
signature_index: this.buf.readUint16(),
index: this.buf.readUint16(),
};
}
break;
}
case 'RuntimeInvisibleParameterAnnotations':
case 'RuntimeVisibleParameterAnnotations': {
attribute.num_parameters = this.buf.readUint8();
attribute.parameter_annotations = new Array(attribute.num_parameters);
for (let parameterIndex = 0; parameterIndex < attribute.num_parameters; parameterIndex++) {
const parameter_annotation = { num_annotations: this.buf.readUint16()};
parameter_annotation.annotations = new Array(parameter_annotation.num_annotations);
for (let annotationIndex = 0; annotationIndex < parameter_annotation.num_annotations; annotationIndex++) {
parameter_annotation.annotations[annotationIndex] = this._readAttributeAnnotation();
}
attribute.parameter_annotations[parameterIndex] = parameter_annotation;
}
break;
}
case 'BootstrapMethods': {
attribute.num_bootstrap_methods = this.buf.readUint16();
attribute.bootstrap_methods = new Array(attribute.num_bootstrap_methods);
for (let bootstrapMethodIndex = 0; bootstrapMethodIndex < attribute.num_bootstrap_methods; bootstrapMethodIndex++) {
const bootstrap_method = {
bootstrap_method_ref: this.buf.readUint16(),
num_bootstrap_arguments: this.buf.readUint16()
};
bootstrap_method.bootstrap_arguments = new Array(bootstrap_method.num_bootstrap_arguments);
for (let bootstrapArgumentIndex = 0; bootstrapArgumentIndex < bootstrap_method.num_bootstrap_arguments; bootstrapArgumentIndex++) {
bootstrap_method.bootstrap_arguments[bootstrapArgumentIndex] = this.buf.readUint16();
}
attribute.bootstrap_methods[bootstrapMethodIndex] = bootstrap_method;
}
break;
}
case 'RuntimeInvisibleTypeAnnotations':
case 'RuntimeVisibleTypeAnnotations': {
attribute.num_annotations = this.buf.readUint16();
attribute.annotations = new Array(attribute.num_annotations);
for (let i = 0; i < attribute.num_annotations; i++) {
attribute.annotations[i] = this._readTypeAnnotation();
}
break;
}
case 'SourceDebugExtension': {
attribute.debug_extension = new Array(attribute.attribute_length);
for (let i = 0; i < attribute.attribute_length; i++) {
attribute.debug_extension[i] = this.buf.readUint8();
}
break;
}
case 'SourceFile':
attribute.sourcefile_index = this.buf.readUint16();
break;
case 'EnclosingMethod':
attribute.class_index = this.buf.readUint16();
attribute.method_index = this.buf.readUint16();
break;
case 'AnnotationDefault':
attribute.default_value = this._readElementValue();
break;
case 'MethodParameters': {
attribute.parameters_count = this.buf.readUint8();
attribute.parameters = new Array(attribute.parameters_count);
for (let i = 0; i < attribute.parameters_count; i++) {
attribute.parameters[i] = {
name_index: this.buf.readUint16(),
access_flags: this.buf.readUint16()
};
}
break;
}
case 'ConstantValue':
attribute.constantvalue_index = this.buf.readUint16();
break;
case 'Signature':
attribute.signature_index = this.buf.readUint16();
break;
case 'StackMap': return this._readStackMapAttribute(attribute);
case 'Exceptions': return this._readExceptionsAttribute(attribute);
case 'StackMapTable': return this._readStackMapTableAttribute(attribute);
case 'Code': return this._readCodeAttribute(attribute);
case 'LineNumberTable': return this._readLineNumberTableAttribute(attribute);
case 'Module': return this._readModuleAttribute(attribute);
case 'ModulePackages': return this._readModulePackagesAttribute(attribute);
case 'ModuleMainClass':
attribute.main_class_index = this.buf.readUint16();
break;
case 'NestHost':
attribute.host_class_index = this.buf.readUint16();
break;
case 'NestMembers':
attribute.number_of_classes = this.buf.readUint16();
attribute.classes = new Array(attribute.number_of_classes);
for (let i = 0; i < attribute.number_of_classes; i++) {
attribute.classes[i] = this.buf.readUint16();
}
break;
// Unknown attributes
// See: https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.7.1
default: {
attribute.info = new Array(attribute.attribute_length);
for (let i = 0; i < attribute.attribute_length; i++) {
attribute.info[i] = this.buf.readUint8();
}
}
}
return attribute;
}
_readCodeAttribute(attribute) {
attribute.max_stack = this.buf.readUint16();
attribute.max_locals = this.buf.readUint16();
attribute.code_length = this.buf.readUint32();
attribute.code = new Array(attribute.code_length);
// Reads "code" array
for (let i = 0; i < attribute.code_length; i++) {
attribute.code[i] = this.buf.readUint8();
}
attribute.exception_table_length = this.buf.readUint16();
attribute.exception_table = new Array(attribute.exception_table_length);
// Reads exception_table
for (let i = 0; i < attribute.exception_table_length; i++) {
attribute.exception_table[i] = {
start_pc: this.buf.readUint16(),
end_pc: this.buf.readUint16(),
handler_pc: this.buf.readUint16(),
catch_type: this.buf.readUint16()
};
}
attribute.attributes_count = this.buf.readUint16();
attribute.attributes = this._readAttributeInfoArray(attribute.attributes_count);
return attribute;
}
_readLineNumberTableAttribute(attribute) {
attribute.line_number_table_length = this.buf.readUint16();
attribute.line_number_table = new Array(attribute.line_number_table_length);
for (let i = 0; i < attribute.line_number_table_length; i++) {
attribute.line_number_table[i] = {
start_pc: this.buf.readUint16(),
line_number: this.buf.readUint16()
};
}
return attribute;
}
// TODO: this function is being deoptimized one time... Check why
_readStackMapTableAttribute(attribute) {
attribute.number_of_entries = this.buf.readUint16();
attribute.entries = new Array(attribute.number_of_entries);
for (let entryIndex = 0; entryIndex < attribute.number_of_entries; entryIndex++) {
const stack_map_frame = {
frame_type: this.buf.readUint8()
};
// Shorthand
const frame_type = stack_map_frame.frame_type;
/**
* offset_delta's that are "constant" are omitted.
* E.g:
* The offset_delta for the "same_frame" is the value of the tag item (frame_type).
* The offset_delta for the "same_locals_1_stack_item_frame " is given by the formula frame_type - 64.
*
* See https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.4
* for more information.
*/
// SAME
if (frame_type >= 0 && frame_type <= 63) {
attribute.entries[entryIndex] = stack_map_frame;
continue;
}
// SAME_LOCALS_1_STACK_ITEM
if (frame_type >= 64 && frame_type <= 127) {
stack_map_frame.stack = [this._readVerificationTypeInfo()];
}
// SAME_LOCALS_1_STACK_ITEM_EXTENDED
else if (stack_map_frame.frame_type === 247) {
stack_map_frame.offset_delta = this.buf.readUint16();
stack_map_frame.stack = [this._readVerificationTypeInfo()];
}
// CHOP = 248-250, SAME_FRAME_EXTENDED = 251
else if (frame_type >= 248 && frame_type <= 251) {
stack_map_frame.offset_delta = this.buf.readUint16();
}
// APPEND
else if (frame_type >= 252 && frame_type <= 254) {
const numberOfLocals = frame_type - 251;
stack_map_frame.offset_delta = this.buf.readUint16();
stack_map_frame.locals = new Array(numberOfLocals);
for (let i = 0; i < numberOfLocals; i++) {
stack_map_frame.locals[i] = this._readVerificationTypeInfo();
}
}
// FULL_FRAME
else if (frame_type === 255) {
stack_map_frame.offset_delta = this.buf.readUint16();
stack_map_frame.number_of_locals = this.buf.readUint16();
stack_map_frame.locals = new Array(stack_map_frame.number_of_locals);
for (let i = 0; i < stack_map_frame.number_of_locals; i++) {
stack_map_frame.locals[i] = this._readVerificationTypeInfo();
}
stack_map_frame.number_of_stack_items = this.buf.readUint16();
stack_map_frame.stack = new Array(stack_map_frame.number_of_stack_items);
for (let i = 0; i < stack_map_frame.number_of_stack_items; i++) {
stack_map_frame.stack[i] = this._readVerificationTypeInfo();
}
}
attribute.entries[entryIndex] = stack_map_frame;
}
return attribute;
}
_readExceptionsAttribute(attribute) {
attribute.number_of_exceptions = this.buf.readUint16();
attribute.exception_index_table = new Array(attribute.number_of_exceptions);
for (let i = 0; i < attribute.number_of_exceptions; i++) {
attribute.exception_index_table[i] = this.buf.readUint16();
}
return attribute;
}
/**
* http://download.oracle.com/otndocs/jcp/7247-j2me_cldc-1.1-fr-spec-oth-JSpec/
*
* Appendix1-verifier.pdf at "2.1 Stack map format"
*
* "According to the CLDC specification, the sizes of some fields are not 16bit
* but 32bit if the code size is more than 64K or the number of the local variables
* is more than 64K. However, for the J2ME CLDC technology, they are always 16bit.
* The implementation of the StackMap class assumes they are 16bit." - javaassist
*/
_readStackMapAttribute(attribute) {
attribute.number_of_entries = this.buf.readUint16();
attribute.entries = new Array(attribute.number_of_entries);
for (let entryIndex = 0; entryIndex < attribute.number_of_entries; entryIndex++) {
const stack_map_frame = { offset: this.buf.readUint16() };
// Read locals
stack_map_frame.number_of_locals = this.buf.readUint16();
stack_map_frame.locals = new Array(stack_map_frame.number_of_locals);
for (let i = 0; i < stack_map_frame.number_of_locals; i++) {
stack_map_frame.locals[i] = this._readVerificationTypeInfo();
}
// Read stack
stack_map_frame.number_of_stack_items = this.buf.readUint16();
stack_map_frame.stack = new Array(stack_map_frame.number_of_stack_items);
for (let i = 0; i < stack_map_frame.number_of_stack_items; i++) {
stack_map_frame.stack[i] = this._readVerificationTypeInfo();
}
attribute.entries[entryIndex] = stack_map_frame;
}
return attribute;
}
_readModuleAttribute(attribute) {
attribute.module_name_index = this.buf.readUint16();
attribute.module_flags = this.buf.readUint16();
attribute.module_version_index = this.buf.readUint16();
attribute.requires_count = this.buf.readUint16();
attribute.requires = new Array(attribute.requires_count);
for (let i = 0; i < attribute.requires_count; i++) {
attribute.requires[i] = {
requires_index: this.buf.readUint16(),
requires_flags: this.buf.readUint16(),
requires_version_index: this.buf.readUint16()
};
}
attribute.exports_count = this.buf.readUint16();
attribute.exports = new Array(attribute.exports_count);
for (let exportIndex = 0; exportIndex < attribute.exports_count; exportIndex++) {
const exportEntry = {
exports_index: this.buf.readUint16(),
exports_flags: this.buf.readUint16(),
exports_to_count: this.buf.readUint16()
};
exportEntry.exports_to_index = new Array(exportEntry.exports_to_count);
for (let exportsToIndex = 0; exportsToIndex < exportEntry.exports_to_count; exportsToIndex++) {
exportEntry.exports_to_index[exportsToIndex] = this.buf.readUint16();
}
attribute.exports[exportIndex] = exportEntry;
}
attribute.opens_count = this.buf.readUint16();
attribute.opens = new Array(attribute.opens_count);
for (let openIndex = 0; openIndex < attribute.opens_count; openIndex++) {
const openEntry = {
opens_index: this.buf.readUint16(),
opens_flags: this.buf.readUint16(),
opens_to_count: this.buf.readUint16()
};
openEntry.opens_to_index = new Array(openEntry.opens_to_count);
for (let opensToIndex = 0; opensToIndex < openEntry.opens_to_count; opensToIndex++) {
openEntry.opens_to_index[opensToIndex] = this.buf.readUint16();
}
attribute.opens[openIndex] = openEntry;
}
attribute.uses_count = this.buf.readUint16();
attribute.uses_index = new Array(attribute.uses_count);;
for (let i = 0; i < attribute.uses_count; i++) {
attribute.uses_index[i] = this.buf.readUint16();
}
attribute.provides_count = this.buf.readUint16();
attribute.provides = new Array(attribute.provides_count);
for (let providesIndex = 0; providesIndex < attribute.provides_count; providesIndex++) {
const provideEntry = {
provides_index: this.buf.readUint16(),
provides_with_count: this.buf.readUint16()
};
provideEntry.provides_with_index = new Array(provideEntry.provides_with_count);
for (let providesWithIndex = 0; providesWithIndex < provideEntry.provides_with_count; providesWithIndex++) {
provideEntry.provides_with_index[providesWithIndex] = this.buf.readUint16();
}
attribute.provides[providesIndex] = provideEntry;
}
return attribute;
}
_readModulePackagesAttribute(attribute) {
attribute.package_count = this.buf.readUint16();
attribute.package_index = new Array(attribute.package_count);
for (let i = 0; i < attribute.package_count; i++) {
attribute.package_index[i] = this.buf.readUint16();
}
return attribute;
}
/**
* Reads the "annotation" structure
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.16
*/
_readAttributeAnnotation() {
const annotation = {
type_index: this.buf.readUint16(),
num_element_value_pairs: this.buf.readUint16()
};
annotation.element_value_pairs = new Array(annotation.num_element_value_pairs);
for (let i = 0; i < annotation.num_element_value_pairs; i++) {
annotation.element_value_pairs[i] = {
element_name_index: this.buf.readUint16(),
element_value: this._readElementValue()
};
}
return annotation;
}
/**
* Reads the "element_value" structure
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.16.1
*/
_readElementValue() {
const element_value = {
tag: this.buf.readUint8(),
value: {}
};
/**
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.16.1-130
*/
switch (element_value.tag) {
case 101: // e
element_value.value.enum_const_value = {
type_name_index: this.buf.readUint16(),
const_name_index: this.buf.readUint16()
};
break;
case 99: // c
element_value.value.class_info_index = this.buf.readUint16();
break;
case 91: { // [
const num_values = this.buf.readUint16();
const values = new Array(num_values);
for (let i = 0; i < num_values; i++) {
values[i] = this._readElementValue();
}
element_value.value.array_value = {
num_values,
values
};
break;
}
case 64: // @
element_value.value.annotation = this._readAttributeAnnotation();
break;
case 66: // B
case 67: // C
case 68: // D
case 70: // F
case 73: // I
case 74: // J
case 83: // S
case 90: // Z
case 115:// s
element_value.value.const_value_index = this.buf.readUint16();
break;
default:
throw Error(`Unexpected tag: ${element_value.tag}`);
}
return element_value;
}
_readInterfaces(interfaceCount) {
const interfaces = new Array(interfaceCount);
for (let i = 0; i < interfaceCount; i++) {
interfaces[i] = this.buf.readUint16();
}
return interfaces;
}
/**
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4
*/
_readConstantPool(poolCount) {
/**
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.1
* The constant_pool table is indexed from 1 to constant_pool_count-1.
*/
const pool = new Array(poolCount);
for (let i = 1; i <= poolCount; i++) {
const entry = this._readConstantPoolEntry();
pool[i] = entry;
/**
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.5
*
* All 8-byte constants take up two entries in the constant_pool table of the class file.
* If a CONSTANT_Long_info or CONSTANT_Double_info structure is the item in the constant_pool table at index n,
* then the next usable item in the pool is located at index n+2.
* The constant_pool index n+1 must be valid but is considered unusable.
*/
if (entry.tag === ConstantType.LONG || entry.tag === ConstantType.DOUBLE) {
pool[++i] = undefined;
}
}
return pool;
}
_readUtf8PoolEntry(tag) {
const length = this.buf.readUint16();
const bytes = new Array(length);
for (let i = 0; i < length; i++) {
bytes[i] = this.buf.readUint8();
}
return { tag, length, bytes };
}
_readConstantPoolEntry() {
const tag = this.buf.readUint8();
switch (tag) {
case ConstantType.UTF8:
return this._readUtf8PoolEntry(tag);
case ConstantType.INTEGER:
case ConstantType.FLOAT:
return { tag, bytes: this.buf.readUint32() };
case ConstantType.LONG:
case ConstantType.DOUBLE:
return { tag, high_bytes: this.buf.readUint32(), low_bytes: this.buf.readUint32() };
case ConstantType.PACKAGE:
case ConstantType.MODULE:
case ConstantType.CLASS:
return { tag, name_index: this.buf.readUint16() };
case ConstantType.STRING:
return { tag, string_index: this.buf.readUint16() };
/**
* Fields, methods, and interface methods are represented by similar structures
* https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.2
*/
case ConstantType.FIELDREF:
case ConstantType.METHODREF:
case ConstantType.INTERFACE_METHODREF:
return { tag, class_index: this.buf.readUint16(), name_and_type_index: this.buf.readUint16() };
case ConstantType.NAME_AND_TYPE:
return { tag, name_index: this.buf.readUint16(), descriptor_index: this.buf.readUint16() };
case ConstantType.METHOD_HANDLE:
return { tag, reference_kind: this.buf.readUint8(), reference_index: this.buf.readUint16() };
case ConstantType.METHOD_TYPE:
return { tag, descriptor_index: this.buf.readUint16() };
case ConstantType.DYNAMIC:
case ConstantType.INVOKE_DYNAMIC:
return { tag, bootstrap_method_attr_index: this.buf.readUint16(), name_and_type_index: this.buf.readUint16() };
default:
throw Error(`Unexpected tag: ${tag}`);
}
}
}
export default JavaClassFileReader;