{"id":13638218,"url":"https://github.com/Washi1337/JavaResolver","last_synced_at":"2025-04-19T17:33:22.792Z","repository":{"id":52663269,"uuid":"165414700","full_name":"Washi1337/JavaResolver","owner":"Washi1337","description":"Java class file inspection library for .NET. ","archived":true,"fork":false,"pushed_at":"2021-04-21T20:01:29.000Z","size":164,"stargazers_count":49,"open_issues_count":0,"forks_count":14,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-11-09T08:39:10.587Z","etag":null,"topics":["bytecode","bytecode-manipulation","class","dotnet","java","jvm","metadata"],"latest_commit_sha":null,"homepage":null,"language":"C#","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/Washi1337.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-12T17:17:35.000Z","updated_at":"2024-08-11T08:19:59.000Z","dependencies_parsed_at":"2022-08-22T03:00:54.900Z","dependency_job_id":null,"html_url":"https://github.com/Washi1337/JavaResolver","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Washi1337%2FJavaResolver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Washi1337%2FJavaResolver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Washi1337%2FJavaResolver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Washi1337%2FJavaResolver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Washi1337","download_url":"https://codeload.github.com/Washi1337/JavaResolver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249750171,"owners_count":21320086,"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","bytecode-manipulation","class","dotnet","java","jvm","metadata"],"created_at":"2024-08-02T01:00:41.959Z","updated_at":"2025-04-19T17:33:22.524Z","avatar_url":"https://github.com/Washi1337.png","language":"C#","readme":"JavaResolver\n============\n\nJavaResolver is a Java class file inspection library allowing .NET programmers to read, modify and write Java class files. The library allows for low level access of the `.class` file format (e.g. direct access to the constants pool and raw member and attribute structures), as well as a higher level representation that provides a more hierarchical view on the metadata.\n\nJavaResolver is released under the MIT license.\n\nFeatures\n========\n- Create, read and edit any `.class` file using the `JavaClassFile` class.\n- Inspect and edit the constant pool.\n- Add, inspect, edit and remove members such as methods, fields and attributes.\n- Disassemble and assemble bytecode of methods (or arbitrary byte arrays).\n\nQuick starters guide\n====================\n\nCreating and reading class files\n--------------------------------\nThe `JavaClassFile` represents the basic raw structure of a class file. You can open one using for example:\n```csharp\nvar classFile = JavaClassFile.FromFile(@\"C:\\path\\to\\your\\file.class\");\n```\n\nCreating new class files can be done through the constructors\n```csharp\nvar classFile = new JavaClassFile();\n```\n\nThe `JavaClassFile` is a __low level representations__ of the class file. If you want a more higher level representation for easier access, you have to open a new `JavaClassImage` from the `JavaClassFile`:\n\n(Note: the following snippet is subject to change)\n```csharp\nvar classImage = new JavaClassImage(classFile);\n```\n\nCreating new class images can also be done directly, by simply calling the other constructor:\n```csharp\nvar classImage = new JavaClassImage(new ClassDefinition(\"MyClass\"))\n{\n    SuperClass = new ClassReference(\"java/lang/Object\"),\n};\n```\n\nFields and methods\n----------------------\nFields and methods can be obtained through the representative properties of `JavaClassImage`:\n```csharp\nforeach (var field in classImage.Fields)\n    Console.WriteLine(field.Name);\n\nforeach (var method in classImage.Methods)\n    Console.WriteLine(method.Name);\n```\n\nFields and methods are represented using the `FieldDefinition` and `MethodDefinition` classes, and can be created using their constructors.\n```csharp\nvar field = new FieldDefinition(\"myIntField\", new FieldDescriptor(BaseType.Int));\nvar method = new MethodDefinition(\"myMethod\", new MethodDescriptor(BaseType.Void));\n```\n\nA more low level approach, where we iterate over raw method, field and attribute structures can be done through the representative properties of the `JavaClassFile` class:\n```csharp\nforeach (var methodInfo in classFile.Methods) \n{\n    string methodName = classFile.ConstantPool.ResolveString(methodInfo.NameIndex);\n    Console.WriteLine(methodName);\n    // ...\n}\n```\n\nInspecting method bodies\n------------------------\nIn high level mode, simply access the `Body` property of a `MethodDefinition`. It contains __mutable__ collections for instructions, local variables, exception handlers and more:\n\n```csharp\nvar method = classImage.Methods.First(m =\u003e m.Name == \"main\");\nforeach (var instruction in method.Body.Instructions)\n    Console.WriteLine(instruction);\n```\n\nYou can also opt for a more low level approach. Java stores the method body as an attribute in the raw method info structure with the name `\"Code\"`. You can find it yourself using:\n```csharp\nvar method = classFile.Methods.First(m =\u003e ...);\n\n// Look up attribute:\nvar codeAttribute = method.Attributes.First(a =\u003e classFile.ConstantPool.ResolveString(a.NameIndex) == CodeAttribute.AttributeName);\n\n// Deserialize contents:\nvar contents = CodeAttribute.FromReader(new MemoryBigEndianReader(codeAttribute.Contents));\n\n// Disassemble bytecode:\nvar disassembler = new ByteCodeDisassembler(new MemoryBigEndianReader(contents.Code));\nforeach (var instruction in disassembler.ReadInstructions())\n    Console.WriteLine(instruction);\n```\n\nTo write instructions, use the `ByteCodeAssembler` instead to get a `byte[]` of the new code.\n\nInspecting the raw constants pool:\n------------------------------\nIterating over each constant defined in the pool can be done using:\n```csharp\nvar constantPool = classFile.ConstantPool\n\nforeach (var constant in constantPool.Constants) \n{\n    // ...\n}\n```\nResolving constant indices can be done through\n```csharp\nvar resolvedConstant = constantPool.ResolveConstant(index);\n```\n\nSince constants are often UTF8 string constants, there is a shortcut for it to make life a little bit easier:\n```csharp\nstring myString = constantPool.ResolveString(index);\n```\n\n","funding_links":[],"categories":["C# #"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWashi1337%2FJavaResolver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FWashi1337%2FJavaResolver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWashi1337%2FJavaResolver/lists"}