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