added modifiers
This commit is contained in:
parent
724b2f3eaa
commit
c2cebaedad
3 changed files with 118 additions and 87 deletions
|
|
@ -163,7 +163,7 @@ fn read_field(index: &mut usize, bytecode: &Vec<u8>) -> Field {
|
|||
fn read_method(constant_pool: Rc<Vec<CpEntry>>, index: &mut usize, bytecode: &Vec<u8>) -> Method {
|
||||
let access_flags = get_u16(bytecode, *index);
|
||||
let name_index = get_u16(bytecode, *index + 2) as usize;
|
||||
let descriptor_index = get_u16(bytecode, *index + 4);
|
||||
let descriptor_index = get_u16(bytecode, *index + 4) as usize;
|
||||
let attributes_count = get_u16(bytecode, *index + 6);
|
||||
*index += 8;
|
||||
let mut attributes = vec![];
|
||||
|
|
|
|||
39
src/types.rs
39
src/types.rs
|
|
@ -1,3 +1,4 @@
|
|||
use std::borrow::ToOwned;
|
||||
use std::rc::Rc;
|
||||
use crate::types::CpEntry::*;
|
||||
|
||||
|
|
@ -62,16 +63,44 @@ pub struct Method {
|
|||
pub constant_pool: Rc<Vec<CpEntry>>,
|
||||
pub access_flags: u16,
|
||||
pub name_index: usize,
|
||||
pub descriptor_index: u16,
|
||||
pub descriptor_index: usize,
|
||||
pub attributes_count: u16,
|
||||
pub attributes: Vec<Attribute>,
|
||||
}
|
||||
|
||||
impl Method {
|
||||
pub fn name(&self) -> &str {
|
||||
if let Utf8(s) = &self.constant_pool[self.name_index - 1] {
|
||||
return &s;
|
||||
pub fn name(&self) -> String {
|
||||
let mut full_name = get_modifier(self.access_flags);
|
||||
if let Utf8(s) = &self.constant_pool[&self.name_index - 1] {
|
||||
full_name.push_str(s);
|
||||
}
|
||||
panic!() // name must be utf8
|
||||
if let Utf8(s) = &self.constant_pool[&self.descriptor_index - 1] {
|
||||
full_name.push_str(s);
|
||||
}
|
||||
|
||||
|
||||
full_name
|
||||
}
|
||||
}
|
||||
|
||||
const MODIFIERS: [(u16, &'static str); 12] = [
|
||||
(0x0001, "public "),
|
||||
(0x0002, "private "),
|
||||
(0x0004, "protected "),
|
||||
(0x0008, "static "),
|
||||
(0x0010, "final "),
|
||||
(0x0020, "synchronized "),
|
||||
(0x0040, "volatile "),
|
||||
(0x0080, "transient "),
|
||||
(0x0100, "native "),
|
||||
(0x0200, "interface "),
|
||||
(0x0400, "interface "),
|
||||
(0x0800, "strict ")];
|
||||
|
||||
pub fn get_modifier (value: u16) -> String {
|
||||
let mut output = String::new();
|
||||
for m in MODIFIERS {
|
||||
if value & m.0 == m.0 { output.push_str(&m.1) }
|
||||
}
|
||||
output
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,21 @@
|
|||
use std::rc::Rc;
|
||||
use classfile_reader::types::{Attribute, Class, Field, Method};
|
||||
use classfile_reader::types::CpEntry::*;
|
||||
mod test {
|
||||
use std::rc::Rc;
|
||||
use classfile_reader::types::{Attribute, Class, Field, Method};
|
||||
use classfile_reader::types::CpEntry::*;
|
||||
|
||||
#[test]
|
||||
fn get_version() {
|
||||
#[test]
|
||||
fn get_version() {
|
||||
assert_eq!((55, 0), get_class().get_version());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_methods() {
|
||||
#[test]
|
||||
fn get_methods() {
|
||||
for m in get_class().get_methods() {
|
||||
println!("{}", m.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_class() -> Class {
|
||||
fn get_class() -> Class {
|
||||
let cp = Rc::new(vec![MethodRef(2, 3),
|
||||
ClassRef(4),
|
||||
NameAndType(5, 6),
|
||||
|
|
@ -84,4 +85,5 @@ fn get_class() -> Class {
|
|||
Field { access_flags: 18, name_index: 15, descriptor_index: 16, attributes_count: 0, attributes: vec![] }],
|
||||
attributes: vec![Attribute { attribute_name_index: 27, info: vec![0, 28] }],
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue