Skip to main content

lvm_class/constant_pool/
types.rs

1use crate::error::ClassFormatErr;
2use num_enum::TryFromPrimitive;
3
4/// Reference to a method, field, or interface method in the constant pool.
5///
6/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.2
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Reference {
9    pub class_index: u16,
10    pub name_and_type_index: u16,
11}
12
13impl Reference {
14    pub(crate) fn new(class_index: u16, name_and_type_index: u16) -> Self {
15        Self {
16            class_index,
17            name_and_type_index,
18        }
19    }
20}
21
22/// Name and type descriptor pair in the constant pool.
23///
24/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.6
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct NameAndType {
27    pub name_index: u16,
28    pub descriptor_index: u16,
29}
30
31impl NameAndType {
32    pub(crate) fn new(name_index: u16, descriptor_index: u16) -> Self {
33        Self {
34            name_index,
35            descriptor_index,
36        }
37    }
38}
39
40/// Dynamic constant or invoke dynamic info.
41///
42/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.10
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct Dynamic {
45    pub bootstrap_method_attr_index: u16,
46    pub name_and_type_index: u16,
47}
48
49impl Dynamic {
50    pub(crate) fn new(bootstrap_method_attr_index: u16, name_and_type_index: u16) -> Self {
51        Self {
52            bootstrap_method_attr_index,
53            name_and_type_index,
54        }
55    }
56}
57
58/// Method handle reference in the constant pool.
59///
60/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.8
61#[derive(Debug, Clone, PartialEq, Eq)]
62pub struct MethodHandle {
63    pub reference_kind: u8,
64    pub reference_index: u16,
65}
66
67impl MethodHandle {
68    pub(crate) fn new(reference_kind: u8, reference_index: u16) -> Self {
69        Self {
70            reference_kind,
71            reference_index,
72        }
73    }
74
75    pub fn get_kind(&self) -> Result<MethodHandleKind, ClassFormatErr> {
76        MethodHandleKind::try_from_primitive(self.reference_kind)
77            .map_err(|_| ClassFormatErr::InvalidMethodHandleKind(self.reference_kind))
78    }
79}
80
81/// Kind of method handle reference.
82///
83/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.8
84#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)]
85#[repr(u8)]
86pub enum MethodHandleKind {
87    GetField = 1,
88    GetStatic = 2,
89    PutField = 3,
90    PutStatic = 4,
91    InvokeVirtual = 5,
92    InvokeStatic = 6,
93    InvokeSpecial = 7,
94    NewInvokeSpecial = 8,
95    InvokeInterface = 9,
96}