diff --git a/loader/src/classfile_loader.rs b/loader/src/classfile_loader.rs index cb4aad2..7bfa5b6 100644 --- a/loader/src/classfile_loader.rs +++ b/loader/src/classfile_loader.rs @@ -1,12 +1,12 @@ use anyhow::{anyhow, Result}; use glob::glob; +use log::debug; use model::api::Parser; use model::class::JvmClass; use std::collections::HashMap; use std::fs::File; use std::io::BufReader; use std::path::{Path, PathBuf}; -use log::debug; use model::api::Classloader; @@ -28,8 +28,12 @@ impl ClassfileLoader { debug!("Parsing classfile {}", file_path.display()); - parser.parse(&mut reader).map(|class| (classpath, class)) - .map_err(|err| anyhow!("Failed to parse classfile {}: {}", file_path.display(), err)) + parser + .parse(&mut reader) + .map(|class| (classpath, class)) + .map_err(|err| { + anyhow!("Failed to parse classfile {}: {}", file_path.display(), err) + }) }) .collect::>()?; diff --git a/loader/src/jarfile_loader.rs b/loader/src/jarfile_loader.rs index 9455c02..d593808 100644 --- a/loader/src/jarfile_loader.rs +++ b/loader/src/jarfile_loader.rs @@ -1,5 +1,5 @@ -use anyhow::Result; use anyhow::anyhow; +use anyhow::Result; use model::api::Classloader; use model::api::Parser; use model::class::JvmClass; @@ -59,7 +59,9 @@ fn parse_classfiles( Some((path, parser.parse(&mut file))) }) .map(|(path, class_result)| { - class_result.map(|class| (path, class)).map_err(|err| anyhow!(err)) + class_result + .map(|class| (path, class)) + .map_err(|err| anyhow!(err)) }) .collect() } diff --git a/model/src/class.rs b/model/src/class.rs index 8032c38..d13f944 100644 --- a/model/src/class.rs +++ b/model/src/class.rs @@ -96,33 +96,33 @@ pub enum ClassConstant { pub enum FieldAccessFlag { /// Declared public; may be accesse from outside its package. Public, - + /// Declared private; usable only within the defining class. - Private, + Private, /// Declared protected; may be accessed within subclasses. - Protected, + Protected, /// Declared static. - Static, + Static, /// Declared final; never directly assigned to after object construction. - Final, + Final, /// Declared volatile; cannot be cached. - Volatile, + Volatile, /// Declared transient; not written or read by a persistent object manager. - Transient, + Transient, /// Declared synthetic; not present in the source code. - Synthetic, + Synthetic, /// Declared as an annotation type. Annotation, /// Declared as an element of an enum. - Enum, + Enum, } pub type ClassFields = Vec; diff --git a/model/src/class_impl.rs b/model/src/class_impl.rs index e38b657..2f2f30f 100644 --- a/model/src/class_impl.rs +++ b/model/src/class_impl.rs @@ -24,11 +24,7 @@ impl ClassConstants { ))?; match constant { &ClassConstant::Class(ref string) => Ok(string), - it => bail!( - "Expected Class but found {:?} at index {}", - it, - index - ), + it => bail!("Expected Class but found {:?} at index {}", it, index), } } } diff --git a/parser/src/attributes.rs b/parser/src/attributes.rs index 1690796..e117a13 100644 --- a/parser/src/attributes.rs +++ b/parser/src/attributes.rs @@ -6,8 +6,6 @@ pub fn map(classfile: &ClassFile, _constants: &ClassConstants) -> Result Result<(EnumSet, String, Option, Vec)> { +use model::class::{ClassAccessFlag, ClassConstants}; + +pub fn map( + classfile: &ClassFile, + constants: &ClassConstants, +) -> Result<( + EnumSet, + String, + Option, + Vec, +)> { let access_flags = Wrap(classfile.access_flags).try_into()?; let this_class = constants.get_class(classfile.this_class)?.into(); @@ -13,9 +21,11 @@ pub fn map(classfile: &ClassFile, constants: &ClassConstants) -> Result<(EnumSet None }; - let interfaces: Vec<_> = classfile.interfaces.iter().map(|index| - constants.get_class(*index).map(|s| s.to_owned()) - ).collect::>()?; + let interfaces: Vec<_> = classfile + .interfaces + .iter() + .map(|index| constants.get_class(*index).map(|s| s.to_owned())) + .collect::>()?; Ok((access_flags, this_class, super_class, interfaces)) } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index f4e1fff..e1b41e4 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -2,14 +2,14 @@ use anyhow::{bail, Result}; use model::class::{ClassVersion, JvmClass}; use std::io::Read; -mod type_signature; mod method_signature; +mod type_signature; +mod attributes; mod class_info; mod constants; mod fields; mod methods; -mod attributes; pub struct ClassfileParser {} diff --git a/parser/src/methods.rs b/parser/src/methods.rs index c160291..10b77c7 100644 --- a/parser/src/methods.rs +++ b/parser/src/methods.rs @@ -1,7 +1,7 @@ -use anyhow::{Result, bail}; +use anyhow::{bail, Result}; +use classfile_parser::{method_info::MethodAccessFlags, types::ClassFile}; use enumset::EnumSet; -use model::class::{ClassConstants, ClassMethods, ClassMethod, MethodAccessFlag}; -use classfile_parser::{types::ClassFile, method_info::MethodAccessFlags}; +use model::class::{ClassConstants, ClassMethod, ClassMethods, MethodAccessFlag}; use crate::method_signature::parse_method_signature; diff --git a/parser/src/type_signature.rs b/parser/src/type_signature.rs index f74bd34..8c93f64 100644 --- a/parser/src/type_signature.rs +++ b/parser/src/type_signature.rs @@ -1,7 +1,7 @@ -use std::str::Chars; use std::iter::Peekable; +use std::str::Chars; -use anyhow::{Result, anyhow, bail}; +use anyhow::{anyhow, bail, Result}; use model::class::TypeSignature; pub fn parse_type_signature(spec: &String) -> Result { @@ -27,4 +27,4 @@ pub(crate) fn parse_type(iterator: &mut Peekable) -> Result) -> String { iterator.take_while(|c| *c != ';').collect::() -} \ No newline at end of file +} diff --git a/parser/tests/class_with_fields_only.rs b/parser/tests/class_with_fields_only.rs index a29534c..41af90d 100644 --- a/parser/tests/class_with_fields_only.rs +++ b/parser/tests/class_with_fields_only.rs @@ -1,5 +1,5 @@ use enumset::EnumSet; -use model::class::{TypeSignature, ClassAccessFlag, FieldAccessFlag}; +use model::class::{ClassAccessFlag, FieldAccessFlag, TypeSignature}; mod test_utils; @@ -24,10 +24,7 @@ fn validate_against_javap() { assert_eq!("testdata/ClassOnlyWithFields", class.this_class); // super_class: #11 // java/lang/Object - assert_eq!( - Some("java/lang/Object".to_string()), - class.super_class - ); + assert_eq!(Some("java/lang/Object".to_string()), class.super_class); // interfaces: 0, fields: 5, methods: 2, attributes: 1 assert_eq!(0, class.interfaces.len()); diff --git a/rt/src/lib.rs b/rt/src/lib.rs index 559be1d..b1a3100 100644 --- a/rt/src/lib.rs +++ b/rt/src/lib.rs @@ -2,7 +2,10 @@ use loader::ClassfileLoader; use model::api::{Classloader, Parser}; pub fn make_classloader(parser: &impl Parser) -> impl Classloader { - let rt_path = std::env::current_dir().unwrap().join("..").join("rt/jmods/java.base/classes"); + let rt_path = std::env::current_dir() + .unwrap() + .join("..") + .join("rt/jmods/java.base/classes"); println!("### {:?}", rt_path); ClassfileLoader::open(rt_path, parser).unwrap() }