Skip to main content

rust_asm/
nodes.rs

1use crate::class_reader::{AttributeInfo, ExceptionTableEntry};
2use crate::class_reader::{LineNumber, LocalVariable, MethodParameter};
3use crate::constant_pool::CpInfo;
4use crate::insn::InsnList;
5use crate::insn::{AbstractInsnNode, TryCatchBlockNode};
6
7/// Represents a parsed Java Class File.
8///
9/// This structure holds the complete object model of a `.class` file, including
10/// its header information, constant pool, interfaces, fields, methods, and attributes.
11///
12/// # See Also
13/// * [JVM Specification: ClassFile Structure](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.1)
14#[derive(Debug, Clone)]
15pub struct ClassNode {
16    /// The minor version of the class file format.
17    pub minor_version: u16,
18
19    /// The major version of the class file format (e.g., 52 for Java 8, 61 for Java 17).
20    pub major_version: u16,
21
22    /// A bitmask of access flags used to denote access permissions to and properties of this class
23    /// (e.g., `ACC_PUBLIC`, `ACC_FINAL`, `ACC_INTERFACE`).
24    pub access_flags: u16,
25
26    /// The raw constant pool containing heterogeneous constants (strings, integers, method references, etc.).
27    /// Index 0 is reserved/unused.
28    pub constant_pool: Vec<CpInfo>,
29
30    /// The index into the constant pool pointing to a `CONSTANT_Class_info` structure representing this class.
31    pub this_class: u16,
32
33    /// The internal name of the class (e.g., `java/lang/String`).
34    pub name: String,
35
36    /// The internal name of the superclass (e.g., `java/lang/String`, `a/b/c`).
37    /// Returns `None` if this class is `java.lang.Object`.
38    pub super_name: Option<String>,
39
40    /// The name of the source file from which this class was compiled, if the `SourceFile` attribute was present.
41    pub source_file: Option<String>,
42
43    /// A list of internal names of the direct superinterfaces of this class or interface.
44    pub interfaces: Vec<String>,
45
46    /// A list of indices into the constant pool representing the direct superinterfaces.
47    pub interface_indices: Vec<u16>,
48
49    /// The fields declared by this class or interface.
50    pub fields: Vec<FieldNode>,
51
52    /// The methods declared by this class or interface.
53    pub methods: Vec<MethodNode>,
54
55    /// Global attributes associated with the class (e.g., `SourceFile`, `InnerClasses`, `EnclosingMethod`).
56    pub attributes: Vec<AttributeInfo>,
57
58    /// The inner class entries associated with this class file.
59    ///
60    /// This is a decoded view of the `InnerClasses` attribute.
61    pub inner_classes: Vec<InnerClassNode>,
62
63    /// The internal name of the enclosing class, if known.
64    ///
65    /// This value is empty when no enclosing class information is available.
66    pub outer_class: String,
67
68    /// Decoded JPMS module descriptor data for `module-info.class`, if present.
69    pub module: Option<ModuleNode>,
70}
71
72impl ClassNode {
73    pub fn new() -> Self {
74        Self {
75            minor_version: 0,
76            major_version: 0,
77            access_flags: 0,
78            constant_pool: Vec::new(),
79            this_class: 0,
80            name: String::new(),
81            super_name: None,
82            source_file: None,
83            interfaces: Vec::new(),
84            interface_indices: Vec::new(),
85            fields: Vec::new(),
86            methods: Vec::new(),
87            attributes: Vec::new(),
88            inner_classes: Vec::new(),
89            outer_class: String::new(),
90            module: None,
91        }
92    }
93}
94
95/// Represents an inner class entry in the `InnerClasses` attribute.
96#[derive(Debug, Clone)]
97pub struct InnerClassNode {
98    /// The internal name of the inner class (e.g., `a/b/Outer$Inner`).
99    pub name: String,
100
101    /// The internal name of the enclosing class, if any.
102    pub outer_name: Option<String>,
103
104    /// The simple (unqualified) name of the inner class, if any.
105    pub inner_name: Option<String>,
106
107    /// The access flags of the inner class as declared in source.
108    pub access_flags: u16,
109}
110
111/// Decoded JPMS module descriptor from the `Module` attribute family.
112#[derive(Debug, Clone)]
113pub struct ModuleNode {
114    /// Module name, e.g. `java.base` or `com.example.app`.
115    pub name: String,
116
117    /// Raw module access flags from the `Module` attribute header.
118    pub access_flags: u16,
119
120    /// Optional module version string.
121    pub version: Option<String>,
122
123    /// `requires` directives.
124    pub requires: Vec<ModuleRequireNode>,
125
126    /// `exports` directives.
127    pub exports: Vec<ModuleExportNode>,
128
129    /// `opens` directives.
130    pub opens: Vec<ModuleOpenNode>,
131
132    /// `uses` directives as internal class names.
133    pub uses: Vec<String>,
134
135    /// `provides ... with ...` directives.
136    pub provides: Vec<ModuleProvideNode>,
137
138    /// Optional `ModulePackages` attribute contents.
139    pub packages: Vec<String>,
140
141    /// Optional `ModuleMainClass` attribute contents.
142    pub main_class: Option<String>,
143}
144
145#[derive(Debug, Clone)]
146pub struct ModuleRequireNode {
147    pub module: String,
148    pub access_flags: u16,
149    pub version: Option<String>,
150}
151
152#[derive(Debug, Clone)]
153pub struct ModuleExportNode {
154    pub package: String,
155    pub access_flags: u16,
156    pub modules: Vec<String>,
157}
158
159#[derive(Debug, Clone)]
160pub struct ModuleOpenNode {
161    pub package: String,
162    pub access_flags: u16,
163    pub modules: Vec<String>,
164}
165
166#[derive(Debug, Clone)]
167pub struct ModuleProvideNode {
168    pub service: String,
169    pub providers: Vec<String>,
170}
171
172/// Represents a field (member variable) within a class.
173///
174/// # See Also
175/// * [JVM Specification: field_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.5)
176#[derive(Debug, Clone)]
177pub struct FieldNode {
178    /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_FINAL`).
179    pub access_flags: u16,
180
181    /// The name of the field.
182    pub name: String,
183
184    /// The field descriptor (e.g., `Ljava/lang/String;` or `I`).
185    pub descriptor: String,
186
187    /// Attributes associated with this field (e.g., `ConstantValue`, `Synthetic`, `Deprecated`, `Signature`).
188    pub attributes: Vec<AttributeInfo>,
189}
190
191/// Represents a method within a class.
192///
193/// # See Also
194/// * [JVM Specification: method_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.6)
195#[derive(Debug, Clone)]
196pub struct MethodNode {
197    /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_SYNCHRONIZED`).
198    pub access_flags: u16,
199
200    /// The name of the method.
201    pub name: String,
202
203    /// The method descriptor describing parameter types and return type.
204    pub descriptor: String,
205
206    /// Whether this method has a `Code` attribute.
207    /// This is `false` for `native` or `abstract` methods.
208    pub has_code: bool,
209
210    /// The maximum stack size required by the method's bytecode.
211    pub max_stack: u16,
212
213    /// The maximum number of local variables required by the method's bytecode.
214    pub max_locals: u16,
215
216    /// Decoded JVM instructions in an `InsnList`.
217    pub instructions: InsnList,
218
219    /// Original bytecode offsets corresponding to entries in `instructions`.
220    pub instruction_offsets: Vec<u16>,
221
222    /// Decoded JVM instructions as tree-style nodes, preserving labels and line markers.
223    pub insn_nodes: Vec<AbstractInsnNode>,
224
225    /// Exception handlers (raw entries in the code attribute).
226    pub exception_table: Vec<ExceptionTableEntry>,
227
228    /// Decoded try/catch blocks keyed by labels in `insn_nodes`.
229    pub try_catch_blocks: Vec<TryCatchBlockNode>,
230
231    /// Decoded line number entries from the `LineNumberTable`.
232    pub line_numbers: Vec<LineNumber>,
233
234    /// Decoded local variable entries from the `LocalVariableTable`.
235    pub local_variables: Vec<LocalVariable>,
236
237    /// Decoded method parameters from the `MethodParameters` attribute.
238    pub method_parameters: Vec<MethodParameter>,
239
240    /// Internal names of declared checked exceptions from the `Exceptions` attribute.
241    pub exceptions: Vec<String>,
242
243    /// Generic signature string from the `Signature` attribute, if present.
244    pub signature: Option<String>,
245
246    /// Attributes associated with the `Code` attribute (e.g., `LineNumberTable`, `LocalVariableTable`).
247    pub code_attributes: Vec<AttributeInfo>,
248
249    /// Other attributes associated with this method (e.g., `Exceptions`, `Synthetic`, `Deprecated`, `Signature`).
250    pub attributes: Vec<AttributeInfo>,
251}