java_class_parser/structures/
class_entries.rs

1use crate::attributes::Attribute;
2use crate::raw_java_class::{RawAttributeInfo, RawFieldInfo, RawMethodInfo};
3use crate::structures::class::JavaClass;
4use crate::utility::match_as;
5use crate::{ConstantPoolInfo, HasAttributes, Signature};
6
7/// A field in a class
8#[derive(Debug)]
9pub struct Field<'a> {
10    entry: Entry<'a>,
11}
12
13impl<'a> Field<'a> {
14    pub(crate) fn new(field_info: &'a RawFieldInfo, java_class: &'a JavaClass) -> Self {
15        Self {
16            entry: Entry::new(
17                java_class,
18                field_info.name_index,
19                field_info.descriptor_index,
20                &field_info.attributes,
21            ),
22        }
23    }
24
25    /// The name of the field
26    pub fn name(&self) -> &'a str {
27        self.entry.name
28    }
29    /// The signature of the field
30    pub fn signature(&self) -> &Signature<'a> {
31        &self.entry.signature
32    }
33}
34
35impl HasAttributes for Field<'_> {
36    type Iter<'a> = <Vec<Attribute<'a>> as IntoIterator>::IntoIter where Self: 'a;
37
38    fn attributes<'a>(&'a self) -> Self::Iter<'a> {
39        self.entry.attributes.clone().into_iter()
40    }
41}
42
43/// A field in a class
44#[derive(Debug)]
45pub struct Method<'a> {
46    entry: Entry<'a>,
47}
48
49impl<'a> Method<'a> {
50    pub(crate) fn new(method_info: &'a RawMethodInfo, java_class: &'a JavaClass) -> Self {
51        Self {
52            entry: Entry::new(
53                java_class,
54                method_info.name_index,
55                method_info.descriptor_index,
56                &method_info.attributes,
57            ),
58        }
59    }
60
61    /// The name of the method
62    pub fn name(&self) -> &'a str {
63        self.entry.name
64    }
65    /// The signature of the method
66    pub fn signature(&self) -> &Signature<'a> {
67        &self.entry.signature
68    }
69}
70
71impl HasAttributes for Method<'_> {
72    type Iter<'a> = <Vec<Attribute<'a>> as IntoIterator>::IntoIter where Self: 'a;
73
74    fn attributes<'a>(&'a self) -> Self::Iter<'a> {
75        self.entry.attributes.clone().into_iter()
76    }
77}
78
79#[derive(Debug)]
80struct Entry<'a> {
81    name: &'a str,
82    signature: Signature<'a>,
83    attributes: Vec<Attribute<'a>>,
84}
85
86impl<'a> Entry<'a> {
87    fn new(
88        java_class: &'a JavaClass,
89        name_index: u16,
90        descriptor_index: u16,
91        attributes: &'a [RawAttributeInfo],
92    ) -> Self {
93        let name = match_as!(name; Some(ConstantPoolInfo::Utf8(name)) = java_class.get_at_index(name_index)).expect("invalid").as_ref();
94        let signature = java_class
95            .get_descriptor(descriptor_index)
96            .expect("should be a valid descriptor");
97
98        let attributes = attributes
99            .iter()
100            .map(|s| {
101                java_class
102                    .create_attribute(s.attribute_name_index, &s.info)
103                    .expect("couldn't create attribute")
104            })
105            .collect::<Vec<_>>();
106
107        Self {
108            name,
109            signature,
110            attributes,
111        }
112    }
113}