rjvm 0.3.0

Parse JVM class files with Rust
Documentation
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
use crate::bytecode::flags::InnerClassAccessFlags;
use crate::bytecode::pool::ConstantPoolIndex;

#[derive(Debug)]
pub enum Attribute {
    ConstantValue(ConstantValueInfo),
    Code(CodeInfo),
    StackMapTable(StackMapTableInfo),
    Exceptions(ExceptionsInfo),
    InnerClasses(InnerClassesInfo),
    EnclosingMethod(EnclosingMethodInfo),
    Synthetic(SyntheticInfo),
    Signature(SignatureInfo),
    SourceFile(SourceFileInfo),
    SourceDebugExtension(SourceDebugExtensionInfo),
    LineNumberTable(LineNumberTableInfo),
    LocalVariableTable(LocalVariableTableInfo),
    LocalVariableTypeTable(LocalVariableTypeTableInfo),
    Deprecated(DeprecatedInfo),
    RuntimeVisibleAnnotations(RuntimeVisibleAnnotationsInfo),
    RuntimeInvisibleAnnotations(RuntimeInvisibleAnnotationsInfo),
    RuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotationsInfo),
    RuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotationsInfo),
    RuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotationsInfo),
    RuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotationsInfo),
    AnnotationDefault(AnnotationDefaultInfo),
    BootstrapMethods(BootstrapMethodsInfo),
    MethodParameters(MethodParametersInfo),
    Module(ModuleInfo),
    ModulePackages(ModulePackagesInfo),
    ModuleMainClass(ModuleMainClassInfo),
    NestHost(NestHostInfo),
    NestMembers(NestMembersInfo),
    Record(RecordInfo),
    PermittedSubtypes(PermittedSubtypesInfo),
}

impl std::fmt::Display for Attribute {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ConstantValue(_) => write!(f, "ConstantValue"),
            Self::Code(_) => write!(f, "Code"),
            Self::StackMapTable(_) => write!(f, "StackMapTable"),
            Self::Exceptions(_) => write!(f, "Exceptions"),
            Self::InnerClasses(_) => write!(f, "InnerClasses"),
            Self::EnclosingMethod(_) => write!(f, "EnclosingMethod"),
            Self::Synthetic(_) => write!(f, "Synthetic"),
            Self::Signature(_) => write!(f, "Signature"),
            Self::SourceFile(_) => write!(f, "SourceFile"),
            Self::SourceDebugExtension(_) => write!(f, "SourceDebugExtension"),
            Self::LineNumberTable(_) => write!(f, "LineNumberTable"),
            Self::LocalVariableTable(_) => write!(f, "LocalVariableTable"),
            Self::LocalVariableTypeTable(_) => write!(f, "LocalVariableTypeTable"),
            Self::Deprecated(_) => write!(f, "Deprecated"),
            Self::RuntimeVisibleAnnotations(_) => write!(f, "RuntimeVisibleAnnotations"),
            Self::RuntimeInvisibleAnnotations(_) => write!(f, "RuntimeInvisibleAnnotations"),
            Self::RuntimeVisibleParameterAnnotations(_) => {
                write!(f, "RuntimeVisibleParameterAnnotations")
            }
            Self::RuntimeInvisibleParameterAnnotations(_) => {
                write!(f, "RuntimeInvisibleParameterAnnotations")
            }
            Self::RuntimeVisibleTypeAnnotations(_) => write!(f, "RuntimeVisibleTypeAnnotations"),
            Self::RuntimeInvisibleTypeAnnotations(_) => {
                write!(f, "RuntimeInvisibleTypeAnnotations")
            }
            Self::AnnotationDefault(_) => write!(f, "AnnotationDefault"),
            Self::BootstrapMethods(_) => write!(f, "BootstrapMethods"),
            Self::MethodParameters(_) => write!(f, "MethodParameters"),
            Self::Module(_) => write!(f, "Module"),
            Self::ModulePackages(_) => write!(f, "ModulePackages"),
            Self::ModuleMainClass(_) => write!(f, "ModuleMainClass"),
            Self::NestHost(_) => write!(f, "NestHost"),
            Self::NestMembers(_) => write!(f, "NestMembers"),
            Self::Record(_) => write!(f, "Record"),
            Self::PermittedSubtypes(_) => write!(f, "PermittedSubtypes"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ExceptionTableEntry {
    pub start_pc: u16,
    pub end_pc: u16,
    pub handler_pc: u16,
    pub catch_type: ConstantPoolIndex,
}

#[derive(Debug, Clone, PartialEq)]
pub enum VerificationTypeInfo {
    Top,
    Integer,
    Float,
    Double,
    Long,
    Null,
    UninitializedThis,
    Object {
        /// An index into the constant pool for the class of the object
        class: ConstantPoolIndex,
    },
    Uninitialized {
        /// Offset into associated code array of a new instruction
        /// that created the object being stored here.
        offset: u16,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct LineNumberTableEntry {
    pub start_pc: u16,
    pub line_number: u16,
}

#[derive(Debug, Clone, PartialEq)]
pub struct LocalVariableTableEntry {
    pub start_pc: u16,
    pub length: u16,
    pub name_index: ConstantPoolIndex,
    pub descriptor_index: ConstantPoolIndex,
    pub index: ConstantPoolIndex,
}

#[derive(Debug, Clone, PartialEq)]
pub struct LocalVariableTypeTableEntry {
    pub start_pc: u16,
    pub length: u16,
    pub name_index: ConstantPoolIndex,
    pub signature_index: ConstantPoolIndex,
    pub index: ConstantPoolIndex,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Annotation {
    pub type_index: ConstantPoolIndex,
    pub num_element_value_pairs: u16,
    pub element_value_pairs: Vec<ElementValuePair>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ElementValuePair {
    pub element_name_index: ConstantPoolIndex,
    pub value: ElementValue,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ElementTag {
    Byte,
    Char,
    Double,
    Float,
    Int,
    Long,
    Short,
    Boolean,
    String,
    Enum {
        type_name_index: ConstantPoolIndex,
        const_name_index: ConstantPoolIndex,
    },
    Class,
    AnnotationType,
    Array {
        num_values: u16,
        values: Vec<ElementValue>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub enum ElementValue {
    ConstValueIndex(ConstantPoolIndex),
    EnumConstValue {
        type_name_index: ConstantPoolIndex,
        const_name_index: ConstantPoolIndex,
    },
    ClassInfoIndex(ConstantPoolIndex),
    Annotation(Annotation),
    Array {
        num_values: u16,
        values: Vec<ElementValue>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct ParameterAnnotation {
    pub num_annotations: u16,
    pub annotations: Vec<Annotation>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypeAnnotation {
    pub target_type: u8,
    pub target_info: TypeAnnotationTargetInfo,
    pub target_path: TypePath,
    pub type_index: ConstantPoolIndex,
    pub num_element_value_pairs: u16,
    pub element_value_pairs: Vec<ElementValuePair>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypeAnnotationTargetInfo {
    pub target_info: TypeAnnotationTargetInfoType,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TypeAnnotationTargetInfoType {
    TypeParameter {
        type_parameter_index: ConstantPoolIndex,
    },
    SuperType {
        super_type_index: ConstantPoolIndex,
    },
    TypeParameterBound {
        type_parameter_index: ConstantPoolIndex,
        bound_index: ConstantPoolIndex,
    },
    Empty,
    FormalParameter {
        formal_parameter_index: ConstantPoolIndex,
    },
    Throws {
        throws_type_index: ConstantPoolIndex,
    },
    LocalVar {
        table: Vec<LocalVarTargetTableEntry>,
    },
    Catch {
        exception_table_index: ConstantPoolIndex,
    },
    Offset {
        offset: u16,
    },
    TypeArgument {
        offset: u16,
        type_argument_index: ConstantPoolIndex,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct LocalVarTargetTableEntry {
    pub start_pc: u16,
    pub length: u16,
    pub index: ConstantPoolIndex,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypePath {
    pub path_length: u8,
    pub path: Vec<TypePathEntry>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct TypePathEntry {
    pub type_path_kind: u8,
    pub type_argument_index: ConstantPoolIndex,
}

#[derive(Debug, Clone, PartialEq)]
pub enum StackMapFrame {
    SameFrame {
        frame_type: u8,
    },
    SameLocals1StackItemFrame {
        frame_type: u8,
        stack: VerificationTypeInfo,
    },
    SameLocals1StackItemFrameExtended {
        frame_type: u8,
        offset_delta: u16,
        stack: VerificationTypeInfo,
    },
    ChopFrame {
        frame_type: u8,
        offset_delta: u16,
    },
    SameFrameExtended {
        frame_type: u8,
        offset_delta: u16,
    },
    AppendFrame {
        frame_type: u8,
        offset_delta: u16,
        locals: Vec<VerificationTypeInfo>,
    },
    FullFrame {
        frame_type: u8,
        offset_delta: u16,
        number_of_locals: u16,
        locals: Vec<VerificationTypeInfo>,
        number_of_stack_items: u16,
        stack: Vec<VerificationTypeInfo>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct InnerClass {
    pub inner_class_info_index: ConstantPoolIndex,
    pub outer_class_info_index: ConstantPoolIndex,
    pub inner_name_index: ConstantPoolIndex,
    pub inner_class_access_flags: InnerClassAccessFlags,
}

#[derive(Debug, Clone, PartialEq)]
pub struct BootstrapMethod {
    pub bootstrap_method_ref: ConstantPoolIndex,
    pub num_bootstrap_arguments: u16,
    pub bootstrap_arguments: Vec<ConstantPoolIndex>,
}

#[derive(Debug)]
pub struct RecordComponent {
    pub name_index: ConstantPoolIndex,
    pub descriptor_index: ConstantPoolIndex,
    pub attributes_count: u16,
    pub attributes: Vec<Attribute>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MethodParameter {
    pub name_index: ConstantPoolIndex,
    pub access_flags: u16,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Requires {
    pub requires_index: ConstantPoolIndex,
    pub requires_flags: u16,
    pub requires_version_index: ConstantPoolIndex,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Exports {
    pub exports_index: ConstantPoolIndex,
    pub exports_flags: u16,
    pub exports_to_count: u16,
    pub exports_to_index: Vec<ConstantPoolIndex>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Opens {
    pub opens_index: ConstantPoolIndex,
    pub opens_flags: u16,
    pub opens_to_count: u16,
    pub opens_to_index: Vec<ConstantPoolIndex>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Provides {
    pub provides_index: ConstantPoolIndex,
    pub provides_with_count: u16,
    pub provides_with_index: Vec<ConstantPoolIndex>,
}

#[derive(Debug)]
pub struct ConstantValueInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub constantvalue_index: ConstantPoolIndex,
}

#[derive(Debug)]
pub struct CodeInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub max_stack: u16,
    pub max_locals: u16,
    pub code_length: u32,
    pub code: Vec<u8>,
    pub exception_table_length: u16,
    pub exception_table: Vec<ExceptionTableEntry>,
    pub attributes_count: u16,
    pub attributes: Vec<Attribute>,
}

#[derive(Debug)]
pub struct StackMapTableInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub number_of_entries: u16,
    pub entries: Vec<StackMapFrame>,
}

#[derive(Debug)]
pub struct ExceptionsInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub number_of_exceptions: u16,
    pub exception_index_table: Vec<ConstantPoolIndex>,
}

#[derive(Debug)]
pub struct InnerClassesInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub number_of_classes: u16,
    pub classes: Vec<InnerClass>,
}

#[derive(Debug)]
pub struct EnclosingMethodInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub class_index: ConstantPoolIndex,
    pub method_index: ConstantPoolIndex,
}

#[derive(Debug)]
pub struct SyntheticInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
}

#[derive(Debug)]
pub struct SignatureInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub signature_index: ConstantPoolIndex,
}

#[derive(Debug)]
pub struct SourceFileInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub sourcefile_index: ConstantPoolIndex,
}

#[derive(Debug)]
pub struct SourceDebugExtensionInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub debug_extension: Vec<u8>,
}

#[derive(Debug)]
pub struct LineNumberTableInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub line_number_table_length: u16,
    pub line_number_table: Vec<LineNumberTableEntry>,
}

#[derive(Debug)]
pub struct LocalVariableTableInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub local_variable_table_length: u16,
    pub local_variable_table: Vec<LocalVariableTableEntry>,
}

#[derive(Debug)]
pub struct LocalVariableTypeTableInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub local_variable_type_table_length: u16,
    pub local_variable_type_table: Vec<LocalVariableTypeTableEntry>,
}

#[derive(Debug)]
pub struct DeprecatedInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
}

#[derive(Debug)]
pub struct RuntimeVisibleAnnotationsInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub num_annotations: u16,
    pub annotations: Vec<Annotation>,
}

#[derive(Debug)]
pub struct RuntimeInvisibleAnnotationsInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub num_annotations: u16,
    pub annotations: Vec<Annotation>,
}

#[derive(Debug)]
pub struct RuntimeVisibleParameterAnnotationsInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub num_parameters: u8,
    pub parameter_annotations: Vec<ParameterAnnotation>,
}

#[derive(Debug)]
pub struct RuntimeInvisibleParameterAnnotationsInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub num_parameters: u8,
    pub parameter_annotations: Vec<ParameterAnnotation>,
}

#[derive(Debug)]
pub struct RuntimeVisibleTypeAnnotationsInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub num_annotations: u16,
    pub annotations: Vec<TypeAnnotation>,
}

#[derive(Debug)]
pub struct RuntimeInvisibleTypeAnnotationsInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub num_annotations: u16,
    pub annotations: Vec<TypeAnnotation>,
}

#[derive(Debug)]
pub struct AnnotationDefaultInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub default_value: ElementValue,
}

#[derive(Debug)]
pub struct BootstrapMethodsInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub num_bootstrap_methods: u16,
    pub bootstrap_methods: Vec<BootstrapMethod>,
}

#[derive(Debug)]
pub struct MethodParametersInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub parameters_count: u8,
    pub parameters: Vec<MethodParameter>,
}

#[derive(Debug)]
pub struct ModuleInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub module_name_index: ConstantPoolIndex,
    pub module_flags: u16,
    pub module_version_index: ConstantPoolIndex,
    pub requires_count: u16,
    pub requires: Vec<Requires>,
    pub exports_count: u16,
    pub exports: Vec<Exports>,
    pub opens_count: u16,
    pub opens: Vec<Opens>,
    pub uses_count: u16,
    pub uses_index: Vec<ConstantPoolIndex>,
    pub provides_count: u16,
    pub provides: Vec<Provides>,
}

#[derive(Debug)]
pub struct ModulePackagesInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub package_count: u16,
    pub package_index: Vec<ConstantPoolIndex>,
}

#[derive(Debug)]
pub struct ModuleMainClassInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub main_class_index: ConstantPoolIndex,
}

#[derive(Debug)]
pub struct NestHostInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub host_class_index: ConstantPoolIndex,
}

#[derive(Debug)]
pub struct NestMembersInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub number_of_classes: u16,
    pub classes: Vec<ConstantPoolIndex>,
}

#[derive(Debug)]
pub struct RecordInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub component_count: u16,
    pub components: Vec<Attribute>,
}

#[derive(Debug)]
pub struct PermittedSubtypesInfo {
    pub attribute_name_index: ConstantPoolIndex,
    pub attribute_length: u32,
    pub number_of_classes: u16,
    pub classes: Vec<String>,
}