Expand description
§Ristretto ClassFile
Implementation of the JVM Class File Format that is used to read, write and verify Java classes.
Supports reading, writing, and verifying class files for any version of Java version up to 25.
§Examples
§Creating a Simple Class File
use ristretto_classfile::{ClassFile, ConstantPool, Version, JAVA_21};
let mut constant_pool = ConstantPool::default();
let this_class = constant_pool.add_class("Foo")?;
let class_file = ClassFile {
version: JAVA_21,
constant_pool,
this_class,
..Default::default()
};
class_file.verify()?;§Reading a Class File from Bytes
use ristretto_classfile::ClassFile;
use std::fs;
use std::io::Cursor;
// Read the bytes of a class file
let bytes = fs::read("path/to/Example.class")?;
// Parse the bytes into a ClassFile
let class_file = ClassFile::from_bytes(&mut Cursor::new(bytes))?;
// Now you can inspect the class
println!("Class name: {}", class_file.class_name()?);
println!("Class version: {}", class_file.version);§Writing a Class File to Bytes
use ristretto_classfile::{ClassFile, ConstantPool, Version, ClassAccessFlags, JAVA_21};
use std::fs;
use std::io::{Cursor, Write};
// Create a new class file
let mut constant_pool = ConstantPool::default();
let this_class = constant_pool.add_class("HelloWorld")?;
let super_class = constant_pool.add_class("java/lang/Object")?;
let class_file = ClassFile {
version: JAVA_21,
access_flags: ClassAccessFlags::PUBLIC,
constant_pool,
this_class,
super_class,
..Default::default()
};
// Verify the class file is valid
class_file.verify()?;
// Write the class file to a vector of bytes
let mut buffer = Vec::new();
class_file.to_bytes(&mut buffer)?;
// Now you can save these bytes to a file
fs::write("HelloWorld.class", buffer)?;Re-exports§
pub use verifiers::VerifyMode;
Modules§
- attributes
- Attributes Module
- mutf8
- Functions to convert between Rust strings and Java Modified UTF-8 byte arrays.
- verifiers
- Verifiers Module
Structs§
- Class
Access Flags - Class access flags.
- Class
File ClassFilerepresents the content of a Java .class file.- Constant
Pool - Constant pool.
- Field
- Represents a field in a Java class file.
- Field
Access Flags - Field access flags used in Java class files to specify the access permissions and properties of fields.
- Method
- Method.
- Method
Access Flags - Method access flags used in Java class files to specify the access permissions and properties of methods.
Enums§
- Base
Type - Represents the primitive types in the Java Virtual Machine.
- Constant
- Constant
- Error
- Errors that can occur when processing JVM class files.
- Field
Type - Represents a Java field type descriptor as defined in the JVM specification.
- Reference
Kind - Represents the behavior of a dynamic call site in the Java Virtual Machine.
- Version
- Implementation of Version based on
ClassFileformat for major/minor versions. - Version
Specification - Specifies a rule for matching Java versions.
Constants§
- JAVA_5
- Constants representing the Java version 5.0.
- JAVA_6
- Constants representing the Java version 6.
- JAVA_7
- Constants representing the Java version 7.
- JAVA_8
- Constants representing the Java version 8.
- JAVA_9
- Constants representing the Java version 9.
- JAVA_
1_ 0_ 2 - Constants representing the Java version 1.0.2.
- JAVA_
1_ 1 - Constants representing the Java version 1.1.
- JAVA_
1_ 2 - Constants representing the Java version 1.2.
- JAVA_
1_ 3 - Constants representing the Java version 1.3.
- JAVA_
1_ 4 - Constants representing the Java version 1.4.
- JAVA_10
- Constants representing the Java version 10.
- JAVA_11
- Constants representing the Java version 11.
- JAVA_12
- Constants representing the Java version 12.
- JAVA_13
- Constants representing the Java version 13.
- JAVA_14
- Constants representing the Java version 14.
- JAVA_15
- Constants representing the Java version 15.
- JAVA_16
- Constants representing the Java version 16.
- JAVA_17
- Constants representing the Java version 17.
- JAVA_18
- Constants representing the Java version 18.
- JAVA_19
- Constants representing the Java version 19.
- JAVA_20
- Constants representing the Java version 20.
- JAVA_21
- Constants representing the Java version 21.
- JAVA_22
- Constants representing the Java version 22.
- JAVA_23
- Constants representing the Java version 23.
- JAVA_24
- Constants representing the Java version 24.
- JAVA_25
- Constants representing the Java version 25.
- JAVA_
PREVIEW_ MINOR_ VERSION - Minor version number that indicates a Java preview release.
Type Aliases§
- Result
- A specialized Result type for Ristretto classfile operations.