Skip to content

Commit

Permalink
clean up descriptor code and some minor warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
bluejekyll authored and staktrace committed Jun 23, 2022
1 parent e9cfc2d commit 29b27d0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target
Cargo.lock

tests/parse/tgz/openjdk16.0.1-modules
1 change: 1 addition & 0 deletions src/constant_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ pub(crate) fn read_cp_invokedynamic<'a>(
}
}

#[allow(dead_code)]
#[derive(Debug)]
pub struct Dynamic<'a> {
attr_index: u16,
Expand Down
84 changes: 42 additions & 42 deletions src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use crate::ParseError;

/// MethodDescriptor as described in section 4.3.3 of the [JVM 18 specification](https://docs.oracle.com/javase/specs/jvms/se18/jvms18.pdf)
/// MethodDescriptor as described in section 4.3.3 of the [JVM 18 specification](https://docs.oracle.com/javase/specs/jvms/se18/html/jvms-4.html#jvms-4.3.3)
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct MethodDescriptor<'a> {
pub parameters: Vec<FieldType<'a>>,
Expand Down Expand Up @@ -58,7 +58,7 @@ impl<'a> fmt::Display for MethodDescriptor<'a> {
}
}

/// FieldType as described in section 4.3.2 of the [JVM 18 specification](https://docs.oracle.com/javase/specs/jvms/se18/jvms18.pdf)
/// FieldType as described in section 4.3.2 of the [JVM 18 specification](https://docs.oracle.com/javase/specs/jvms/se18/html/jvms-4.html#jvms-4.3.2)
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum FieldType<'a> {
Base(BaseType),
Expand Down Expand Up @@ -97,38 +97,38 @@ impl<'a> fmt::Display for FieldType<'a> {
}
}

/// BaseType as described in Table 4.3-A. of the [JVM 18 specification](https://docs.oracle.com/javase/specs/jvms/se18/jvms18.pdf)
/// BaseType as described in Table 4.3-A. of the [JVM 18 specification](https://docs.oracle.com/javase/specs/jvms/se18/html/jvms-4.html#jvms-4.3.2-200)
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum BaseType {
/// B, byte, signed byte
BYTE,
Byte,
/// C, char, Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16
CHAR,
Char,
/// D, double, double-precision floating-point value
DOUBLE,
Double,
/// F, float, single-precision floating-point value
FLOAT,
Float,
/// I, int, integer
INT,
Int,
/// J, long, long integer
LONG,
Long,
/// S, short, signed short
SHORT,
Short,
/// Z, boolean, true or false
BOOLEAN,
Boolean,
}

impl BaseType {
fn parse(ch: char) -> Result<Self, ParseError> {
let this = match ch {
'B' => Self::BYTE,
'C' => Self::CHAR,
'D' => Self::DOUBLE,
'F' => Self::FLOAT,
'I' => Self::INT,
'J' => Self::LONG,
'S' => Self::SHORT,
'Z' => Self::BOOLEAN,
'B' => Self::Byte,
'C' => Self::Char,
'D' => Self::Double,
'F' => Self::Float,
'I' => Self::Int,
'J' => Self::Long,
'S' => Self::Short,
'Z' => Self::Boolean,
_ => fail!("Invalid base type {}", ch),
};

Expand All @@ -139,14 +139,14 @@ impl BaseType {
impl fmt::Display for BaseType {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let c = match self {
Self::BYTE => 'B',
Self::CHAR => 'C',
Self::DOUBLE => 'D',
Self::FLOAT => 'F',
Self::INT => 'I',
Self::LONG => 'J',
Self::SHORT => 'S',
Self::BOOLEAN => 'Z',
Self::Byte => 'B',
Self::Char => 'C',
Self::Double => 'D',
Self::Float => 'F',
Self::Int => 'I',
Self::Long => 'J',
Self::Short => 'S',
Self::Boolean => 'Z',
};

f.write_char(c)
Expand Down Expand Up @@ -243,7 +243,7 @@ mod tests {
let mut parameters = descriptor.parameters.into_iter();
let result = descriptor.result;

assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::LONG));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::Long));
assert!(parameters.next().is_none());
assert_eq!(result, ReturnDescriptor::Void);
}
Expand All @@ -259,7 +259,7 @@ mod tests {
assert!(parameters.next().is_none());
assert_eq!(
result,
ReturnDescriptor::Return(FieldType::Base(BaseType::LONG))
ReturnDescriptor::Return(FieldType::Base(BaseType::Long))
);
}

Expand All @@ -271,19 +271,19 @@ mod tests {
let mut parameters = descriptor.parameters.into_iter();
let result = descriptor.result;

assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::BYTE));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::CHAR));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::Byte));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::Char));
assert_eq!(
parameters.next().unwrap(),
FieldType::Base(BaseType::DOUBLE)
FieldType::Base(BaseType::Double)
);
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::FLOAT));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::INT));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::LONG));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::SHORT));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::Float));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::Int));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::Long));
assert_eq!(parameters.next().unwrap(), FieldType::Base(BaseType::Short));
assert_eq!(
parameters.next().unwrap(),
FieldType::Base(BaseType::BOOLEAN)
FieldType::Base(BaseType::Boolean)
);
assert!(parameters.next().is_none());
assert_eq!(result, ReturnDescriptor::Void);
Expand Down Expand Up @@ -366,7 +366,7 @@ mod tests {

assert_eq!(
parameters.next().unwrap(),
FieldType::Array(Box::new(FieldType::Base(BaseType::LONG)))
FieldType::Array(Box::new(FieldType::Base(BaseType::Long)))
);
assert!(parameters.next().is_none());
assert_eq!(result, ReturnDescriptor::Void);
Expand All @@ -383,7 +383,7 @@ mod tests {
assert_eq!(
parameters.next().unwrap(),
FieldType::Array(Box::new(FieldType::Array(Box::new(FieldType::Base(
BaseType::LONG
BaseType::Long
)))))
);
assert!(parameters.next().is_none());
Expand All @@ -401,17 +401,17 @@ mod tests {
assert!(parameters.next().is_none());
assert_eq!(
result,
ReturnDescriptor::Return(FieldType::Array(Box::new(FieldType::Base(BaseType::LONG))))
ReturnDescriptor::Return(FieldType::Array(Box::new(FieldType::Base(BaseType::Long))))
);
}

#[test]
fn test_display() {
let descriptor = MethodDescriptor {
parameters: vec![
FieldType::Base(BaseType::LONG),
FieldType::Base(BaseType::Long),
FieldType::Object(Cow::Borrowed("java/lang/Object")),
FieldType::Array(Box::new(FieldType::Base(BaseType::BYTE))),
FieldType::Array(Box::new(FieldType::Base(BaseType::Byte))),
],
result: ReturnDescriptor::Void,
};
Expand Down
9 changes: 1 addition & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::constant_pool::{
};
use crate::descriptor::{FieldType, MethodDescriptor, ReturnDescriptor};
pub use crate::error::ParseError;
use crate::names::{is_field_descriptor, is_method_descriptor, is_unqualified_name};
use crate::names::is_unqualified_name;

pub(crate) fn read_u1(bytes: &[u8], ix: &mut usize) -> Result<u8, ParseError> {
if bytes.len() < *ix + 1 {
Expand Down Expand Up @@ -159,10 +159,6 @@ fn read_fields<'a>(
}
let descriptor = read_cp_utf8(bytes, ix, pool)
.map_err(|e| err!(e, "descriptor of class field {}", i))?;
if !is_field_descriptor(&descriptor) {
fail!("Invalid descriptor for class field {}", i);
}

let descriptor = FieldType::parse(&descriptor)?;

let unique_id = (name.clone(), descriptor.clone());
Expand Down Expand Up @@ -230,9 +226,6 @@ fn read_methods<'a>(
}
let descriptor = read_cp_utf8(bytes, ix, pool)
.map_err(|e| err!(e, "descriptor of class method {}", i))?;
if !is_method_descriptor(&descriptor) {
fail!("Invalid descriptor for class method {}", i);
}
let descriptor = MethodDescriptor::parse(&descriptor)?;

if allow_init && name == "<init>" && descriptor.result != ReturnDescriptor::Void {
Expand Down

0 comments on commit 29b27d0

Please sign in to comment.