pclass_parser/classfile/
method_info.rs

1use crate::classfile::attributes::{Code, CodeException, LineNumber, LocalVariable, StackMapFrame, Type};
2use crate::classfile::constant_pool;
3use crate::classfile::types::{BytesRef, ConstantPool, U2};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone)]
7pub struct MethodInfo {
8    pub acc_flags: U2,
9    pub name_index: U2,
10    pub desc_index: U2,
11    pub attrs: Vec<Type>,
12}
13
14impl MethodInfo {
15    pub fn get_code(&self) -> Option<Code> {
16        for it in self.attrs.iter() {
17            if let Type::Code(code) = it {
18                return Some(code.clone());
19            }
20        }
21
22        None
23    }
24
25    pub fn get_line_number_table(&self) -> Vec<LineNumber> {
26        let mut line_num_table = Vec::new();
27
28        self.attrs.iter().for_each(|attr| {
29            if let Type::Code(code) = attr {
30                code.attrs.iter().for_each(|it| {
31                    if let Type::LineNumberTable {tables} = it {
32                        line_num_table.extend_from_slice(tables.as_slice());
33                    }
34                });
35            }
36        });
37
38        line_num_table
39    }
40
41    pub fn get_throws(&self) -> Option<Vec<U2>> {
42        for it in self.attrs.iter() {
43            if let Type::Exceptions { exceptions } = it {
44                return Some(exceptions.clone());
45            }
46        }
47
48        None
49    }
50
51    pub fn get_ex_table(&self) -> Option<Vec<CodeException>> {
52        for it in self.attrs.iter() {
53            if let Type::Code(code) = it {
54                if !code.exceptions.is_empty() {
55                    return Some(code.exceptions.clone())
56                }
57            }
58        }
59
60        None
61    }
62
63    pub fn get_stack_map_table(&self) -> Option<Vec<StackMapFrame>> {
64        if let Some(code) = self.get_code() {
65            for it in code.attrs.iter() {
66                if let Type::StackMapTable { entries} = it {
67                    return Some(entries.clone());
68                }
69            }
70        }
71
72        None
73    }
74
75    pub fn get_local_variable_table(&self) -> Option<Vec<LocalVariable>> {
76        if let Some(code) = self.get_code() {
77            for it in code.attrs.iter() {
78                if let Type::LocalVariableTable { tables } = it {
79                    return Some(tables.clone());
80                }
81            }
82        }
83
84        None
85    }
86
87    pub fn get_local_variable_type_table(&self) -> Option<Vec<LocalVariable>> {
88        if let Some(code) = self.get_code() {
89            for it in code.attrs.iter() {
90                if let Type::LocalVariableTypeTable { tables } = it {
91                    return Some(tables.clone());
92                }
93            }
94        }
95
96        None
97    }
98}