Skip to main content

Crate ristretto_classfile

Crate ristretto_classfile 

Source
Expand description

§Ristretto ClassFile

Code Coverage Benchmarks License Semantic Versioning

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§

ClassAccessFlags
Class access flags.
ClassFile
ClassFile represents the content of a Java .class file.
ConstantPool
Constant pool.
Field
Represents a field in a Java class file.
FieldAccessFlags
Field access flags used in Java class files to specify the access permissions and properties of fields.
Method
Method.
MethodAccessFlags
Method access flags used in Java class files to specify the access permissions and properties of methods.

Enums§

BaseType
Represents the primitive types in the Java Virtual Machine.
Constant
Constant
Error
Errors that can occur when processing JVM class files.
FieldType
Represents a Java field type descriptor as defined in the JVM specification.
ReferenceKind
Represents the behavior of a dynamic call site in the Java Virtual Machine.
Version
Implementation of Version based on ClassFile format for major/minor versions.
VersionSpecification
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.