From 5f4bd8a02eb0c1f0d41058755d0b6adf21718da5 Mon Sep 17 00:00:00 2001 From: Kartikaya Gupta Date: Fri, 20 Jun 2025 02:26:44 -0400 Subject: [PATCH] Add another example program --- examples/classrefs.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/classrefs.rs diff --git a/examples/classrefs.rs b/examples/classrefs.rs new file mode 100644 index 0000000..5bd4106 --- /dev/null +++ b/examples/classrefs.rs @@ -0,0 +1,30 @@ +use std::env; +use std::fs::File; +use std::io::Read; + +use cafebabe::constant_pool::ConstantPoolItem::ClassInfo; +use cafebabe::constant_pool::ConstantPoolItem::FieldRef; +use cafebabe::constant_pool::ConstantPoolItem::InterfaceMethodRef; +use cafebabe::constant_pool::ConstantPoolItem::MethodRef; + +fn main() { + for arg in env::args().skip(1) { + let mut file = File::open(&arg).unwrap(); + let mut bytes = Vec::new(); + file.read_to_end(&mut bytes).unwrap(); + match cafebabe::parse_class(&bytes) { + Ok(class) => { + for cp in class.constantpool_iter() { + match cp { + ClassInfo(c) => println!("{}", c), + FieldRef(r) | MethodRef(r) | InterfaceMethodRef(r) => { + println!("{}", r.class_name) + } + _ => (), + } + } + } + Err(e) => eprintln!("Error: {} when parsing {:?}", e, arg), + }; + } +}