Skip to content

Commit

Permalink
Rewrite descriptor parsing
Browse files Browse the repository at this point in the history
This unifies some stuff from names.rs and descriptor.rs
into descriptors.rs. This is a breaking change, as the
exposed type structure has changed slightly. FieldType
has been replaced by FieldDescriptor, which is structured
slightly differently but contains the same information.
MethodDescriptor's result is now called return_type.

Additionally, the Display trait is no longer implemented
by these types - however I'm open to adding that back
if there is demand for it.
  • Loading branch information
staktrace committed Aug 26, 2024
1 parent 2d3bde1 commit aee1368
Show file tree
Hide file tree
Showing 7 changed files with 628 additions and 661 deletions.
4 changes: 2 additions & 2 deletions examples/publicapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
{
continue;
}
println!(" {} {}", field.name, field.descriptor);
println!(" {} {:?}", field.name, field.descriptor);
}
for method in class.methods {
if method
Expand All @@ -26,7 +26,7 @@ fn main() {
{
continue;
}
println!(" {} {}", method.name, method.descriptor);
println!(" {} {:?}", method.name, method.descriptor);
}
}
Err(e) => eprintln!("Error: {} when parsing {:?}", e, arg),
Expand Down
20 changes: 10 additions & 10 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::constant_pool::{
use crate::constant_pool::{
BootstrapArgument, ConstantPoolEntry, LiteralConstant, MethodHandle, NameAndType,
};
use crate::descriptor::FieldType;
use crate::names::{is_return_descriptor, is_unqualified_name};
use crate::descriptors::{is_return_descriptor, parse_field_descriptor, FieldDescriptor};
use crate::names::is_unqualified_name;
use crate::{read_u1, read_u2, read_u4, AccessFlags, CafeRc, ParseError, ParseOptions};

#[derive(Debug)]
Expand Down Expand Up @@ -104,7 +104,7 @@ pub struct LocalVariableEntry<'a> {
pub start_pc: u16,
pub length: u16,
pub name: Cow<'a, str>,
pub descriptor: FieldType<'a>,
pub descriptor: FieldDescriptor<'a>,
pub index: u16,
}

Expand All @@ -129,7 +129,7 @@ pub enum AnnotationElementValue<'a> {
BooleanConstant(i32),
StringConstant(Cow<'a, str>),
EnumConstant {
type_name: FieldType<'a>,
type_name: FieldDescriptor<'a>,
const_name: Cow<'a, str>,
},
ClassLiteral {
Expand All @@ -147,7 +147,7 @@ pub struct AnnotationElement<'a> {

#[derive(Debug)]
pub struct Annotation<'a> {
pub type_descriptor: FieldType<'a>,
pub type_descriptor: FieldDescriptor<'a>,
pub elements: Vec<AnnotationElement<'a>>,
}

Expand Down Expand Up @@ -309,7 +309,7 @@ pub struct ModuleData<'a> {
#[derive(Debug)]
pub struct RecordComponentEntry<'a> {
pub name: Cow<'a, str>,
pub descriptor: FieldType<'a>,
pub descriptor: FieldDescriptor<'a>,
pub attributes: Vec<AttributeInfo<'a>>,
}

Expand Down Expand Up @@ -602,7 +602,7 @@ fn read_localvariable_data<'a>(
fail!("Invalid unqualified name for variable {}", i);
}
let descriptor = read_cp_utf8(bytes, ix, pool)
.and_then(|descriptor| FieldType::parse(&descriptor))
.and_then(|descriptor| parse_field_descriptor(&descriptor, 0))
.map_err(|e| err!(e, "descriptor for variable {}", i))?;
let index = read_u2(bytes, ix)?;
localvariables.push(LocalVariableEntry {
Expand Down Expand Up @@ -661,7 +661,7 @@ fn read_annotation_element_value<'a>(
's' => AnnotationElementValue::StringConstant(read_cp_utf8(bytes, ix, pool)?),
'e' => {
let type_name = read_cp_utf8(bytes, ix, pool)
.and_then(|descriptor| FieldType::parse(&descriptor))
.and_then(|descriptor| parse_field_descriptor(&descriptor, 0))
.map_err(|e| err!(e, "annotation element value enum descriptor"))?;
let const_name = read_cp_utf8(bytes, ix, pool)?;
AnnotationElementValue::EnumConstant {
Expand Down Expand Up @@ -699,7 +699,7 @@ fn read_annotation<'a>(
pool: &[CafeRc<ConstantPoolEntry<'a>>],
) -> Result<Annotation<'a>, ParseError> {
let type_descriptor = read_cp_utf8(bytes, ix, pool)
.and_then(|descriptor| FieldType::parse(&descriptor))
.and_then(|descriptor| parse_field_descriptor(&descriptor, 0))
.map_err(|e| err!(e, "type descriptor field"))?;
let element_count = read_u2(bytes, ix)?;
let mut elements = Vec::with_capacity(element_count.into());
Expand Down Expand Up @@ -1035,7 +1035,7 @@ fn read_record_data<'a>(
fail!("Invalid unqualified name for entry {}", i);
}
let descriptor = read_cp_utf8(bytes, ix, pool)
.and_then(|descriptor| FieldType::parse(&descriptor))
.and_then(|descriptor| parse_field_descriptor(&descriptor, 0))
.map_err(|e| err!(e, "descriptor of entry {}", i))?;
let attributes =
read_attributes(bytes, ix, pool, opts).map_err(|e| err!(e, "entry {}", i))?;
Expand Down
4 changes: 2 additions & 2 deletions src/constant_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::ops::DerefMut;
#[cfg(feature = "threadsafe")]
use std::sync::Mutex;

use crate::descriptors::{is_array_descriptor, is_field_descriptor, is_method_descriptor};
use crate::names::{
is_array_descriptor, is_binary_name, is_field_descriptor, is_method_descriptor, is_module_name,
is_unqualified_method_name, is_unqualified_name,
is_binary_name, is_module_name, is_unqualified_method_name, is_unqualified_name,
};
use crate::{read_u1, read_u2, read_u4, read_u8, CafeRc, ParseError};

Expand Down
Loading

0 comments on commit aee1368

Please sign in to comment.