Skip to main content

ClassWriter

Struct ClassWriter 

Source
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

Source

pub fn new(options: u32) -> Self

Creates a new ClassWriter.

§Arguments
  • options - Bitwise flags to control generation (e.g., COMPUTE_FRAMES, COMPUTE_MAXS).
Source

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). Use None for Object.
  • interfaces - A list of interfaces implemented by this class.
Source

pub fn visit_source_file(&mut self, name: &str) -> &mut Self

Sets the source file name attribute for the class.

Source

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.

Source

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.

Source

pub fn add_attribute(&mut self, attr: AttributeInfo) -> &mut Self

Adds a custom attribute to the class.

Source

pub fn to_class_node(self) -> Result<ClassNode, String>

Converts the builder state into a ClassNode object model.

Source

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.

Source

pub fn write_class_node( class_node: &ClassNode, options: u32, ) -> Result<Vec<u8>, ClassWriteError>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.