RPNXCortado
C++23 JVM class-file, bytecode, validation, and JAR toolkit
Loading...
Searching...
No Matches
Getting started

Parse, validate, and serialize a class

std::span< std::byte const > input = /* complete class-file bytes */;
if (!report.valid())
{
// Inspect report.diagnostics before attempting strict serialization.
}
std::vector< std::byte > output = rpnx::cortado::serialize_class_file(model);
Class-file serialization, deserialization, and iterator adapters.
std::vector< std::byte > serialize_class_file(class_file const &value, class_file_write_options const &options={})
Serializes a class file using the big-endian JVM class-file format.
class_file deserialize_class_file(std::span< std::byte const > bytes, class_file_read_options const &options={})
Deserializes exactly one class file and rejects trailing bytes.
validation_report validate_class_file(class_file const &value, validation_options const &options={})
Validates a complete class-file model without throwing for model errors.
An owning, typed representation of one JVM class file.
The complete bounded diagnostic result of one validation pass.
bool valid() const noexcept
Returns true when validation produced no diagnostics.

Use deserialize_class_file_prefix when a class file is embedded at the start of a larger byte range. It reports the exact number of consumed bytes. The ordinary deserialize_class_file entry point instead requires the input range to contain exactly one class file.

Generate a method

rpnx::cortado::class_file_builder type("example/Adder");
code.iload({0})
.iload({1})
.append< rpnx::cortado::opcode::iadd >()
.append< rpnx::cortado::opcode::ireturn >();
type.add_method(
"add",
"(II)I",
rpnx::cortado::method_access_flags::is_public |
rpnx::cortado::method_access_flags::is_static,
code);
rpnx::cortado::class_file model = type.build();
Deterministic constant-pool and symbolic class-file builders.
An owning symbolic class-file builder with a shared constant-pool interner.
Definition builders.hpp:673
An owning symbolic JVM code builder.
Definition builders.hpp:356

Branch targets must be labels created by the same code_builder. Bind every referenced label exactly once before calling finish indirectly through class_file_builder::add_method.

Work with JAR files

archive.entries.push_back({.name = "example/Adder.class", .data = class_bytes});
std::vector< std::byte > jar_bytes = rpnx::cortado::serialize_jar(archive);
JAR-compatible ZIP serialization and deserialization APIs.
std::vector< std::byte > serialize_jar(jar_file const &value)
Serializes an archive as a non-Zip64 JAR-compatible ZIP file.
An owning JAR archive representation.
Definition types.hpp:299
std::vector< jar_entry > entries
Entries in central-directory order; duplicate names remain distinct.
Definition types.hpp:300

Entry data is always uncompressed in memory. jar_entry::compression selects how serialization stores it in the ZIP stream. Deserialization inflates and verifies each entry before returning the owning jar_file.

Error handling

  • parse_error means input bytes cannot be decoded under the selected mode and limits.
  • validation_error means a structured model cannot be emitted as a valid class file.
  • assembly_error means symbolic bytecode cannot be resolved or analyzed.
  • unsupported_feature_error means the requested format or analysis case is deliberately outside the supported boundary.

The diagnostic vector returned by validate_class_file is for expected model validation failures. Exceptions remain appropriate for entry points that cannot return their requested result.