{"id":18113862,"url":"https://github.com/jezza/class_file","last_synced_at":"2025-06-15T03:12:20.919Z","repository":{"id":57604574,"uuid":"163700517","full_name":"Jezza/class_file","owner":"Jezza","description":"A Rust library for parsing JVM class files.","archived":false,"fork":false,"pushed_at":"2019-03-14T21:38:11.000Z","size":22,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-17T00:57:07.150Z","etag":null,"topics":["bytecode","class","classfile","jvm","parser","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jezza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-31T22:24:13.000Z","updated_at":"2024-04-03T05:38:42.000Z","dependencies_parsed_at":"2022-09-12T21:42:24.844Z","dependency_job_id":null,"html_url":"https://github.com/Jezza/class_file","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Jezza/class_file","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jezza%2Fclass_file","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jezza%2Fclass_file/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jezza%2Fclass_file/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jezza%2Fclass_file/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jezza","download_url":"https://codeload.github.com/Jezza/class_file/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jezza%2Fclass_file/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259914929,"owners_count":22931331,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bytecode","class","classfile","jvm","parser","rust"],"created_at":"2024-11-01T02:10:08.593Z","updated_at":"2025-06-15T03:12:20.902Z","avatar_url":"https://github.com/Jezza.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"A library for parsing JVM classfiles.  \nIt uses nom to achieve its amazing speed, so go thank the author.    \n\nAs of writing, this crate is \"basically feature complete\".  \n\n * All standard attributes as declared within JVMS 11.  \n * All standard constant pool entries as declared within JVMS 11.  \n * Type checked constant pool indices  \n\nThere's not much to explain about the implementation.  \nIt uses nom and a lot of macros...  \nAnd not all from nom...  \n\nThis crate should be a complete one-to-one implementation of the spec.\n\nIf the spec says some binary structure defines this structure which defines this one, and so one.  \nThen in here, it'll be replicated.  \nThis crate is designed not as a user facing library, but to be used by other libraries.  \n\nIt's very low level.\n\nThere's a couple things to note:\n\nConstantPool indices carry information about their contents.  \nSo, if you get back an index into the constant pool, you will get back a specific value.  \n\nA really easy example to show it off is:\n\n```rust\nextern crate class_file;\n\nuse class_file::*;\n\nfn main() {\n\tlet data = include_bytes!(\"Main.class\");\n\tlet class_file = ClassFile::parse(data)\n    \t\t.expect(\"Failed to parse \\\"Main.class\\\"\")\n    \t\t.1; // This is needed because I actually return a nom result, which means you'll get back the remaining bytes.... Still undecided if I should hide that...\n\n\tlet cp = \u0026class_file.constant_pool;\n\n\t// So if you don't know, this_class is an index into the constant pool.\n\t// Specifically, it's an index to a ClassInfo at that location.\n\t// So the type of this_class is something like `CPIndex\u003c'_, ClassInfo\u003c'_\u003e\u003e`\n\tlet this_class = class_file.this_class;\n\n\t// This information is used when you try fetching data from the constant pool.\n\n\t// This returns a ClassInfo, and from there, you'll start going down the rabbit hole that is the specification.\n\tlet class_info = cp.index(this_class);\n\n\t// For example, to fetch the name of the class itself, you have to do something like:\n\tlet class_name: \u0026mstr = {\n\t\tlet this_class = class_file.this_class;\n\t\tlet class_info = cp.index(this_class)\n\t\t\t.expect(\"Unable to locate \\\"this_class\\\" inside of constant pool.\");\n\t\tlet info = cp.index(class_info.name_index)\n\t\t\t.expect(\"Unable to locate \\\"this_class.name_index\\\" in constant pool\");\n\t\tinfo.data\n\t};\n\n\t// But as you might have noticed, that's not a str, but a mstr.\n\t// This is because the JVM classfile uses MUTF8 strings.\n\t// So to save converting literally every string a classfile has, it's returned as a mstr.\n\t// You can easily call .to_utf8() on it, and it'll convert it to a `Cow\u003c'_, str\u003e`.\n\tprintln!(\"This class: {}\", class_name.to_utf8());\n\t// Side-note: mstr doesn't implement Display, because I'm a lazy fuck, but I'll get around to it...\n}\n```\n\nIf you're interested in a bit more, go take a look at the one test I have...  \nAlthough, saying that, it's basically the same as the code above...  \n\nA quite note:  \nCurrently, a couple of things are pub until I expand more of the internals and ergonomics of this crate...\n\nWhat other crazy things does this crate do...\n\nUh, it has constants for most things in the JVM.  \nMost things...  \nThere's some things that are missing, and I will add them in time.  \nI know, for example, there are some constants in the attr module that I haven't moved into there yet...  \n\nUh, what else...\n\nAttributes are just a trait thing, so you can implement your own Attribute.  \nIt's pretty easy if you use a macro.  \nActually, even if you don't, it's still pretty easy.  \nYou just need to map nom's result to an optional.  \nAnd actually implement the parsing part.  \n\nGoals\n---\n\nMUTF-8 handling isn't perfect.... I want to expand that and make it that much more seamless.\n\nThis is probably more crazy, but ideally, I want to be able to serialise the entire structure.  \nThe idea would be you can use this library to parse a class file, modify it, and then dump it back.  \nOr even more crazy, would be able to create these structures from scratch.  \nI have a couple of ideas with regard to that.  \n\nI was thinking of an objectasm-like library, and while that's still an option, I don't know if I like how it turned out.  \nThe visitor pattern played weirdly with Rust's semantics.  \nThe end result was it got really messy trying to what you'd normally use objectasm for.  \n\nSo, I think after I clean this and mutf8 up, I want to try another idea.  \nBasically, using the same techniques that the `syn` and `quote` crates have, create some macro library that accepts some syntax for creating and transforming classfiles.  \n\nJasmin falls so well into this category that it makes it a perfect target.  \nI'll look into that and see how it goes.  \nIdeally, I would want to combine this and this theoretical jasmin crate to create and modify classfiles with ease.\n\n* [ ] Improve MUTF-8 handling  \n* [ ] Start utilising this crate, perhaps with a quote-like crate with jasmin-like syntax.  \n\nI'll probably forget to change this list, but as of now, those are my main goals.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjezza%2Fclass_file","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjezza%2Fclass_file","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjezza%2Fclass_file/lists"}