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 {
|
fn read_method(constant_pool: Rc<Vec<CpEntry>>, index: &mut usize, bytecode: &Vec<u8>) -> Method {
|
||||||
let access_flags = get_u16(bytecode, *index);
|
let access_flags = get_u16(bytecode, *index);
|
||||||
let name_index = get_u16(bytecode, *index + 2) as usize;
|
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);
|
let attributes_count = get_u16(bytecode, *index + 6);
|
||||||
*index += 8;
|
*index += 8;
|
||||||
let mut attributes = vec![];
|
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 std::rc::Rc;
|
||||||
use crate::types::CpEntry::*;
|
use crate::types::CpEntry::*;
|
||||||
|
|
||||||
|
|
@ -62,16 +63,44 @@ pub struct Method {
|
||||||
pub constant_pool: Rc<Vec<CpEntry>>,
|
pub constant_pool: Rc<Vec<CpEntry>>,
|
||||||
pub access_flags: u16,
|
pub access_flags: u16,
|
||||||
pub name_index: usize,
|
pub name_index: usize,
|
||||||
pub descriptor_index: u16,
|
pub descriptor_index: usize,
|
||||||
pub attributes_count: u16,
|
pub attributes_count: u16,
|
||||||
pub attributes: Vec<Attribute>,
|
pub attributes: Vec<Attribute>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Method {
|
impl Method {
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> String {
|
||||||
if let Utf8(s) = &self.constant_pool[self.name_index - 1] {
|
let mut full_name = get_modifier(self.access_flags);
|
||||||
return &s;
|
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,3 +1,4 @@
|
||||||
|
mod test {
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use classfile_reader::types::{Attribute, Class, Field, Method};
|
use classfile_reader::types::{Attribute, Class, Field, Method};
|
||||||
use classfile_reader::types::CpEntry::*;
|
use classfile_reader::types::CpEntry::*;
|
||||||
|
|
@ -85,3 +86,4 @@ fn get_class() -> Class {
|
||||||
attributes: vec![Attribute { attribute_name_index: 27, info: vec![0, 28] }],
|
attributes: vec![Attribute { attribute_name_index: 27, info: vec![0, 28] }],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue