bindgen_jni/class_file_visitor/
method.rs

1//! [Java SE 7 § 4.6](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6):  Parsing APIs and structures for class methods.
2
3use super::*;
4
5
6
7bitflags! {
8    #[derive(Default)]
9    /// [Java SE 7 § 4.6](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6):  method_info::access_flags values.
10    pub struct MethodAccessFlags : u16 {
11        /// Declared `public`; may be accessed from outside its package.
12        const PUBLIC        = 0x0001;
13        /// Declared `private`; usable only with the defining class.
14        const PRIVATE       = 0x0002;
15        /// Declared `protectdd`; may be accessed within subclasses.
16        const PROTECTED     = 0x0004;
17        /// Declared `static`.
18        const STATIC        = 0x0008;
19        /// Declared `final`; no subclasses allowed.
20        const FINAL         = 0x0010;
21        /// Declared `syncronized`; invocation is wrapped by a monitor use.
22        const SYNCRONIZED   = 0x0020;
23        /// A bridge method, generated by the compiler.
24        const BRIDGE        = 0x0040;
25        /// Declared with variable number of arguments.
26        const VARARGS       = 0x0080;
27        /// Declared `native`; implemented in a langauge other than Java.
28        const NATIVE        = 0x0100;
29        /// Declared `abstract`; must not be instantiated.
30        const ABSTRACT      = 0x0400;
31        /// Declared `strictfp`; floating-point mode is FP-strict.
32        const STRICT        = 0x0800;
33        /// Declared synthetic; not present in the source code.
34        const SYNTHETIC     = 0x1000;
35    }
36}
37
38impl MethodAccessFlags {
39    pub fn read(r: &mut impl Read) -> io::Result<Self> {
40        Ok(Self::from_bits_truncate(read_u2(r)?))
41    }
42}
43
44
45
46/// [Java SE 7 &sect; 4.6](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6):  method_info, minus the trailing attributes
47#[derive(Clone, Copy, Debug)]
48pub struct Method {
49    pub access_flags:       MethodAccessFlags,
50    pub name_index:         u16,
51    pub descriptor_index:   u16,
52    pub attributes_count:   u16,
53}
54
55impl Method {
56    pub(crate) fn read_except_attributes(read: &mut impl Read) -> io::Result<Self> {
57        Ok(Self{
58            access_flags:       MethodAccessFlags::read(read)?,
59            name_index:         read_u2(read)?,
60            descriptor_index:   read_u2(read)?,
61            attributes_count:   read_u2(read)?,
62        })
63    }
64
65    pub(crate) fn read_list_visitor(read: &mut impl Read, visitor: &mut impl Visitor) -> io::Result<()> {
66        let method_count = read_u2(read)?;
67        for method_index in 0..method_count {
68            let method = Method::read_except_attributes(read)?;
69            visitor.on_method(method_index, method);
70            Attribute::read_list_callback(read, method.attributes_count, |attribute_index, attribute| visitor.on_method_attribute(method_index, attribute_index, attribute))?;
71        }
72        Ok(())
73    }
74}
75
76
77
78/// [Java SE 7 &sect; 4.6](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.6):  Visit a method_info
79pub trait Visitor {
80    fn on_method(&mut self, _index: u16, _method: Method) {}
81    fn on_method_attribute(&mut self, _method_index: u16, _attribute_index: u16, _attribute: Attribute) {}
82}
83