rust_asm/nodes.rs
1use crate::class_reader::{AttributeInfo, CpInfo, ExceptionTableEntry};
2use crate::insn::InsnList;
3
4/// Represents a parsed Java Class File.
5///
6/// This structure holds the complete object model of a `.class` file, including
7/// its header information, constant pool, interfaces, fields, methods, and attributes.
8///
9/// # See Also
10/// * [JVM Specification: ClassFile Structure](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.1)
11#[derive(Debug, Clone)]
12pub struct ClassNode {
13 /// The minor version of the class file format.
14 pub minor_version: u16,
15
16 /// The major version of the class file format (e.g., 52 for Java 8, 61 for Java 17).
17 pub major_version: u16,
18
19 /// A bitmask of access flags used to denote access permissions to and properties of this class
20 /// (e.g., `ACC_PUBLIC`, `ACC_FINAL`, `ACC_INTERFACE`).
21 pub access_flags: u16,
22
23 /// The raw constant pool containing heterogeneous constants (strings, integers, method references, etc.).
24 /// Index 0 is reserved/unused.
25 pub constant_pool: Vec<CpInfo>,
26
27 /// The index into the constant pool pointing to a `CONSTANT_Class_info` structure representing this class.
28 pub this_class: u16,
29
30 /// The internal name of the class (e.g., `java/lang/String`).
31 pub name: String,
32
33 /// The internal name of the superclass (e.g., `java/lang/String`, `a/b/c`).
34 /// Returns `None` if this class is `java.lang.Object`.
35 pub super_name: Option<String>,
36
37 /// The name of the source file from which this class was compiled, if the `SourceFile` attribute was present.
38 pub source_file: Option<String>,
39
40 /// A list of internal names of the direct superinterfaces of this class or interface.
41 pub interfaces: Vec<String>,
42
43 /// A list of indices into the constant pool representing the direct superinterfaces.
44 pub interface_indices: Vec<u16>,
45
46 /// The fields declared by this class or interface.
47 pub fields: Vec<FieldNode>,
48
49 /// The methods declared by this class or interface.
50 pub methods: Vec<MethodNode>,
51
52 /// Global attributes associated with the class (e.g., `SourceFile`, `InnerClasses`, `EnclosingMethod`).
53 pub attributes: Vec<AttributeInfo>,
54
55 /// The inner class entries associated with this class file.
56 ///
57 /// This is a decoded view of the `InnerClasses` attribute.
58 pub inner_classes: Vec<InnerClassNode>,
59
60 /// The internal name of the enclosing class, if known.
61 ///
62 /// This value is empty when no enclosing class information is available.
63 pub outer_class: String,
64}
65
66impl ClassNode {
67 pub fn new() -> Self {
68 Self {
69 minor_version: 0,
70 major_version: 0,
71 access_flags: 0,
72 constant_pool: Vec::new(),
73 this_class: 0,
74 name: String::new(),
75 super_name: None,
76 source_file: None,
77 interfaces: Vec::new(),
78 interface_indices: Vec::new(),
79 fields: Vec::new(),
80 methods: Vec::new(),
81 attributes: Vec::new(),
82 inner_classes: Vec::new(),
83 outer_class: String::new(),
84 }
85 }
86}
87
88/// Represents an inner class entry in the `InnerClasses` attribute.
89#[derive(Debug, Clone)]
90pub struct InnerClassNode {
91 /// The internal name of the inner class (e.g., `a/b/Outer$Inner`).
92 pub name: String,
93
94 /// The internal name of the enclosing class, if any.
95 pub outer_name: Option<String>,
96
97 /// The simple (unqualified) name of the inner class, if any.
98 pub inner_name: Option<String>,
99
100 /// The access flags of the inner class as declared in source.
101 pub access_flags: u16,
102}
103
104/// Represents a field (member variable) within a class.
105///
106/// # See Also
107/// * [JVM Specification: field_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.5)
108#[derive(Debug, Clone)]
109pub struct FieldNode {
110 /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_FINAL`).
111 pub access_flags: u16,
112
113 /// The constant pool index containing the name of the field.
114 pub name_index: u16,
115
116 /// The constant pool index containing the field descriptor.
117 pub descriptor_index: u16,
118
119 /// The name of the field.
120 pub name: String,
121
122 /// The field descriptor (e.g., `Ljava/lang/String;` or `I`).
123 pub descriptor: String,
124
125 /// Attributes associated with this field (e.g., `ConstantValue`, `Synthetic`, `Deprecated`, `Signature`).
126 pub attributes: Vec<AttributeInfo>,
127}
128
129/// Represents a method within a class.
130///
131/// # See Also
132/// * [JVM Specification: method_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.6)
133#[derive(Debug, Clone)]
134pub struct MethodNode {
135 /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_SYNCHRONIZED`).
136 pub access_flags: u16,
137
138 /// The name of the method.
139 pub name: String,
140
141 /// The method descriptor describing parameter types and return type.
142 pub descriptor: String,
143
144 /// Whether this method has a `Code` attribute.
145 /// This is `false` for `native` or `abstract` methods.
146 pub has_code: bool,
147
148 /// The maximum stack size required by the method's bytecode.
149 pub max_stack: u16,
150
151 /// The maximum number of local variables required by the method's bytecode.
152 pub max_locals: u16,
153
154 /// Decoded JVM instructions in an `InsnList`.
155 pub instructions: InsnList,
156
157 /// Exception handlers (raw entries in the code attribute).
158 pub exception_table: Vec<ExceptionTableEntry>,
159
160 /// Attributes associated with the `Code` attribute (e.g., `LineNumberTable`, `LocalVariableTable`).
161 pub code_attributes: Vec<AttributeInfo>,
162
163 /// Other attributes associated with this method (e.g., `Exceptions`, `Synthetic`, `Deprecated`, `Signature`).
164 pub attributes: Vec<AttributeInfo>,
165}