cfsp 1.0.1

A JVM Bytecode Manipulation Framework inspired by ASM.
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
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
use std::cell::RefCell;
use std::ops::Deref;

use paste::paste;
use serde::{Deserialize, Serialize};

use crate::node::attribute::{Attribute, AttributeInfo, BootstrapMethod, BootstrapMethods};
use crate::node::error::{NodeResError, NodeResResult};
use crate::parse::ParseError;

macro_rules! const_getter {
    ($(#[$attr:meta])* => $variant_name: ident, $variant: ty) => {
        paste! {
            $(#[$attr])*
            pub fn [< get_ $variant_name >] (&self, index: u16) -> Option<&$variant> {
                if let Some(Some(Constant::$variant(constant))) = self.get(index as usize - 1)
                {
                    Some(constant)
                } else {
                    None
                }
            }
        }
    };
}

/// Represents a class constant pool.
///
/// See [4.4 The Constant Pool](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=93).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ConstantPool {
    len: u16,
    entries: Vec<Option<Constant>>,
}

impl ConstantPool {
    pub(crate) fn with_capacity(capacity: u16) -> Self {
        Self {
            len: 1,
            entries: Vec::with_capacity(capacity as usize),
        }
    }

    pub fn len(&self) -> u16 {
        self.len
    }

    pub(crate) fn add(&mut self, constant: Constant) {
        let occupies_2_slots = constant.occupies_2_slots();

        self.entries.push(Some(constant));

        if occupies_2_slots {
            self.len += 2;

            self.entries.push(None);
        } else {
            self.len += 1;
        }
    }

    pub fn get_constant(&self, index: u16) -> Option<&Constant> {
        self.get(index as usize - 1).map(Option::as_ref).flatten()
    }

    const_getter!(
        /// Gets [Utf8] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => utf8, Utf8
    );
    const_getter!(
        /// Gets [Integer] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => integer, Integer
    );
    const_getter!(
        /// Gets [Float] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => float, Float
    );
    const_getter!(
        /// Gets [Long] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => long, Long
    );
    const_getter!(
        /// Gets [Double] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => double, Double
    );
    const_getter!(
        /// Gets [Class] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => class, Class
    );
    const_getter!(
        /// Gets [String] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => string, String
    );
    const_getter!(
        /// Gets [FieldRef] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => field_ref, FieldRef
    );
    const_getter!(
        /// Gets [MethodRef] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => method_ref, MethodRef
    );
    const_getter!(
        /// Gets [InterfaceMethodRef] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => interface_method_ref, InterfaceMethodRef
    );
    const_getter!(
        /// Gets [NameAndType] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => name_and_type, NameAndType
    );
    const_getter!(
        /// Gets [MethodHandle] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => method_handle, MethodHandle
    );
    const_getter!(
        /// Gets [MethodType] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => method_type, MethodType
    );
    const_getter!(
        /// Gets [Dynamic] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => dynamic, Dynamic
    );
    const_getter!(
        /// Gets [InvokeDynamic] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => invoke_dynamic, InvokeDynamic
    );
    const_getter!(
        /// Gets [InvokeDynamic] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => module, Module
    );
    const_getter!(
        /// Gets [Package] constant reference from constant pool based on given index, returns
        /// [None] if the constant does not exist or match.
        => package, Package
    );
}

impl Deref for ConstantPool {
    type Target = Vec<Option<Constant>>;

    fn deref(&self) -> &Self::Target {
        &self.entries
    }
}

/// Represents JVM constant tags.
///
/// See [Table 4.4-A](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=94).
#[repr(u8)]
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum ConstantTag {
    Utf8 = 1,
    Integer = 3,
    Float = 4,
    Long = 5,
    Double = 6,
    Class = 7,
    String = 8,
    FieldRef = 9,
    MethodRef = 10,
    InterfaceMethodRef = 11,
    NameAndType = 12,
    MethodHandle = 15,
    MethodType = 16,
    Dynamic = 17,
    InvokeDynamic = 18,
    Module = 19,
    Package = 20,
}

impl TryFrom<u8> for ConstantTag {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use ConstantTag::*;

        let tag = match value {
            1 => Utf8,
            3 => Integer,
            4 => Float,
            5 => Long,
            6 => Double,
            7 => Class,
            8 => String,
            9 => FieldRef,
            10 => MethodRef,
            11 => InterfaceMethodRef,
            12 => NameAndType,
            15 => MethodHandle,
            16 => MethodType,
            17 => Dynamic,
            18 => InvokeDynamic,
            19 => Module,
            20 => Package,
            _ => {
                return Err(ParseError::MatchOutOfBoundUsize(
                    "constant tag",
                    vec!["1", "3..=12", "15..=20"],
                    value as usize,
                ))
            }
        };

        Ok(tag)
    }
}

impl Into<&'static str> for ConstantTag {
    fn into(self) -> &'static str {
        match self {
            ConstantTag::Utf8 => "Utf8",
            ConstantTag::Integer => "Integer",
            ConstantTag::Float => "Float",
            ConstantTag::Long => "Long",
            ConstantTag::Double => "Double",
            ConstantTag::Class => "Class",
            ConstantTag::String => "String",
            ConstantTag::FieldRef => "FieldRef",
            ConstantTag::MethodRef => "MethodRef",
            ConstantTag::InterfaceMethodRef => "InterfaceMethodRef",
            ConstantTag::NameAndType => "NameAndType",
            ConstantTag::MethodHandle => "MethodHandle",
            ConstantTag::MethodType => "MethodType",
            ConstantTag::Dynamic => "Dynamic",
            ConstantTag::InvokeDynamic => "InvokeDynamic",
            ConstantTag::Module => "Module",
            ConstantTag::Package => "Package",
        }
    }
}

/// Represents JVM constants.
///
/// See [4.4 The Constant Pool](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=93).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Constant {
    Utf8(Utf8),
    Integer(Integer),
    Float(Float),
    Long(Long),
    Double(Double),
    Class(Class),
    String(String),
    FieldRef(FieldRef),
    MethodRef(MethodRef),
    InterfaceMethodRef(InterfaceMethodRef),
    NameAndType(NameAndType),
    MethodHandle(MethodHandle),
    MethodType(MethodType),
    Dynamic(Dynamic),
    InvokeDynamic(InvokeDynamic),
    Module(Module),
    Package(Package),
}

impl Constant {
    /// Get corresponding [ConstantTag] for current constant.
    pub const fn tag(&self) -> ConstantTag {
        match self {
            Constant::Utf8(..) => ConstantTag::Utf8,
            Constant::Integer(..) => ConstantTag::Integer,
            Constant::Float(..) => ConstantTag::Float,
            Constant::Long(..) => ConstantTag::Long,
            Constant::Double(..) => ConstantTag::Double,
            Constant::Class(..) => ConstantTag::Class,
            Constant::String(..) => ConstantTag::String,
            Constant::FieldRef(..) => ConstantTag::FieldRef,
            Constant::MethodRef(..) => ConstantTag::MethodRef,
            Constant::InterfaceMethodRef(..) => ConstantTag::InterfaceMethodRef,
            Constant::NameAndType(..) => ConstantTag::NameAndType,
            Constant::MethodHandle(..) => ConstantTag::MethodHandle,
            Constant::MethodType(..) => ConstantTag::MethodType,
            Constant::Dynamic(..) => ConstantTag::Dynamic,
            Constant::InvokeDynamic(..) => ConstantTag::InvokeDynamic,
            Constant::Module(..) => ConstantTag::Module,
            Constant::Package(..) => ConstantTag::Package,
        }
    }

    pub const fn occupies_2_slots(&self) -> bool {
        match self {
            Constant::Long(_) | Constant::Double(_) => true,
            _ => false,
        }
    }

    pub fn name(&self) -> &'static str {
        self.tag().into()
    }
}

/// Represents constant UTF8.
///
/// See [4.4.7 The CONSTANT_Utf8_info Structure](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=102).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Utf8 {
    pub length: u16,
    pub bytes: Vec<u8>,
    #[serde(skip)]
    pub string: RefCell<Option<std::string::String>>,
}

impl Utf8 {
    /// Converts bytes into string.
    pub fn string(&self) -> NodeResResult<std::string::String> {
        let string_ref = self.string.borrow_mut();

        if let Some(string) = string_ref.as_ref() {
            Ok(string.clone())
        } else {
            cesu8::from_java_cesu8(&self.bytes[..])
                .map_err(|_| NodeResError::StringParseFail(self.bytes.clone().into_boxed_slice()))
                .map(|string| string.to_string())
        }
    }
}

/// Represents constant Integer.
///
/// See [4.4.4 The CONSTANT_Integer_info and CONSTANT_Float_info Structures](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=98).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Integer {
    pub bytes: [u8; 4],
}

impl Integer {
    /// Converts bytes into i32.
    pub fn as_i32(&self) -> i32 {
        i32::from_be_bytes(self.bytes)
    }
}

/// Represents constant Float.
///
/// See [4.4.4 The CONSTANT_Integer_info and CONSTANT_Float_info Structures](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=98).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Float {
    pub bytes: [u8; 4],
}

impl Float {
    /// Converts bytes into f32.
    pub fn as_f32(&self) -> f32 {
        f32::from_be_bytes(self.bytes)
    }
}

/// Represents constant Long.
///
/// See [4.4.5 The CONSTANT_Long_info and CONSTANT_Double_info Structures](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=100).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Long {
    pub high_bytes: [u8; 4],
    pub low_bytes: [u8; 4],
}

impl Long {
    /// Converts bytes into i64.
    pub fn as_i64(&self) -> i64 {
        let mut bytes = [0u8; 8];
        bytes[..4].copy_from_slice(&self.high_bytes);
        bytes[4..].copy_from_slice(&self.low_bytes);

        i64::from_be_bytes(bytes)
    }
}

/// Represents constant Double.
///
/// See [4.4.5 The CONSTANT_Long_info and CONSTANT_Double_info Structures](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=100).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Double {
    pub high_bytes: [u8; 4],
    pub low_bytes: [u8; 4],
}

impl Double {
    /// Converts bytes into f64.
    pub fn as_f64(&self) -> f64 {
        let mut bytes = [0u8; 8];
        bytes[..4].copy_from_slice(&self.high_bytes);
        bytes[4..].copy_from_slice(&self.low_bytes);

        f64::from_be_bytes(bytes)
    }
}

/// Represents constant Class.
///
/// See [4.4.1 The CONSTANT_Class_info Structure](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=96).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Class {
    pub name_index: u16,
}

//noinspection DuplicatedCode
impl Class {
    /// Get name of class.
    pub fn name<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Utf8> {
        constant_pool.get_utf8(self.name_index)
    }
}

/// Represents constant String.
///
/// See [4.4.3 The CONSTANT_String_info Structure](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=98).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct String {
    pub string_index: u16,
}

impl String {
    /// Gets the string of [string](String).
    pub fn string<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Utf8> {
        constant_pool.get_utf8(self.string_index)
    }
}

/// Represents constant FieldRef.
///
/// See [4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=97).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct FieldRef {
    pub class_index: u16,
    pub name_and_type_index: u16,
}

//noinspection DuplicatedCode
impl FieldRef {
    /// Gets owner class of field.
    pub fn class<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Class> {
        constant_pool.get_class(self.class_index)
    }

    /// Gets [NameAndType] of field.
    pub fn name_and_type<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool NameAndType> {
        constant_pool.get_name_and_type(self.name_and_type_index)
    }
}

/// Represents constant MethodRef.
///
/// See [4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=97).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct MethodRef {
    pub class_index: u16,
    pub name_and_type_index: u16,
}

//noinspection DuplicatedCode
impl MethodRef {
    /// Gets owner class of method.
    pub fn class<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Class> {
        constant_pool.get_class(self.class_index)
    }

    /// Gets [NameAndType] of method.
    pub fn name_and_type<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool NameAndType> {
        constant_pool.get_name_and_type(self.name_and_type_index)
    }
}

/// Represents constant InterfaceMethodRef.
///
/// See [4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=97).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct InterfaceMethodRef {
    pub class_index: u16,
    pub name_and_type_index: u16,
}

//noinspection DuplicatedCode
impl InterfaceMethodRef {
    /// Gets owner class of interface method.
    pub fn class<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Class> {
        constant_pool.get_class(self.class_index)
    }

    /// Gets [NameAndType] of interface method.
    pub fn name_and_type<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool NameAndType> {
        constant_pool.get_name_and_type(self.name_and_type_index)
    }
}

/// Represents constant NameAndType.
///
/// See [4.4.6 The CONSTANT_NameAndType_info Structure](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=101).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct NameAndType {
    pub name_index: u16,
    pub type_index: u16,
}

//noinspection DuplicatedCode
impl NameAndType {
    /// Gets the name of [NameAndType].
    pub fn name<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Utf8> {
        constant_pool.get_utf8(self.name_index)
    }

    /// Gets the type of [NameAndType].
    pub fn typ<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Utf8> {
        constant_pool.get_utf8(self.type_index)
    }
}

/// Represents constant MethodHandle.
///
/// See [4.4.8 The CONSTANT_MethodHandle_info Structure](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=104).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct MethodHandle {
    pub reference_kind: u8,
    pub reference_index: u16,
}

impl MethodHandle {
    /// Gets [RefKind] of MethodHandle.
    pub fn reference_kind(&self) -> NodeResResult<RefKind> {
        RefKind::try_from(self.reference_kind).map_err(|_| {
            NodeResError::MatchOutOfBound(
                "reference kind",
                vec!["1..=9"],
                self.reference_kind as usize,
            )
        })
    }

    /// Gets the referenced constant of MethodHandle.
    pub fn reference_constant<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> NodeResResult<Option<&'constant_pool Constant>> {
        if let Some(constant) = constant_pool.get_constant(self.reference_index) {
            match self.reference_kind()? {
                RefKind::GetField | RefKind::GetStatic | RefKind::PutField | RefKind::PutStatic => {
                    if let Constant::FieldRef(_) = constant {
                        Ok(Some(constant))
                    } else {
                        Err(NodeResError::MismatchReferenceConstant(
                            "FieldRef",
                            self.reference_index,
                            constant.name(),
                        ))
                    }
                }
                RefKind::InvokeVirtual | RefKind::NewInvokeSpecial => {
                    if let Constant::MethodRef(_) = constant {
                        Ok(Some(constant))
                    } else {
                        Err(NodeResError::MismatchReferenceConstant(
                            "MethodRef",
                            self.reference_index,
                            constant.name(),
                        ))
                    }
                }
                RefKind::InvokeStatic | RefKind::InvokeSpecial => {
                    if matches!(
                        constant,
                        Constant::MethodRef(_) | Constant::InterfaceMethodRef(_)
                    ) {
                        Ok(Some(constant))
                    } else {
                        Err(NodeResError::MismatchReferenceConstant(
                            "MethodRef or constant InterfaceMethodRef",
                            self.reference_index,
                            constant.name(),
                        ))
                    }
                }
                RefKind::InvokeInterface => {
                    if let Constant::InterfaceMethodRef(_) = constant {
                        Ok(Some(constant))
                    } else {
                        Err(NodeResError::MismatchReferenceConstant(
                            "InterfaceMethodRef",
                            self.reference_index,
                            constant.name(),
                        ))
                    }
                }
            }
        } else {
            Ok(None)
        }
    }
}

/// Represents constant MethodType.
///
/// See [4.4.9 The CONSTANT_MethodType_info Structure](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=106).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct MethodType {
    pub descriptor_index: u16,
}

impl MethodType {
    /// Gets descriptor of the MethodType.
    pub fn descriptor<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Utf8> {
        constant_pool.get_utf8(self.descriptor_index)
    }
}

/// Represents constant Dynamic.
///
/// See [4.4.10 The CONSTANT_Dynamic_info and CONSTANT_InvokeDynamic_info Structures](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=106).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Dynamic {
    pub bootstrap_method_attr_index: u16,
    pub name_and_type_index: u16,
}

//noinspection DuplicatedCode
impl Dynamic {
    /// Gets the [BootstrapMethod] reference [Dynamic] targeting to.
    pub fn bootstrap_method<'bootstrap_method, 'attributes: 'bootstrap_method>(
        &self,
        attribute_infos: &'attributes Vec<AttributeInfo>,
    ) -> Option<&'bootstrap_method BootstrapMethod> {
        let bootstrap_methods = if let Some(AttributeInfo {
            attribute:
                Some(Attribute::BootstrapMethods(BootstrapMethods {
                    bootstrap_methods, ..
                })),
            ..
        }) = attribute_infos.iter().find(|attribute_info| {
            matches!(
                attribute_info.attribute,
                Some(Attribute::BootstrapMethods { .. })
            )
        }) {
            bootstrap_methods
        } else {
            return None;
        };

        bootstrap_methods.get(self.bootstrap_method_attr_index as usize)
    }

    /// Gets the descriptor of the [Dynamic].
    pub fn name_and_type<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool NameAndType> {
        constant_pool.get_name_and_type(self.name_and_type_index)
    }
}

/// Represents constant InvokeDynamic.
///
/// See [4.4.10 The CONSTANT_Dynamic_info and CONSTANT_InvokeDynamic_info Structures](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=106).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct InvokeDynamic {
    pub bootstrap_method_attr_index: u16,
    pub name_and_type_index: u16,
}

//noinspection DuplicatedCode
impl InvokeDynamic {
    /// Gets the [BootstrapMethod] reference [InvokeDynamic] targeting to.
    pub fn bootstrap_method<'bootstrap_method, 'attributes: 'bootstrap_method>(
        &self,
        attribute_infos: &'attributes Vec<AttributeInfo>,
    ) -> Option<&'bootstrap_method BootstrapMethod> {
        let bootstrap_methods = if let Some(AttributeInfo {
            attribute:
                Some(Attribute::BootstrapMethods(BootstrapMethods {
                    bootstrap_methods, ..
                })),
            ..
        }) = attribute_infos.iter().find(|attribute_info| {
            matches!(
                attribute_info.attribute,
                Some(Attribute::BootstrapMethods { .. })
            )
        }) {
            bootstrap_methods
        } else {
            return None;
        };

        bootstrap_methods.get(self.bootstrap_method_attr_index as usize)
    }

    /// Gets the descriptor of the [InvokeDynamic].
    pub fn name_and_type<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool NameAndType> {
        constant_pool.get_name_and_type(self.name_and_type_index)
    }
}

/// Represents constant Module.
///
/// See [4.4.11 The CONSTANT_Module_info Structure](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=107).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Module {
    pub name_index: u16,
}

//noinspection DuplicatedCode
impl Module {
    /// Gets name of [Module].
    pub fn name<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Utf8> {
        constant_pool.get_utf8(self.name_index)
    }
}

/// Represents constant Package.
///
/// See [4.4.12 The CONSTANT_Package_info Structure](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=108).
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Package {
    pub name_index: u16,
}

//noinspection DuplicatedCode
impl Package {
    /// Gets name of [Package].
    pub fn name<'constant, 'constant_pool: 'constant>(
        &'constant self,
        constant_pool: &'constant_pool ConstantPool,
    ) -> Option<&'constant_pool Utf8> {
        constant_pool.get_utf8(self.name_index)
    }
}

/// Represents reference kind used by [MethodHandle].
///
/// See [Table 5.4.3.5-A](https://docs.oracle.com/javase/specs/jvms/se20/jvms20.pdf#page=396)
#[repr(u8)]
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub enum RefKind {
    GetField = 1,
    GetStatic = 2,
    PutField = 3,
    PutStatic = 4,
    InvokeVirtual = 5,
    InvokeStatic = 6,
    InvokeSpecial = 7,
    NewInvokeSpecial = 8,
    InvokeInterface = 9,
}

impl TryFrom<u8> for RefKind {
    type Error = ParseError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        use RefKind::*;

        let ref_kind = match value {
            1 => GetField,
            2 => GetStatic,
            3 => PutField,
            4 => PutStatic,
            5 => InvokeVirtual,
            6 => InvokeStatic,
            7 => InvokeSpecial,
            8 => NewInvokeSpecial,
            9 => InvokeInterface,
            _ => {
                return Err(ParseError::MatchOutOfBoundUsize(
                    "reference kind",
                    vec!["1..=9"],
                    value as usize,
                ))
            }
        };

        Ok(ref_kind)
    }
}