chamomile icon indicating copy to clipboard operation
chamomile copied to clipboard

Chamomile is a Java Virtual Machine class file assembler and disassembler.

project logo


Chamomile is a Java Virtual Machine class file assembler and disassembler.


Installation

Maven

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.github.wdestroier</groupId>
        <artifactId>chamomile</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

Gradle

repositories {
  maven("https://jitpack.io")
}

dependencies {
  implementation("com.github.wdestroier:chamomile:1.0.0")
}

Demonstration

Assemble a list of instructions

var assembler = new SinglePassAssembler();

var bytecode = assembler.assemble(List.of(
		new Iconst1Instruction(),
		new I2lInstruction(),
		new AconstNullInstruction(),
		new PopInstruction(),
		new Fconst2Instruction(),
		new F2lInstruction(),
		new LaddInstruction(),
		new InvokestaticInstruction(InstructionFactory.instance.getOpcode(
	InvokestaticInstruction.class), 12)
		));

System.out.println(Arrays.toString(bytecode));

Output

[4, 133, 1, 87, 13, 140, 97, 184, 0, 12]

Disassemble an array of "unsigned bytes"

var disassembler = new LinearSweepDisassembler();

var bytecode = new short[] { 4, 133, 1, 87, 13, 140, 97, 184, 0, 12 };

var instructions = disassembler.disassemble(bytecode);

instructions.stream()
	.map(instruction -> InstructionFactory.instance.getMnemonic(instruction.getOpcode()))
	.forEach(System.out::println);

Output

iconst_1
i2l
aconst_null
pop
fconst_2
f2l
ladd
invokestatic

More examples can be found here