Expand description
§Class File Reader
The reader module provides high-performance parsing of JVM .class binary files into the structured JvmProgram model.
§Features
- On-demand Reading: Supports partial reading of class components for better performance when only specific information (like class metadata) is needed.
- Robust Error Handling: Provides detailed diagnostics and error reporting for malformed or unsupported class files.
- Version Support: Compatible with all major JVM class file versions (Java 1.1 to latest).
- Constant Pool Resolution: Automatically resolves constant pool indices into human-readable strings and symbolic references.
§Usage Example
use jvm_assembler::formats::class::ClassReadConfig;
use jvm_assembler::formats::class::reader::ClassReader;
use std::fs::File;
use std::io::BufReader;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Open the .class file
let file = File::open("MyClass.class")?;
let reader = BufReader::new(file);
// 2. Configure the reader
let config = ClassReadConfig::default();
let class_reader = ClassReader::new(reader, &config);
// 3. Parse the class into a JvmProgram
let diagnostics = class_reader.read();
if let Ok(program) = diagnostics.result {
println!("Successfully parsed class: {}", program.name);
for method in &program.methods {
println!("Method: {} {}", method.name, method.descriptor);
}
} else {
eprintln!("Failed to parse class file");
for diag in diagnostics.diagnostics {
eprintln!("Error: {:?}", diag);
}
}
Ok(())
}§Internal Workflow
- Header Parsing: Validates the
0xCAFEBABEmagic number and reads version info. - Constant Pool Loading: Parses all entries in the constant pool.
- Metadata Extraction: Reads class access flags, name, superclass, and interfaces.
- Member Parsing: Iteratively parses fields and methods, including their attributes (like
CodeandSignature). - Model Construction: Maps the low-level binary structures to the high-level
JvmProgramIR.
Structs§
- Class
Reader - JVM Class 文件读取器