mirror of
https://github.com/Noratrieb/coldsquare.git
synced 2026-01-15 08:55:10 +01:00
Refactored Attributes to be a struct
because why not
This commit is contained in:
parent
5dcee9db57
commit
3546a5b268
2 changed files with 178 additions and 170 deletions
285
src/parse/mod.rs
285
src/parse/mod.rs
|
|
@ -245,10 +245,12 @@ impl Parse for MethodInfo {
|
|||
|
||||
impl Parse for AttributeInfo {
|
||||
fn parse(data: &mut Data) -> Result<Self> {
|
||||
Ok(Self::Unknown {
|
||||
Ok(Self {
|
||||
attribute_name_index: data.u2()?,
|
||||
attribute_length: data.u4()?,
|
||||
attribute_content: parse_vec(data, data.last_u4()? as usize)?,
|
||||
inner: AttributeInfoInner::Unknown {
|
||||
attribute_content: parse_vec(data, data.last_u4()? as usize)?,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -456,13 +458,20 @@ fn resolve_attributes(class: &mut ClassFile) -> Result<()> {
|
|||
impl AttributeInfo {
|
||||
fn resolve_attribute(&mut self, pool: &[CpInfo]) -> Result<()> {
|
||||
// this is a borrow checker hack, but it works :(
|
||||
let attr = std::mem::replace(self, AttributeInfo::__Empty);
|
||||
let attr = std::mem::replace(
|
||||
self,
|
||||
AttributeInfo {
|
||||
attribute_name_index: 0,
|
||||
attribute_length: 0,
|
||||
inner: AttributeInfoInner::__Empty,
|
||||
},
|
||||
);
|
||||
|
||||
let (&index, &len, content) = match &attr {
|
||||
AttributeInfo::Unknown {
|
||||
AttributeInfo {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
attribute_content,
|
||||
inner: AttributeInfoInner::Unknown { attribute_content },
|
||||
} => (attribute_name_index, attribute_length, attribute_content),
|
||||
_ => unreachable!("Attribute already resolved"),
|
||||
};
|
||||
|
|
@ -486,127 +495,163 @@ impl AttributeInfo {
|
|||
let _ = std::mem::replace(
|
||||
self,
|
||||
match name {
|
||||
"ConstantValue" => Self::ConstantValue {
|
||||
"ConstantValue" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
constantvalue_index: data.u2()?,
|
||||
},
|
||||
"Code" => Self::Code {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
max_stack: data.u2()?,
|
||||
max_locals: data.u2()?,
|
||||
code_length: data.u4()?,
|
||||
code: parse_vec(data, data.last_u4()? as usize)?,
|
||||
exception_table_length: data.u2()?,
|
||||
exception_table: parse_vec(data, data.last_u2()?)?,
|
||||
attributes_count: data.u2()?,
|
||||
attributes: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"StackMapTable" => Self::StackMapTable {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
number_of_entries: data.u2()?,
|
||||
entries: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"Exceptions" => Self::Exceptions {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
number_of_exceptions: data.u2()?,
|
||||
exception_index_table: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"InnerClasses" => Self::InnerClasses {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
number_of_classes: data.u2()?,
|
||||
classes: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"EnclosingMethod" => Self::EnclosingMethod {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
class_index: data.u2()?,
|
||||
method_index: data.u2()?,
|
||||
},
|
||||
"Synthetic" => Self::Synthetic {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
},
|
||||
"Signature" => Self::Signature {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
signature_index: data.u2()?,
|
||||
},
|
||||
"SourceFile" => Self::SourceFile {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
sourcefile_index: data.u2()?,
|
||||
},
|
||||
"SourceDebugExtension" => Self::SourceDebugExtension {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
debug_extension: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"LineNumberTable" => Self::LineNumberTable {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
line_number_table_length: data.u2()?,
|
||||
line_number_table: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"LocalVariableTable" => Self::LocalVariableTable {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
local_variable_table_length: data.u2()?,
|
||||
local_variable_table: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"LocalVariableTypeTable" => Self::LocalVariableTypeTable {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
local_variable_table_length: data.u2()?,
|
||||
local_variable_table: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"Deprecated" => Self::Deprecated {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
},
|
||||
"RuntimeVisibleAnnotations" => Self::RuntimeVisibleAnnotations {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
num_annotations: data.u2()?,
|
||||
annotations: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"RuntimeInvisibleAnnotations" => Self::RuntimeInvisibleAnnotations {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
num_annotations: data.u2()?,
|
||||
annotations: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
"RuntimeVisibleParameterAnnotations" => Self::RuntimeVisibleParameterAnnotations {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
num_parameters: data.u1()?,
|
||||
parameter_annotations: parse_vec(data, data.last_u1()?)?,
|
||||
},
|
||||
"RuntimeInvisibleParameterAnnotations" => {
|
||||
Self::RuntimeInvisibleParameterAnnotations {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
num_parameters: data.u1()?,
|
||||
parameter_annotations: parse_vec(data, data.last_u1()?)?,
|
||||
}
|
||||
}
|
||||
"AnnotationDefault" => Self::AnnotationDefault {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
default_value: AnnotationElementValue {
|
||||
tag: data.u1()?,
|
||||
value: AnnotationElementValueValue::parse(data)?,
|
||||
inner: AttributeInfoInner::ConstantValue {
|
||||
constantvalue_index: data.u2()?,
|
||||
},
|
||||
},
|
||||
"BootstrapMethods" => Self::BootstrapMethods {
|
||||
"Code" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
num_bootstrap_methods: data.u2()?,
|
||||
bootstrap_methods: parse_vec(data, data.last_u2()?)?,
|
||||
inner: AttributeInfoInner::Code {
|
||||
max_stack: data.u2()?,
|
||||
max_locals: data.u2()?,
|
||||
code_length: data.u4()?,
|
||||
code: parse_vec(data, data.last_u4()? as usize)?,
|
||||
exception_table_length: data.u2()?,
|
||||
exception_table: parse_vec(data, data.last_u2()?)?,
|
||||
attributes_count: data.u2()?,
|
||||
attributes: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"StackMapTable" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::StackMapTable {
|
||||
number_of_entries: data.u2()?,
|
||||
entries: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"Exceptions" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::Exceptions {
|
||||
number_of_exceptions: data.u2()?,
|
||||
exception_index_table: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"InnerClasses" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::InnerClasses {
|
||||
number_of_classes: data.u2()?,
|
||||
classes: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"EnclosingMethod" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::EnclosingMethod {
|
||||
class_index: data.u2()?,
|
||||
method_index: data.u2()?,
|
||||
},
|
||||
},
|
||||
"Synthetic" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::Synthetic,
|
||||
},
|
||||
"Signature" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::Signature {
|
||||
signature_index: data.u2()?,
|
||||
},
|
||||
},
|
||||
"SourceFile" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::SourceFile {
|
||||
sourcefile_index: data.u2()?,
|
||||
},
|
||||
},
|
||||
"SourceDebugExtension" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::SourceDebugExtension {
|
||||
debug_extension: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"LineNumberTable" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::LineNumberTable {
|
||||
line_number_table_length: data.u2()?,
|
||||
line_number_table: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"LocalVariableTable" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::LocalVariableTable {
|
||||
local_variable_table_length: data.u2()?,
|
||||
local_variable_table: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"LocalVariableTypeTable" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::LocalVariableTypeTable {
|
||||
local_variable_table_length: data.u2()?,
|
||||
local_variable_table: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"Deprecated" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::Deprecated,
|
||||
},
|
||||
"RuntimeVisibleAnnotations" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::RuntimeVisibleAnnotations {
|
||||
num_annotations: data.u2()?,
|
||||
annotations: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"RuntimeInvisibleAnnotations" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::RuntimeInvisibleAnnotations {
|
||||
num_annotations: data.u2()?,
|
||||
annotations: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
"RuntimeVisibleParameterAnnotations" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::RuntimeVisibleParameterAnnotations {
|
||||
num_parameters: data.u1()?,
|
||||
parameter_annotations: parse_vec(data, data.last_u1()?)?,
|
||||
},
|
||||
},
|
||||
"RuntimeInvisibleParameterAnnotations" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::RuntimeInvisibleParameterAnnotations {
|
||||
num_parameters: data.u1()?,
|
||||
parameter_annotations: parse_vec(data, data.last_u1()?)?,
|
||||
},
|
||||
},
|
||||
"AnnotationDefault" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::AnnotationDefault {
|
||||
default_value: AnnotationElementValue {
|
||||
tag: data.u1()?,
|
||||
value: AnnotationElementValueValue::parse(data)?,
|
||||
},
|
||||
},
|
||||
},
|
||||
"BootstrapMethods" => Self {
|
||||
attribute_name_index,
|
||||
attribute_length,
|
||||
inner: AttributeInfoInner::BootstrapMethods {
|
||||
num_bootstrap_methods: data.u2()?,
|
||||
bootstrap_methods: parse_vec(data, data.last_u2()?)?,
|
||||
},
|
||||
},
|
||||
name => return Err(ParseErr(format!("Invalid Attribute name: {}", name))),
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue