jawa.util.bytecode module

Utilities for reading & writing JVM method bytecode.

class jawa.util.bytecode.Instruction[source]

Bases: jawa.util.bytecode.Instruction

Represents a single JVM instruction, consisting of an opcode and its potential operands.

classmethod create(mnemonic_or_op, operands=None)[source]
details

Extended opcode information.

name

Alias for mnemonic.

size_on_disk(start_pos=0)[source]

Returns the size of this instruction and its operands when packed. start_pos is required for the tableswitch and lookupswitch instruction as the padding depends on alignment.

wide

True if this instruction needs to be prefixed by the WIDE opcode.

class jawa.util.bytecode.Operand(op_type, value)

Bases: tuple

op_type

Alias for field number 0

value

Alias for field number 1

class jawa.util.bytecode.OperandFmts[source]

Bases: enum.Enum

An enumeration.

BYTE = <Struct object>
INTEGER = <Struct object>
SHORT = <Struct object>
UBYTE = <Struct object>
USHORT = <Struct object>
class jawa.util.bytecode.OperandTypes[source]

Bases: enum.IntEnum

Constants used to determine the “type” of operand on an opcode, such as a BRANCH [offset] or a LITERAL [value].

BRANCH = 40
CONSTANT_INDEX = 30
LITERAL = 10
LOCAL_INDEX = 20
PADDING = 50
jawa.util.bytecode.load_bytecode_definitions(*, path=None) → dict[source]

Load bytecode definitions from JSON file.

If no path is provided the default bytecode.json will be loaded.

Parameters:path – Either None or a path to a JSON file to load containing bytecode definitions.
jawa.util.bytecode.read_instruction(fio, start_pos)[source]

Reads a single instruction from fio and returns it, or None if the stream is empty.

Parameters:
  • fio – Any file-like object providing read().
  • start_pos – The current position in the stream.
jawa.util.bytecode.write_instruction(fout, start_pos, ins)[source]

Writes a single instruction of opcode with operands to fout.

Parameters:
  • fout – Any file-like object providing write().
  • start_pos – The current position in the stream.
  • ins – The Instruction to write.