bindgen_jni/gather_java/
class_constants.rs

1use super::*;
2
3
4
5#[derive(Clone, Debug, Default)]
6pub(crate) struct ClassConstants(pub(crate) Vec<constant::Constant>);
7
8impl ClassConstants {
9    pub fn class(&self, index: u16) -> ClassRef {
10        let index = index as usize;
11        if index == 0 || index >= self.0.len() {
12            panic!("Constant {} is not a Class: Out of bounds", index);
13        }
14
15        let instance = &self.0[index];
16        if let constant::Constant::Class(ref class) = instance {
17            ClassRef { constants: &self, class }
18        } else {
19            panic!("Constant {} is not a Class: {:?}", index, instance);
20        }
21    }
22
23    pub fn field(&self, index: u16) -> FieldRef {
24        let index = index as usize;
25        if index == 0 || index >= self.0.len() {
26            panic!("Constant {} is not a FieldRef: Out of bounds", index);
27        }
28
29        let instance = &self.0[index];
30        if let constant::Constant::Fieldref(ref field) = instance {
31            FieldRef { constants: &self, field }
32        } else {
33            panic!("Constant {} is not a FieldRef: {:?}", index, instance);
34        }
35    }
36
37    pub fn method(&self, index: u16) -> MethodRef {
38        let index = index as usize;
39        if index == 0 || index >= self.0.len() {
40            panic!("Constant {} is not a MethodRef: Out of bounds", index);
41        }
42
43        let instance = &self.0[index];
44        if let constant::Constant::Methodref(ref method) = instance {
45            MethodRef { constants: &self, method }
46        } else {
47            panic!("Constant {} is not a MethodRef: {:?}", index, instance);
48        }
49    }
50
51    pub fn interface_method(&self, index: u16) -> InterfaceMethodRef {
52        let index = index as usize;
53        if index == 0 || index >= self.0.len() {
54            panic!("Constant {} is not a InterfaceMethodRef: Out of bounds", index);
55        }
56
57        let instance = &self.0[index];
58        if let constant::Constant::InterfaceMethodref(ref method) = instance {
59            InterfaceMethodRef { constants: &self, method }
60        } else {
61            panic!("Constant {} is not a InterfaceMethodRef: {:?}", index, instance);
62        }
63    }
64
65    pub(crate) fn name_and_type(&self, index: u16) -> NameAndTypeRef {
66        let index = index as usize;
67        if index == 0 || index >= self.0.len() {
68            panic!("Constant {} is not a NameAndType: Out of bounds", index);
69        }
70
71        let instance = &self.0[index];
72        if let constant::Constant::NameAndType(ref name_and_type) = instance {
73            NameAndTypeRef { constants: &self, name_and_type }
74        } else {
75            panic!("Constant {} is not a NameAndType: {:?}", index, instance);
76        }
77    }
78
79    pub(crate) fn utf8(&self, index: u16) -> &String {
80        let index = index as usize;
81        if index == 0 || index >= self.0.len() {
82            panic!("Constant {} is not a Utf8: Out of bounds", index);
83        }
84
85        let instance = &self.0[index];
86        if let constant::Constant::Utf8(ref utf8) = instance {
87            &utf8.0
88        } else {
89            panic!("Constant {} is not a Utf8: {:?}", index, instance);
90        }
91    }
92}
93
94
95
96pub struct ClassRef<'a> {
97    constants:  &'a ClassConstants,
98    class:      &'a constant::Class,
99}
100
101impl<'a> ClassRef<'a> {
102    pub fn name(&self) -> &'a String { self.constants.utf8(self.class.name_index) }
103}
104
105
106
107pub struct FieldRef<'a> {
108    constants:  &'a ClassConstants,
109    field:      &'a constant::Fieldref,
110}
111
112impl<'a> FieldRef<'a> {
113    pub fn class(&self) -> ClassRef<'a> { self.constants.class(self.field.class_index) }
114    pub fn name(&self) -> &'a String { self.constants.name_and_type(self.field.name_and_type_index).name() }
115    pub fn descriptor(&self) -> &'a String { self.constants.name_and_type(self.field.name_and_type_index).descriptor() }
116}
117
118
119
120pub struct MethodRef<'a> {
121    constants:  &'a ClassConstants,
122    method:     &'a constant::Methodref,
123}
124
125impl<'a> MethodRef<'a> {
126    pub fn class(&self) -> ClassRef<'a> { self.constants.class(self.method.class_index) }
127    pub fn name(&self) -> &'a String { self.constants.name_and_type(self.method.name_and_type_index).name() }
128    pub fn descriptor(&self) -> &'a String { self.constants.name_and_type(self.method.name_and_type_index).descriptor() }
129}
130
131
132
133pub struct InterfaceMethodRef<'a> {
134    constants:  &'a ClassConstants,
135    method:     &'a constant::InterfaceMethodref,
136}
137
138impl<'a> InterfaceMethodRef<'a> {
139    pub fn class(&self) -> ClassRef<'a> { self.constants.class(self.method.class_index) }
140    pub fn name(&self) -> &'a String { self.constants.name_and_type(self.method.name_and_type_index).name() }
141    pub fn descriptor(&self) -> &'a String { self.constants.name_and_type(self.method.name_and_type_index).descriptor() }
142}
143
144
145pub(crate) struct NameAndTypeRef<'a> {
146    constants:      &'a ClassConstants,
147    name_and_type:  &'a constant::NameAndType,
148}
149
150impl <'a> NameAndTypeRef<'a> {
151    pub fn name(&self)       -> &'a String { self.constants.utf8(self.name_and_type.name_index) }
152    pub fn descriptor(&self) -> &'a String { self.constants.utf8(self.name_and_type.descriptor_index) }
153}