use crate::error::ClassFormatErr;
use num_enum::TryFromPrimitive;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Reference {
pub class_index: u16,
pub name_and_type_index: u16,
}
impl Reference {
pub(crate) fn new(class_index: u16, name_and_type_index: u16) -> Self {
Self {
class_index,
name_and_type_index,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NameAndType {
pub name_index: u16,
pub descriptor_index: u16,
}
impl NameAndType {
pub(crate) fn new(name_index: u16, descriptor_index: u16) -> Self {
Self {
name_index,
descriptor_index,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dynamic {
pub bootstrap_method_attr_index: u16,
pub name_and_type_index: u16,
}
impl Dynamic {
pub(crate) fn new(bootstrap_method_attr_index: u16, name_and_type_index: u16) -> Self {
Self {
bootstrap_method_attr_index,
name_and_type_index,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MethodHandle {
pub reference_kind: u8,
pub reference_index: u16,
}
impl MethodHandle {
pub(crate) fn new(reference_kind: u8, reference_index: u16) -> Self {
Self {
reference_kind,
reference_index,
}
}
pub fn get_kind(&self) -> Result<MethodHandleKind, ClassFormatErr> {
MethodHandleKind::try_from_primitive(self.reference_kind)
.map_err(|_| ClassFormatErr::InvalidMethodHandleKind(self.reference_kind))
}
}
#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)]
#[repr(u8)]
pub enum MethodHandleKind {
GetField = 1,
GetStatic = 2,
PutField = 3,
PutStatic = 4,
InvokeVirtual = 5,
InvokeStatic = 6,
InvokeSpecial = 7,
NewInvokeSpecial = 8,
InvokeInterface = 9,
}