Skip to main content

rust_asm/
nodes.rs

1use crate::class_reader::{AttributeInfo, CodeAttribute, CpInfo};
2
3/// Represents a parsed Java Class File.
4///
5/// This structure holds the complete object model of a `.class` file, including
6/// its header information, constant pool, interfaces, fields, methods, and attributes.
7///
8/// # See Also
9/// * [JVM Specification: ClassFile Structure](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.1)
10#[derive(Debug, Clone)]
11pub struct ClassNode {
12    /// The minor version of the class file format.
13    pub minor_version: u16,
14
15    /// The major version of the class file format (e.g., 52 for Java 8, 61 for Java 17).
16    pub major_version: u16,
17
18    /// A bitmask of access flags used to denote access permissions to and properties of this class
19    /// (e.g., `ACC_PUBLIC`, `ACC_FINAL`, `ACC_INTERFACE`).
20    pub access_flags: u16,
21
22    /// The raw constant pool containing heterogeneous constants (strings, integers, method references, etc.).
23    /// Index 0 is reserved/unused.
24    pub constant_pool: Vec<CpInfo>,
25
26    /// The index into the constant pool pointing to a `CONSTANT_Class_info` structure representing this class.
27    pub this_class: u16,
28
29    /// The internal name of the class (e.g., `java/lang/String`).
30    pub name: String,
31
32    /// The internal name of the superclass (e.g., `java/lang/String`, `a/b/c`).
33    /// Returns `None` if this class is `java.lang.Object`.
34    pub super_name: Option<String>,
35
36    /// The name of the source file from which this class was compiled, if the `SourceFile` attribute was present.
37    pub source_file: Option<String>,
38
39    /// A list of internal names of the direct superinterfaces of this class or interface.
40    pub interfaces: Vec<String>,
41
42    /// A list of indices into the constant pool representing the direct superinterfaces.
43    pub interface_indices: Vec<u16>,
44
45    /// The fields declared by this class or interface.
46    pub fields: Vec<FieldNode>,
47
48    /// The methods declared by this class or interface.
49    pub methods: Vec<MethodNode>,
50
51    /// Global attributes associated with the class (e.g., `SourceFile`, `InnerClasses`, `EnclosingMethod`).
52    pub attributes: Vec<AttributeInfo>,
53}
54
55/// Represents a field (member variable) within a class.
56///
57/// # See Also
58/// * [JVM Specification: field_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.5)
59#[derive(Debug, Clone)]
60pub struct FieldNode {
61    /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_FINAL`).
62    pub access_flags: u16,
63
64    /// The constant pool index containing the name of the field.
65    pub name_index: u16,
66
67    /// The constant pool index containing the field descriptor.
68    pub descriptor_index: u16,
69
70    /// The name of the field.
71    pub name: String,
72
73    /// The field descriptor (e.g., `Ljava/lang/String;` or `I`).
74    pub descriptor: String,
75
76    /// Attributes associated with this field (e.g., `ConstantValue`, `Synthetic`, `Deprecated`, `Signature`).
77    pub attributes: Vec<AttributeInfo>,
78}
79
80/// Represents a method within a class.
81///
82/// # See Also
83/// * [JVM Specification: method_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.6)
84#[derive(Debug, Clone)]
85pub struct MethodNode {
86    /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_SYNCHRONIZED`).
87    pub access_flags: u16,
88
89    /// The constant pool index containing the name of the method (e.g., `<init>` or `main`).
90    pub name_index: u16,
91
92    /// The constant pool index containing the method descriptor (e.g., `([Ljava/lang/String;)V`).
93    pub descriptor_index: u16,
94
95    /// The name of the method.
96    pub name: String,
97
98    /// The method descriptor describing parameter types and return type.
99    pub descriptor: String,
100
101    /// The `Code` attribute containing the JVM bytecode instructions and exception handlers.
102    /// This will be `None` for `native` or `abstract` methods.
103    pub code: Option<CodeAttribute>,
104
105    /// Other attributes associated with this method (e.g., `Exceptions`, `Synthetic`, `Deprecated`, `Signature`).
106    /// Note that the `Code` attribute is stored separately in the `code` field for convenience.
107    pub attributes: Vec<AttributeInfo>,
108}
109
110
111
112
113
114