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 index into the constant pool pointing to a `CONSTANT_Class_info` structure representing the direct superclass.
30 /// This is 0 for `java.lang.Object`.
31 pub super_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. Returns `None` if this class is `java.lang.Object`.
37 pub super_name: Option<String>,
38
39 /// The name of the source file from which this class was compiled, if the `SourceFile` attribute was present.
40 pub source_file: Option<String>,
41
42 /// A list of internal names of the direct superinterfaces of this class or interface.
43 pub interfaces: Vec<String>,
44
45 /// A list of indices into the constant pool representing the direct superinterfaces.
46 pub interface_indices: Vec<u16>,
47
48 /// The fields declared by this class or interface.
49 pub fields: Vec<FieldNode>,
50
51 /// The methods declared by this class or interface.
52 pub methods: Vec<MethodNode>,
53
54 /// Global attributes associated with the class (e.g., `SourceFile`, `InnerClasses`, `EnclosingMethod`).
55 pub attributes: Vec<AttributeInfo>,
56}
57
58/// Represents a field (member variable) within a class.
59///
60/// # See Also
61/// * [JVM Specification: field_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.5)
62#[derive(Debug, Clone)]
63pub struct FieldNode {
64 /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_FINAL`).
65 pub access_flags: u16,
66
67 /// The constant pool index containing the name of the field.
68 pub name_index: u16,
69
70 /// The constant pool index containing the field descriptor.
71 pub descriptor_index: u16,
72
73 /// The name of the field.
74 pub name: String,
75
76 /// The field descriptor (e.g., `Ljava/lang/String;` or `I`).
77 pub descriptor: String,
78
79 /// Attributes associated with this field (e.g., `ConstantValue`, `Synthetic`, `Deprecated`, `Signature`).
80 pub attributes: Vec<AttributeInfo>,
81}
82
83/// Represents a method within a class.
84///
85/// # See Also
86/// * [JVM Specification: method_info](https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.6)
87#[derive(Debug, Clone)]
88pub struct MethodNode {
89 /// A bitmask of access flags (e.g., `ACC_PUBLIC`, `ACC_STATIC`, `ACC_SYNCHRONIZED`).
90 pub access_flags: u16,
91
92 /// The constant pool index containing the name of the method (e.g., `<init>` or `main`).
93 pub name_index: u16,
94
95 /// The constant pool index containing the method descriptor (e.g., `([Ljava/lang/String;)V`).
96 pub descriptor_index: u16,
97
98 /// The name of the method.
99 pub name: String,
100
101 /// The method descriptor describing parameter types and return type.
102 pub descriptor: String,
103
104 /// The `Code` attribute containing the JVM bytecode instructions and exception handlers.
105 /// This will be `None` for `native` or `abstract` methods.
106 pub code: Option<CodeAttribute>,
107
108 /// Other attributes associated with this method (e.g., `Exceptions`, `Synthetic`, `Deprecated`, `Signature`).
109 /// Note that the `Code` attribute is stored separately in the `code` field for convenience.
110 pub attributes: Vec<AttributeInfo>,
111}