Static object creation

This commit is contained in:
VegOwOtenks 2024-09-02 17:44:59 +02:00
parent 8642dcdd6a
commit 6c0fbd179a
6 changed files with 160 additions and 29 deletions

View file

@ -266,10 +266,10 @@ impl JavaClassFile {
#[derive(Debug)]
pub struct FieldInfo {
access_flags: FieldAccessFlagMask,
name: String,
descriptor: AbstractTypeDescription,
attributes: Box<[AttributeInfo]>,
pub access_flags: FieldAccessFlagMask,
pub name: String,
pub descriptor: AbstractTypeDescription,
pub attributes: Box<[AttributeInfo]>,
}
impl FieldInfo {
@ -535,11 +535,15 @@ impl NestMembersAttributeData {
pub enum AttributeData {
Code(CodeAttributeData),
Signature(SignatureAttributeData),
Synthetic(),
Exceptions(ExceptionAttributeData),
NestMembers(NestMembersAttributeData),
SourceFile(SourceFileAttributeData),
InnerClasses(InnerClassesAttributeData),
ConstantValue(ConstantValueAttributeData),
DebugExtension(DebugExtensionAttributeData),
LineNumberTable(LineNumberTableAttributeData),
EnclosingMethod(EnclosingMethodAttributeData),
Unknown(UnknownAttributeData),
}
@ -564,7 +568,7 @@ impl AttributeInfo {
fn from_reader(reader: &mut dyn Read, pool: &Box<[ConstantPoolInfo]>, allow_code_attr: bool) -> Result<Self, Error> {
let attribute_name_index: u16 = read_u16(reader)? - 1;
let _attribute_byte_size: usize = read_u32(reader)?.try_into()?;
let attribute_byte_size: usize = read_u32(reader)?.try_into()?;
let data = {
let name_entry = &pool[attribute_name_index as usize];
@ -574,35 +578,47 @@ impl AttributeInfo {
};
match &utf8[..] {
"ConstantValue" => AttributeData::ConstantValue(
"ConstantValue" => AttributeData::ConstantValue(
ConstantValueAttributeData {
constant_value_index: read_u16(reader)?,
}
),
),
"LineNumberTable" => AttributeData::LineNumberTable(
"LineNumberTable" => AttributeData::LineNumberTable(
LineNumberTableAttributeData::from_reader(reader)?
),
),
"Code" => if allow_code_attr {
"Code" => if allow_code_attr {
AttributeData::Code(
CodeAttributeData::from_reader(reader, pool)?
)
} else {
} else {
return Err(Error::BadFileError("Nested Code attributes are forbidden.".to_string()));
},
},
"SourceFile" => AttributeData::SourceFile(SourceFileAttributeData::from_reader(reader)?),
"SourceFile" => AttributeData::SourceFile(SourceFileAttributeData::from_reader(reader)?),
"Signature" => AttributeData::Signature(SignatureAttributeData::from_reader(reader)?),
"Signature" => AttributeData::Signature(SignatureAttributeData::from_reader(reader)?),
"InnerClasses" => AttributeData::InnerClasses(InnerClassesAttributeData::from_reader(reader)?),
"InnerClasses" => AttributeData::InnerClasses(InnerClassesAttributeData::from_reader(reader)?),
"NestMembers" => AttributeData::NestMembers(NestMembersAttributeData::from_reader(reader)?),
"NestMembers" => AttributeData::NestMembers(NestMembersAttributeData::from_reader(reader)?),
"Exceptions" => AttributeData::Exceptions(ExceptionAttributeData::from_reader(reader)?),
"EnclosingMethod" => AttributeData::EnclosingMethod(EnclosingMethodAttributeData {class_index: read_u16(reader)?, method_index: read_u16(reader)?}),
"Synthetic" => AttributeData::Synthetic(),
"DebugExtension" => AttributeData::DebugExtension(
DebugExtensionAttributeData {
utf8: std::str::from_utf8(&(read_buffer(reader, attribute_byte_size)?))?.to_string(),
}
),
&_ => AttributeData::Unknown(
UnknownAttributeData {
info: read_buffer(reader, _attribute_byte_size)?,
info: read_buffer(reader, attribute_byte_size)?,
}
)
}
@ -617,8 +633,42 @@ impl AttributeInfo {
}
}
#[derive(Debug)]
pub struct DebugExtensionAttributeData {
utf8: String,
}
#[derive(Debug)]
pub struct EnclosingMethodAttributeData {
class_index: u16,
method_index: u16,
}
// for use on methods, what exception can be thrown by this
#[derive(Debug)]
pub struct ExceptionAttributeData {
exception_indices: Box<[u16]>,
}
impl ExceptionAttributeData {
fn from_reader(reader: &mut dyn Read) -> Result<Self, Error> {
let length = read_u16(reader)?;
let mut exception_vector = Vec::with_capacity(length as usize);
for _ in 0..length {
exception_vector.push(read_u16(reader)?);
}
Ok(
ExceptionAttributeData {
exception_indices: exception_vector.into_boxed_slice()
}
)
}
}
#[repr(u8)]
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum AbstractTypeKind {
Void() = b'V', // void
Byte() = b'B', // signed byte
@ -649,7 +699,7 @@ impl Into<String> for &AbstractTypeKind {
}
}
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct AbstractTypeDescription {
pub array_level: u8,
pub kind: AbstractTypeKind,