Giter Club home page Giter Club logo

javaresolver's Introduction

JavaResolver

JavaResolver 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.

JavaResolver is released under the MIT license.

Features

  • Create, read and edit any .class file using the JavaClassFile class.
  • Inspect and edit the constant pool.
  • Add, inspect, edit and remove members such as methods, fields and attributes.
  • Disassemble and assemble bytecode of methods (or arbitrary byte arrays).

Quick starters guide

Creating and reading class files

The JavaClassFile represents the basic raw structure of a class file. You can open one using for example:

var classFile = JavaClassFile.FromFile(@"C:\path\to\your\file.class");

Creating new class files can be done through the constructors

var classFile = new JavaClassFile();

The 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:

(Note: the following snippet is subject to change)

var classImage = new JavaClassImage(classFile);

Creating new class images can also be done directly, by simply calling the other constructor:

var classImage = new JavaClassImage(new ClassDefinition("MyClass"))
{
    SuperClass = new ClassReference("java/lang/Object"),
};

Fields and methods

Fields and methods can be obtained through the representative properties of JavaClassImage:

foreach (var field in classImage.Fields)
    Console.WriteLine(field.Name);

foreach (var method in classImage.Methods)
    Console.WriteLine(method.Name);

Fields and methods are represented using the FieldDefinition and MethodDefinition classes, and can be created using their constructors.

var field = new FieldDefinition("myIntField", new FieldDescriptor(BaseType.Int));
var method = new MethodDefinition("myMethod", new MethodDescriptor(BaseType.Void));

A 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:

foreach (var methodInfo in classFile.Methods) 
{
    string methodName = classFile.ConstantPool.ResolveString(methodInfo.NameIndex);
    Console.WriteLine(methodName);
    // ...
}

Inspecting method bodies

In high level mode, simply access the Body property of a MethodDefinition. It contains mutable collections for instructions, local variables, exception handlers and more:

var method = classImage.Methods.First(m => m.Name == "main");
foreach (var instruction in method.Body.Instructions)
    Console.WriteLine(instruction);

You 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:

var method = classFile.Methods.First(m => ...);

// Look up attribute:
var codeAttribute = method.Attributes.First(a => classFile.ConstantPool.ResolveString(a.NameIndex) == CodeAttribute.AttributeName);

// Deserialize contents:
var contents = CodeAttribute.FromReader(new MemoryBigEndianReader(codeAttribute.Contents));

// Disassemble bytecode:
var disassembler = new ByteCodeDisassembler(new MemoryBigEndianReader(contents.Code));
foreach (var instruction in disassembler.ReadInstructions())
    Console.WriteLine(instruction);

To write instructions, use the ByteCodeAssembler instead to get a byte[] of the new code.

Inspecting the raw constants pool:

Iterating over each constant defined in the pool can be done using:

var constantPool = classFile.ConstantPool

foreach (var constant in constantPool.Constants) 
{
    // ...
}

Resolving constant indices can be done through

var resolvedConstant = constantPool.ResolveConstant(index);

Since constants are often UTF8 string constants, there is a shortcut for it to make life a little bit easier:

string myString = constantPool.ResolveString(index);

javaresolver's People

Contributors

darkdaskin avatar dependabot[bot] avatar washi1337 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.