1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::classfile::attributes::{Code, CodeException, LineNumber, LocalVariable, StackMapFrame, Type};
use crate::classfile::constant_pool;
use crate::classfile::types::{BytesRef, ConstantPool, U2};
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct MethodInfo {
    pub acc_flags: U2,
    pub name_index: U2,
    pub desc_index: U2,
    pub attrs: Vec<Type>,
}

impl MethodInfo {
    pub fn get_code(&self) -> Option<Code> {
        for it in self.attrs.iter() {
            if let Type::Code(code) = it {
                return Some(code.clone());
            }
        }

        None
    }

    pub fn get_line_number_table(&self) -> Vec<LineNumber> {
        let mut line_num_table = Vec::new();

        self.attrs.iter().for_each(|attr| {
            if let Type::Code(code) = attr {
                code.attrs.iter().for_each(|it| {
                    if let Type::LineNumberTable {tables} = it {
                        line_num_table.extend_from_slice(tables.as_slice());
                    }
                });
            }
        });

        line_num_table
    }

    pub fn get_throws(&self) -> Option<Vec<U2>> {
        for it in self.attrs.iter() {
            if let Type::Exceptions { exceptions } = it {
                return Some(exceptions.clone());
            }
        }

        None
    }

    pub fn get_ex_table(&self) -> Option<Vec<CodeException>> {
        for it in self.attrs.iter() {
            if let Type::Code(code) = it {
                if !code.exceptions.is_empty() {
                    return Some(code.exceptions.clone())
                }
            }
        }

        None
    }

    pub fn get_stack_map_table(&self) -> Option<Vec<StackMapFrame>> {
        if let Some(code) = self.get_code() {
            for it in code.attrs.iter() {
                if let Type::StackMapTable { entries} = it {
                    return Some(entries.clone());
                }
            }
        }

        None
    }

    pub fn get_local_variable_table(&self) -> Option<Vec<LocalVariable>> {
        if let Some(code) = self.get_code() {
            for it in code.attrs.iter() {
                if let Type::LocalVariableTable { tables } = it {
                    return Some(tables.clone());
                }
            }
        }

        None
    }

    pub fn get_local_variable_type_table(&self) -> Option<Vec<LocalVariable>> {
        if let Some(code) = self.get_code() {
            for it in code.attrs.iter() {
                if let Type::LocalVariableTypeTable { tables } = it {
                    return Some(tables.clone());
                }
            }
        }

        None
    }
}