lvm-class 0.7.2

JVM class file parser and writer for the Lagertha VM project
Documentation
//! Class file serialization module: converts `ClassFile` structures into `.class` bytecode.
//!
//! Gated behind the `write` feature flag.

mod attribute;
pub mod builder;
pub mod class_file;
mod constant_pool;
mod flags;
mod method;

use crate::ClassFile;

impl ClassFile {
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.extend(&ClassFile::MAGIC.to_be_bytes());
        bytes.extend(&self.minor_version.to_be_bytes());
        bytes.extend(&self.major_version.to_be_bytes());
        bytes.extend(&((self.cp.inner.len()) as u16).to_be_bytes());
        for entry in &self.cp.inner[1..] {
            entry.write_to(&mut bytes);
        }
        bytes.extend(&self.access_flags.get_raw().to_be_bytes());
        bytes.extend(&self.this_class.to_be_bytes());
        bytes.extend(&self.super_class.to_be_bytes());
        bytes.extend(&(self.interfaces.len() as u16).to_be_bytes());
        for entry in &self.interfaces {
            bytes.extend(&entry.to_be_bytes());
        }
        bytes.extend(&(self.fields.len() as u16).to_be_bytes());
        if !self.fields.is_empty() {
            todo!("assembling fields is not implemented yet");
        }
        bytes.extend(&(self.methods.len() as u16).to_be_bytes());
        for method in &self.methods {
            method.write_to(&mut bytes);
        }
        bytes.extend(&(self.attributes.len() as u16).to_be_bytes());
        for attr in &self.attributes {
            attr.write_to(&mut bytes);
        }
        bytes
    }
}