jvm_assembler/program/
entities.rs1use super::{
2 instructions::JvmInstruction,
3 pool::JvmConstantPool,
4 types::{JvmAccessFlags, JvmAttribute, JvmField, JvmVersion},
5};
6
7#[derive(Debug, Clone)]
9pub struct JvmProgram {
10 pub name: String,
12 pub access_flags: JvmAccessFlags,
14 pub super_class: Option<String>,
16 pub interfaces: Vec<String>,
18 pub fields: Vec<JvmField>,
20 pub methods: Vec<JvmMethod>,
22 pub attributes: Vec<JvmAttribute>,
24 pub constant_pool: JvmConstantPool,
26 pub version: JvmVersion,
28 pub source_file: Option<String>,
30}
31
32impl JvmProgram {
33 pub fn new(name: String) -> Self {
35 Self {
36 name,
37 access_flags: JvmAccessFlags::public(),
38 super_class: None,
39 interfaces: Vec::new(),
40 fields: Vec::new(),
41 methods: Vec::new(),
42 attributes: Vec::new(),
43 constant_pool: JvmConstantPool::new(),
44 version: JvmVersion::java_8(),
45 source_file: None,
46 }
47 }
48
49 pub fn set_version(&mut self, major: u16, minor: u16) {
51 self.version = JvmVersion::new(major, minor);
52 }
53
54 pub fn set_super_class(&mut self, super_class: String) {
56 self.super_class = Some(super_class);
57 }
58
59 pub fn add_interface(&mut self, interface: String) {
61 self.interfaces.push(interface);
62 }
63
64 pub fn add_method(&mut self, method: JvmMethod) {
66 self.methods.push(method);
67 }
68
69 pub fn add_field(&mut self, field: JvmField) {
71 self.fields.push(field);
72 }
73
74 pub fn add_attribute(&mut self, attribute: JvmAttribute) {
76 self.attributes.push(attribute);
77 }
78
79 pub fn set_source_file(&mut self, filename: String) {
81 self.source_file = Some(filename.clone());
82 self.add_attribute(JvmAttribute::SourceFile { filename });
83 }
84
85 pub fn validate(&self) -> bool {
87 if self.name.is_empty() {
89 return false;
90 }
91 true
92 }
93}
94
95#[derive(Debug, Clone)]
97pub struct JvmMethod {
98 pub name: String,
100 pub descriptor: String,
102 pub access_flags: JvmAccessFlags,
104 pub instructions: Vec<JvmInstruction>,
106 pub max_stack: u16,
108 pub max_locals: u16,
110 pub exception_handlers: Vec<JvmExceptionHandler>,
112 pub exceptions: Vec<String>,
114 pub attributes: Vec<JvmAttribute>,
116}
117
118impl JvmMethod {
119 pub fn new(name: String, descriptor: String) -> Self {
121 Self {
122 name,
123 descriptor,
124 access_flags: JvmAccessFlags::public(),
125 instructions: Vec::new(),
126 max_stack: 0,
127 max_locals: 0,
128 exception_handlers: Vec::new(),
129 exceptions: Vec::new(),
130 attributes: Vec::new(),
131 }
132 }
133
134 pub fn add_instruction(&mut self, inst: JvmInstruction) {
136 self.instructions.push(inst);
137 }
138
139 pub fn add_exception_handler(&mut self, handler: JvmExceptionHandler) {
141 self.exception_handlers.push(handler);
142 }
143
144 pub fn add_exception(&mut self, exception: String) {
146 self.exceptions.push(exception);
147 }
148
149 pub fn add_attribute(&mut self, attribute: JvmAttribute) {
151 self.attributes.push(attribute);
152 }
153}
154
155#[derive(Debug, Clone)]
157pub struct JvmExceptionHandler {
158 pub start_label: String,
160 pub end_label: String,
162 pub handler_label: String,
164 pub catch_type: Option<String>,
166}