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    pub permitted_subclasses: Vec<String>,
72
73    pub record_components: Vec<RecordComponentNode>,
74}
75
76/// Represents an inner class entry in the `InnerClasses` attribute.
77#[derive(Debug, Clone)]
78pub struct InnerClassNode {
79    /// The internal name of the inner class (e.g., `a/b/Outer$Inner`).
80    pub name: String,
81
82    /// The internal name of the enclosing class, if any.
83    pub outer_name: Option<String>,
84
85    /// The simple (unqualified) name of the inner class, if any.
86    pub inner_name: Option<String>,
87
88    /// The access flags of the inner class as declared in source.
89    pub access_flags: u16,
90}
91
92/// Decoded JPMS module descriptor from the `Module` attribute family.
93#[derive(Debug, Clone)]
94pub struct ModuleNode {
95    /// Module name, e.g. `java.base` or `com.example.app`.
96    pub name: String,
97
98    /// Raw module access flags from the `Module` attribute header.
99    pub access_flags: u16,
100
101    /// Optional module version string.
102    pub version: Option<String>,
103
104    /// `requires` directives.
105    pub requires: Vec<ModuleRequireNode>,
106
107    /// `exports` directives.
108    pub exports: Vec<ModuleExportNode>,
109
110    /// `opens` directives.
111    pub opens: Vec<ModuleOpenNode>,
112
113    /// `uses` directives as internal class names.
114    pub uses: Vec<String>,
115
116    /// `provides ... with ...` directives.
117    pub provides: Vec<ModuleProvideNode>,
118
119    /// Optional `ModulePackages` attribute contents.
120    pub packages: Vec<String>,
121
122    /// Optional `ModuleMainClass` attribute contents.
123    pub main_class: Option<String>,
124}
125
126#[derive(Debug, Clone)]
127pub struct ModuleRequireNode {
128    pub module: String,
129    pub access_flags: u16,
130    pub version: Option<String>,
131}
132
133#[derive(Debug, Clone)]
134pub struct ModuleExportNode {
135    pub package: String,
136    pub access_flags: u16,
137    pub modules: Vec<String>,
138}
139
140#[derive(Debug, Clone)]
141pub struct ModuleOpenNode {
142    pub package: String,
143    pub access_flags: u16,
144    pub modules: Vec<String>,
145}
146
147#[derive(Debug, Clone)]
148pub struct ModuleProvideNode {
149    pub service: String,
150    pub providers: Vec<String>,
151}
152
153/// Represents a field (member variable) within a class.
154///
155/// # See Also
156/// * [JVM Specification: field_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.5)
157#[derive(Debug, Clone)]
158pub struct FieldNode {
159    /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_FINAL`).
160    pub access_flags: u16,
161
162    /// The name of the field.
163    pub name: String,
164
165    /// The field descriptor (e.g., `Ljava/lang/String;` or `I`).
166    pub descriptor: String,
167
168    /// Attributes associated with this field (e.g., `ConstantValue`, `Synthetic`, `Deprecated`, `Signature`).
169    pub attributes: Vec<AttributeInfo>,
170}
171
172/// Represents a method within a class.
173///
174/// # See Also
175/// * [JVM Specification: method_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.6)
176#[derive(Debug, Clone)]
177pub struct MethodNode {
178    /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_SYNCHRONIZED`).
179    pub access_flags: u16,
180
181    /// The name of the method.
182    pub name: String,
183
184    /// The method descriptor describing parameter types and return type.
185    pub descriptor: String,
186
187    /// Whether this method has a `Code` attribute.
188    /// This is `false` for `native` or `abstract` methods.
189    pub has_code: bool,
190
191    /// The maximum stack size required by the method's bytecode.
192    pub max_stack: u16,
193
194    /// The maximum number of local variables required by the method's bytecode.
195    pub max_locals: u16,
196
197    /// Decoded JVM instructions in an `InsnList`.
198    pub instructions: InsnList,
199
200    /// Original bytecode offsets corresponding to entries in `instructions`.
201    pub instruction_offsets: Vec<u16>,
202
203    /// Decoded JVM instructions as tree-style nodes, preserving labels and line markers.
204    pub insn_nodes: Vec<AbstractInsnNode>,
205
206    /// Exception handlers (raw entries in the code attribute).
207    pub exception_table: Vec<ExceptionTableEntry>,
208
209    /// Decoded try/catch blocks keyed by labels in `insn_nodes`.
210    pub try_catch_blocks: Vec<TryCatchBlockNode>,
211
212    /// Decoded line number entries from the `LineNumberTable`.
213    pub line_numbers: Vec<LineNumber>,
214
215    /// Decoded local variable entries from the `LocalVariableTable`.
216    pub local_variables: Vec<LocalVariable>,
217
218    /// Decoded method parameters from the `MethodParameters` attribute.
219    pub method_parameters: Vec<MethodParameter>,
220
221    /// Internal names of declared checked exceptions from the `Exceptions` attribute.
222    pub exceptions: Vec<String>,
223
224    /// Generic signature string from the `Signature` attribute, if present.
225    pub signature: Option<String>,
226
227    /// Attributes associated with the `Code` attribute (e.g., `LineNumberTable`, `LocalVariableTable`).
228    pub code_attributes: Vec<AttributeInfo>,
229
230    /// Other attributes associated with this method (e.g., `Exceptions`, `Synthetic`, `Deprecated`, `Signature`).
231    pub attributes: Vec<AttributeInfo>,
232}
233
234#[derive(Debug, Clone)]
235pub struct RecordComponentNode {
236    /// The name of the record component
237    pub name: String,
238
239    /// The record component descriptor (e.g., `Ljava/lang/String;` or `I`).
240    pub descriptor: String,
241
242    /// Attributes associated with this component (e.g., `ConstantValue`, `Synthetic`, `Deprecated`, `Signature`).
243    pub attributes: Vec<AttributeInfo>,
244}