pub struct ClassWriter { /* private fields */ }Expand description
A writer that generates a Java Class File structure.
This is the main entry point for creating class files programmatically. It allows visiting the class header, fields, methods, and attributes.
§Example
use rust_asm::{class_writer::{ClassWriter, COMPUTE_FRAMES}, opcodes};
let mut cw = ClassWriter::new(COMPUTE_FRAMES);
cw.visit(52, 0, 1, "com/example/MyClass", Some("java/lang/Object"), &[]);
let mut mv = cw.visit_method(1, "myMethod", "()V");
mv.visit_code();
mv.visit_insn(opcodes::RETURN);
mv.visit_maxs(0, 0); // Computed automatically due to COMPUTE_FRAMES
let bytes = cw.to_bytes().unwrap();Implementations§
Source§impl ClassWriter
impl ClassWriter
Sourcepub fn new(options: u32) -> Self
pub fn new(options: u32) -> Self
Creates a new ClassWriter.
§Arguments
options- Bitwise flags to control generation (e.g.,COMPUTE_FRAMES,COMPUTE_MAXS).
Sourcepub fn visit(
&mut self,
major: u16,
minor: u16,
access_flags: u16,
name: &str,
super_name: Option<&str>,
interfaces: &[&str],
) -> &mut Self
pub fn visit( &mut self, major: u16, minor: u16, access_flags: u16, name: &str, super_name: Option<&str>, interfaces: &[&str], ) -> &mut Self
Defines the header of the class.
§Arguments
major- The major version (e.g., 52 for Java 8).minor- The minor version.access_flags- Access modifiers (e.g., public, final).name- The internal name of the class (e.g., “java/lang/String”).super_name- The internal name of the super class (e.g.,java/lang/String,a/b/c). UseNoneforObject.interfaces- A list of interfaces implemented by this class.
Sourcepub fn visit_source_file(&mut self, name: &str) -> &mut Self
pub fn visit_source_file(&mut self, name: &str) -> &mut Self
Sets the source file name attribute for the class.
Sourcepub fn visit_method(
&mut self,
access_flags: u16,
name: &str,
descriptor: &str,
) -> MethodVisitor
pub fn visit_method( &mut self, access_flags: u16, name: &str, descriptor: &str, ) -> MethodVisitor
Visits a method of the class.
Returns a MethodVisitor that should be used to define the method body.
The visit_end method of the returned visitor must be called to attach it to the class.
Sourcepub fn visit_field(
&mut self,
access_flags: u16,
name: &str,
descriptor: &str,
) -> FieldVisitor
pub fn visit_field( &mut self, access_flags: u16, name: &str, descriptor: &str, ) -> FieldVisitor
Visits a field of the class.
Returns a FieldVisitor to define field attributes.
If visit_end is not called, the field is still committed when the visitor is dropped.
Sourcepub fn add_attribute(&mut self, attr: AttributeInfo) -> &mut Self
pub fn add_attribute(&mut self, attr: AttributeInfo) -> &mut Self
Adds a custom attribute to the class.
Sourcepub fn to_class_node(self) -> Result<ClassNode, String>
pub fn to_class_node(self) -> Result<ClassNode, String>
Converts the builder state into a ClassNode object model.
Sourcepub fn to_bytes(self) -> Result<Vec<u8>, ClassWriteError>
pub fn to_bytes(self) -> Result<Vec<u8>, ClassWriteError>
Generates the raw byte vector representing the .class file.
This method performs all necessary computations (stack map frames, max stack size)
based on the options provided in new.