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
//! The tree package provides the basic structure of a basic class file

use std::collections::BTreeMap;

/// The first 4 bytes of every java class file
pub const MAGIC: &[u8] = &[0xCA, 0xFE, 0xBA, 0xBE];

/// A java class file.
#[derive(Debug)]
pub struct Class {
    pub minor_version: u16,
    pub major_version: u16,

    pub access_flags: AccessFlags,
    pub name: u16,
    pub super_name: u16,
    pub interfaces: Vec<u16>,

    pub fields: Vec<Field>,
    pub methods: Vec<Method>,

    pub attributes: Vec<Attribute>,
}

/// A field.
#[derive(Debug)]
pub struct Field {
    pub access_flags: AccessFlags,
    pub name: u16,
    pub desc: u16,
    pub attributes: Vec<Attribute>,
}

/// A method.
#[derive(Debug)]
pub struct Method {
    pub access_flags: AccessFlags,
    pub name: u16,
    pub desc: u16,
    pub attributes: Vec<Attribute>,
}

/// An Attribute.
#[derive(Debug)]
pub enum Attribute {
    AnnotationDefault(ElementValue),
    BootstrapMethods(Vec<BootstrapMethod>),
    Code {
        max_stack: u16,
        max_locals: u16,
        instructions: Vec<Option<Instruction>>,
        exceptions: Vec<Exception>,
        attributes: Vec<Attribute>,
    },
    ConstantValue(u16),
    Deprecated,
    EnclosingMethod {
        class_index: u16,
        method_index: u16,
    },
    Exceptions(Vec<u16>),
    InnerClasses(Vec<InnerClass>),
    LineNumberTable(Vec<LineNumber>),
    LocalVariableTable(Vec<LocalVariable>),
    LocalVariableTypeTable(Vec<LocalVariableType>),
    MethodParameters(Vec<MethodParameter>),
    Module {
        name: u16,
        flags: AccessFlags,
        version: u16,

        requires: Vec<Requirement>,
        exports: Vec<Export>,
        opens: Vec<Opening>,
        uses: Vec<u16>,
        provides: Vec<Provider>,
    },
    ModuleMainClass(u16),
    ModulePackages(Vec<u16>),
    RuntimeVisibleAnnotations(Vec<Annotation>),
    RuntimeInvisibleAnnotations(Vec<Annotation>),
    RuntimeVisibleParameterAnnotations(Vec<Vec<Annotation>>),
    RuntimeInvisibleParameterAnnotations(Vec<Vec<Annotation>>),
    RuntimeVisibleTypeAnnotations(Vec<TypeAnnotation>),
    RuntimeInvisibleTypeAnnotations(Vec<TypeAnnotation>),
    Signature(u16),
    Synthetic,
    SourceFile(u16),
    SourceDebugExtension(String),
    StackMapTable(Vec<StackMapFrame>),
    Unknown(u16, Vec<u8>),
}

bitflags! {
    /// The access flags of a part of the class
    pub struct AccessFlags: u16 {
        const PUBLIC       = 0b0000_0000_0000_0001;
        const PRIVATE      = 0b0000_0000_0000_0010;
        const PROTECTED    = 0b0000_0000_0000_0100;
        const STATIC       = 0b0000_0000_0000_1000;
        const FINAL        = 0b0000_0000_0001_0000;
        const SUPER        = 0b0000_0000_0010_0000;
        const SYNCHRONIZED = 0b0000_0000_0010_0000;
        const VOLATILE     = 0b0000_0000_0100_0000;
        const BRIDGE       = 0b0000_0000_0100_0000;
        const STATIC_PHASE = 0b0000_0000_0100_0000;
        const TRANSIENT    = 0b0000_0000_1000_0000;
        const VARARGS      = 0b0000_0000_1000_0000;
        const NATIVE       = 0b0000_0001_0000_0000;
        const INTERFACE    = 0b0000_0010_0000_0000;
        const ABSTRACT     = 0b0000_0100_0000_0000;
        const STRICT       = 0b0000_1000_0000_0000;
        const SYNTHETIC    = 0b0001_0000_0000_0000;
        const ANNOTATION   = 0b0010_0000_0000_0000;
        const ENUM         = 0b0100_0000_0000_0000;
        const MODULE       = 0b1000_0000_0000_0001;
        const MANDATED     = 0b1000_0000_0000_0001;
    }
}

#[derive(Debug)]
pub struct Exception {
    pub start: u16,
    pub end: u16,
    pub handler: u16,
    pub catch_type: u16,
}

#[derive(Debug)]
pub struct BootstrapMethod {
    pub method_ref: u16,
    pub arguments: Vec<u16>,
}

#[derive(Debug)]
pub struct LineNumber {
    pub start: u16,
    pub line_number: u16,
}

#[derive(Debug)]
pub enum Instruction {
    /// No operation
    NOP,

    /// Pushes null on the stack
    AConstNull,

    /// Pushes the int -1 on the stack
    IConstM1,

    /// Pushes the int 0 on the stack
    IConst0,
    /// Pushes the int 1 on the stack
    IConst1,
    /// Pushes the int 2 on the stack
    IConst2,
    /// Pushes the int 3 on the stack
    IConst3,
    /// Pushes the int 4 on the stack
    IConst4,
    /// Pushes the int 5 on the stack
    IConst5,

    /// Pushes the long 0 on the stack
    LConst0,
    /// Pushes the long 1 on the stack
    LConst1,

    /// Pushes the float 0 on the stack
    FConst0,
    /// Pushes the float 1 on the stack
    FConst1,
    /// Pushes the float 2 on the stack
    FConst2,

    /// Pushes the double 0 on the stack
    DConst0,
    /// Pushes the double 1 on the stack
    DConst1,

    /// Pushes a byte on the stack
    BIPush(i8),
    /// Pushes a short on the stack
    SIPush(i16),

    /// Pushes a constant from the constant pool on the stack
    LDC(u16),

    /// Pushes the int at a specific local variable index on the stack
    ILoad(u16),
    /// Pushes the long at a specific local variable index on the stack
    LLoad(u16),
    /// Pushes the float at a specific local variable index on the stack
    FLoad(u16),
    /// Pushes the double at a specific local variable index on the stack
    DLoad(u16),
    /// Pushes the reference at a specific local variable index on the stack
    ALoad(u16),

    /// Pushes the int at local variable index 0 on the stack
    ILoad0,
    /// Pushes the int at local variable index 1 on the stack
    ILoad1,
    /// Pushes the int at local variable index 2 on the stack
    ILoad2,
    /// Pushes the int at local variable index 3 on the stack
    ILoad3,

    /// Pushes the long at local variable index 0 on the stack
    LLoad0,
    /// Pushes the long at local variable index 1 on the stack
    LLoad1,
    /// Pushes the long at local variable index 2 on the stack
    LLoad2,
    /// Pushes the long at local variable index 3 on the stack
    LLoad3,

    /// Pushes the float at local variable index 0 on the stack
    FLoad0,
    /// Pushes the float at local variable index 1 on the stack
    FLoad1,
    /// Pushes the float at local variable index 2 on the stack
    FLoad2,
    /// Pushes the float at local variable index 3 on the stack
    FLoad3,

    /// Pushes the double at local variable index 0 on the stack
    DLoad0,
    /// Pushes the double at local variable index 1 on the stack
    DLoad1,
    /// Pushes the double at local variable index 2 on the stack
    DLoad2,
    /// Pushes the double at local variable index 3 on the stack
    DLoad3,

    /// Pushes the reference at local variable index 0 on the stack
    ALoad0,
    /// Pushes the reference at local variable index 1 on the stack
    ALoad1,
    /// Pushes the reference at local variable index 2 on the stack
    ALoad2,
    /// Pushes the reference at local variable index 3 on the stack
    ALoad3,

    /// Pushes the value from an int array, which is popped from the stack,
    /// at an index, which is popped from the stack as well, on the stack.
    IALoad,
    /// Pushes the value from an long array, which is popped from the stack,
    /// at an index, which is popped from the stack as well, on the stack.
    LALoad,
    /// Pushes the value from an float array, which is popped from the stack,
    /// at an index, which is popped from the stack as well, on the stack.
    FALoad,
    /// Pushes the value from an double array, which is popped from the stack,
    /// at an index, which is popped from the stack as well, on the stack.
    DALoad,
    /// Pushes the value from an reference array, which is popped from the stack,
    /// at an index, which is popped from the stack as well, on the stack.
    AALoad,
    /// Pushes the value from an byte array, which is popped from the stack,
    /// at an index, which is popped from the stack as well, on the stack.
    BALoad,
    /// Pushes the value from an char array, which is popped from the stack,
    /// at an index, which is popped from the stack as well, on the stack.
    CALoad,
    /// Pushes the value from an short array, which is popped from the stack,
    /// at an index, which is popped from the stack as well, on the stack.
    SALoad,

    IStore(u16),
    LStore(u16),
    FStore(u16),
    DStore(u16),
    AStore(u16),

    IStore0,
    IStore1,
    IStore2,
    IStore3,

    LStore0,
    LStore1,
    LStore2,
    LStore3,

    FStore0,
    FStore1,
    FStore2,
    FStore3,

    DStore0,
    DStore1,
    DStore2,
    DStore3,

    AStore0,
    AStore1,
    AStore2,
    AStore3,

    IAStore,
    LAStore,
    FAStore,
    DAStore,
    AAStore,
    BAStore,
    CAStore,
    SAStore,

    Pop,
    Pop2,

    Dup,
    DupX1,
    DupX2,

    Dup2,
    Dup2X1,
    Dup2X2,

    Swap,

    IAdd,
    LAdd,
    FAdd,
    DAdd,

    ISub,
    LSub,
    FSub,
    DSub,

    IMul,
    LMul,
    FMul,
    DMul,

    IDiv,
    LDiv,
    FDiv,
    DDiv,

    IRem,
    LRem,
    FRem,
    DRem,

    INeg,
    LNeg,
    FNeg,
    DNeg,

    IShL,
    LShL,

    IShR,
    LShR,

    IUShR,
    LUShR,

    IAnd,
    LAnd,

    IOr,
    LOr,

    IXOr,
    LXOr,

    IInc(u16, i16),

    I2L,
    I2F,
    I2D,

    L2I,
    L2F,
    L2D,

    F2I,
    F2L,
    F2D,

    D2I,
    D2L,
    D2F,

    I2B,
    I2C,
    I2S,

    LCmp,

    FCmpL,
    FCmpG,

    DCmpL,
    DCmpG,

    IfEq(i16),
    IfNE(i16),
    IfLT(i16),
    IfGE(i16),
    IfGT(i16),
    IfLE(i16),

    IfICmpEq(i16),
    IfICmpNE(i16),
    IfICmpLT(i16),
    IfICmpGE(i16),
    IfICmpGT(i16),
    IfICmpLE(i16),

    IfACmpEq(i16),
    IfACmpNE(i16),

    GoTo(i32),
    JSR(i32),
    Ret(u16),

    TableSwitch {
        default: i32,
        low: i32,
        high: i32,
        offsets: Vec<i32>,
    },
    LookupSwitch {
        default: i32,
        offsets: BTreeMap<i32, i32>,
    },

    IReturn,
    LReturn,
    FReturn,
    DReturn,
    AReturn,
    Return,

    GetStatic(u16),
    PutStatic(u16),
    GetField(u16),
    PutField(u16),

    InvokeVirtual(u16),
    InvokeSpecial(u16),
    InvokeStatic(u16),
    InvokeInterface(u16, u8),
    InvokeDynamic(u16),

    New(u16),
    NewArray(ArrayType),
    ANewArray(u16),
    ArrayLength,

    AThrow,

    CheckCast(u16),
    InstanceOf(u16),

    MonitorEnter,
    MonitorExit,

    MultiANewArray(u16, u8),

    IfNull(i16),
    IfNonNull(i16),

    BreakPoint,
    ImpDep1,
    ImpDep2,
}

#[derive(Debug)]
pub enum ArrayType {
    Boolean,
    Char,
    Float,
    Double,
    Byte,
    Short,
    Int,
    Long,
}

#[derive(Debug)]
pub struct InnerClass {
    pub inner_class_info: u16,
    pub outer_class_info: u16,
    pub inner_name: u16,
    pub inner_class_access_flags: AccessFlags,
}

#[derive(Debug)]
pub enum StackMapFrame {
    Same {
        offset_delta: u16,
    },
    Same1 {
        offset_delta: u16,
        stack: VerificationType,
    },
    Chop {
        offset_delta: u16,
        count: u8,
    },
    Append {
        offset_delta: u16,
        locals: Vec<VerificationType>,
    },
    Full {
        offset_delta: u16,
        locals: Vec<VerificationType>,
        stack: Vec<VerificationType>,
    },
}

#[derive(Debug)]
pub enum VerificationType {
    Top,
    Integer,
    Float,
    Double,
    Long,
    Null,
    UninitializedThis,
    Object(u16),
    Uninitialized(u16),
}

#[derive(Debug)]
pub struct Annotation {
    /// Must be an index to the constant pool with an `Item::UTF8(_)`
    /// representing a field descriptor
    pub type_index: u16,
    /// The value every single pair holds.
    /// The first part is an index to the constant pool,
    /// which must be an `Item::UTF8(_)`.
    /// The second one is the value itself.
    pub element_value_pairs: Vec<(u16, ElementValue)>,
}

#[derive(Debug)]
pub enum ElementValue {
    /// The index to the constant pool
    /// which must be an `Item::Integer(_)`.
    Byte(u16),
    /// The index to the constant pool
    /// which must be an `Item::Integer(_)`.
    Short(u16),
    /// The index to the constant pool
    /// which must be an `Item::Integer(_)`.
    Char(u16),
    /// The index to the constant pool
    /// which must be an `Item::Integer(_)`.
    Int(u16),
    /// The index to the constant pool
    /// which must be an `Item::Long(_)`.
    Long(u16),
    /// The index to the constant pool
    /// which must be an `Item::Float(_)`.
    Float(u16),
    /// The index to the constant pool
    /// which must be an `Item::Double(_)`.
    Double(u16),
    /// The index to the constant pool
    /// which must be an `Item::Integer(_)`.
    /// Yes, it really needs a constant pool entry for this.
    Boolean(u16),
    /// The index to the constant pool
    /// which must be an `Item::UTF8(_)`.
    String(u16),
    /// An enum constant.
    Enum {
        /// The index to the constant pool,
        /// which must be an `Item::UTF8(_)`.
        /// It results in the internal form of the binary name
        /// of the type of this enum constant.
        type_name: u16,
        /// The index to the constant pool,
        /// which must be an `Item::UTF8(_)`.
        /// It results in the simple name
        /// of this enum constant.
        const_name: u16,
    },
    /// A class literal.
    /// The index to the constant pool
    /// which must be an `Item::UTF8(_)`
    /// representing a return descriptor.
    Class(u16),
    /// Another annotation.
    Annotation(Box<Annotation>),
    /// Multiple `ElementValue`s
    Array(Vec<ElementValue>),
}

#[derive(Debug)]
pub struct TypeAnnotation {
    pub target_type: TargetType,
    pub target_path: Vec<TypePathElement>,
    pub annotation: Annotation,
}

#[derive(Debug)]
pub enum TargetType {
    /// Indicates that an annotation is present
    /// on the type parameter of a class.
    /// The index of the type parameter.
    TypeParameterClass(u8),
    /// Indicates that an annotation is present
    /// on the type parameter of a method.
    /// The index of the type parameter.
    TypeParameterMethod(u8),
    /// Indicates that an annotation is present
    /// on the implements or extends clause of a class.
    /// The index of the super type,
    /// 0xFFFF is the extends clause.
    SuperType(u16),
    /// Indicates that an annotation is present
    /// on a bound of a type parameter of a class.
    TypeParameterBoundClass {
        /// The index of the type parameter.
        type_parameter: u8,
        /// The index of the bound.
        bound_index: u8,
    },
    /// Indicates that an annotation is present
    /// on a bound of a type parameter of a method.
    TypeParameterBoundMethod {
        /// The index of the type parameter.
        type_parameter: u8,
        /// The index of the bound.
        bound_index: u8,
    },
    /// Indicates that an annotation is present
    /// on the type of a field declaration.
    EmptyField,
    /// Indicates that an annotation is present
    /// on the return type of a method
    /// or the type of a newly constructed object.
    EmptyReturn,
    /// Indicates that an annotation is present
    /// on the receiver type of a method.
    EmptyReceiver,
    /// Indicates that an annotation is present
    /// on the type in a formal parameter declaration.
    /// The index of the formal parameter.
    FormalParameter(u8),
    /// Indicates that an annotation is present
    /// on the type in a throws clause.
    /// The index into the table of the `Exceptions` attribute of the method.
    Throws(u16),
    /// Indicates that an annotation is present
    /// on the type in a local variable declaration.
    LocalVariable(Vec<LocalVariableTarget>),
    /// Indicates that an annotation is present
    /// on the type in a local variable declaration.
    ResourceVariable(Vec<LocalVariableTarget>),
    /// Indicates that an annotation is present
    /// on the type in an exception parameter declaration.
    Catch(u16),
    /// Indicates that an annotation is present
    /// on the type in an instanceof expression.
    OffsetInstanceOf(u16),
    /// Indicates that an annotation is present
    /// on the type in a new expression.
    OffsetNew(u16),
    /// Indicates that an annotation is present
    /// on the type before the ::new
    /// of a method reference expression.
    OffsetNewRef(u16),
    /// Indicates that an annotation is present
    /// on the type before the ::name
    /// of a method reference expression.
    OffsetRef(u16),
    /// Indicates that an annotation is present
    /// on the type of a cast expression.
    TypeArgumentCast { offset: u16, type_argument: u8 },
    /// Indicates that an annotation is present
    /// on the type of a method call expression.
    TypeArgumentMethod { offset: u16, type_argument: u8 },
    /// Indicates that an annotation is present
    /// on the type of a new expression.
    TypeArgumentConstructor { offset: u16, type_argument: u8 },
    /// Indicates that an annotation is present
    /// on the type of a ::new expression.
    TypeArgumentNewRef { offset: u16, type_argument: u8 },
    /// Indicates that an annotation is present
    /// on the type of a ::name expression.
    TypeArgumentRef { offset: u16, type_argument: u8 },
}

#[derive(Debug)]
pub struct TypePathElement {
    pub path_kind: TypePathKind,
    pub argument_index: u8,
}

#[derive(Debug)]
pub enum TypePathKind {
    /// Annotation is deeper in an array kind
    ArrayType,
    /// Annotation is deeper in a nested type
    NestedType,
    /// Annotation is on the bound of a wildcard type argument of a parameterized type
    WildcardType,
    /// Annotation is on a type argument of a parameterized type
    Type,
}

#[derive(Debug)]
pub struct LocalVariableTarget {
    /// Start of the Code.
    pub start: u16,
    /// Length of the Code.
    pub length: u16,
    /// The index in the local variable array of the current frame.
    /// double and long do occupy two spaces.
    pub index: u16,
}

/// An entry of the `LocalVariableTable`
#[derive(Debug)]
pub struct LocalVariable {
    /// Start of the Code.
    pub start: u16,
    /// Length of the Code.
    pub length: u16,
    /// The index to an `Item::UTF8(_)` representing a valid unqalified name.
    pub name: u16,
    /// The index to an `Item::UTF8(_)` representing a field/type descriptor.
    pub descriptor: u16,
    /// The index in the local variable array of the current frame.
    /// double and long do occupy two spaces.
    pub index: u16,
}

/// An entry of the `LocalVariableTypeTable`
#[derive(Debug)]
pub struct LocalVariableType {
    /// Start of the Code.
    pub start: u16,
    /// Length of the Code.
    pub length: u16,
    /// The index to an `Item::UTF8(_)` representing a valid unqalified name.
    pub name: u16,
    /// The index to an `Item::UTF8(_)` representing a field signature.
    pub signature: u16,
    /// The index in the local variable array of the current frame.
    /// double and long do occupy two spaces.
    pub index: u16,
}

#[derive(Debug)]
pub struct MethodParameter {
    pub name: u16,
    pub access_flags: AccessFlags,
}

#[derive(Debug)]
pub struct Requirement {
    pub index: u16,
    pub flags: AccessFlags,
    pub version: u16,
}

#[derive(Debug)]
pub struct Export {
    pub index: u16,
    pub flags: AccessFlags,
    pub to: Vec<u16>,
}

#[derive(Debug)]
pub struct Opening {
    pub index: u16,
    pub flags: AccessFlags,
    pub to: Vec<u16>,
}

#[derive(Debug)]
pub struct Provider {
    pub index: u16,
    pub with: Vec<u16>,
}