Utility APIs

jawa.util.bytecode module

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.

jawa.util.descriptor module

Methods for parsing standard JVM type descriptors for fields and methods.

class jawa.util.descriptor.JVMType(base_type, dimensions, name)

Bases: tuple

base_type

Alias for field number 0

dimensions

Alias for field number 1

name

Alias for field number 2

class jawa.util.descriptor.MethodDescriptor(returns, args, returns_descriptor, args_descriptor, descriptor)

Bases: tuple

args

Alias for field number 1

args_descriptor

Alias for field number 3

descriptor

Alias for field number 4

returns

Alias for field number 0

returns_descriptor

Alias for field number 2

jawa.util.descriptor.field_descriptor(descriptor: str) → str[source]

Parses a Field descriptor as described in section 4.3.2 of the JVM specification.

jawa.util.descriptor.method_descriptor(descriptor: str) → jawa.util.descriptor.MethodDescriptor[source]

Parses a Method descriptor as described in section 4.3.3 of the JVM specification.

jawa.util.descriptor.parse_descriptor(descriptor: str) → list[source]

Uses a tiny state machine to parse JVM descriptors. To get useful wrappers around the results, use jawa.core.descriptor.method_descriptor() or jawa.core.descriptor.field_descriptor().

jawa.util.flags module

class jawa.util.flags.Flags(binary_format, flags)[source]

Bases: object

Convenience class for handling bit flags.

get(name)[source]

Returns the value of the field name.

pack()[source]

A shortcut for struct.pack(flag.binary_format, flag.value).

set(name, value)[source]

Sets the value of the field name to value, which is True or False.

to_dict()[source]

Returns this Flags object’s fields as a dictionary.

unpack(source)[source]

A shortcut for struct.unpack(flag.binary_format, <bytes>).

value

The numeric value of the bitfield.

jawa.util.utf module

Utility methods for handling oddities in character encoding encountered when parsing and writing JVM ClassFiles or object serialization archives.

Note

http://bugs.python.org/issue2857 was an attempt in 2008 to get support for MUTF-8/CESU-8 into the python core.

jawa.util.utf.decode_modified_utf8(s: bytes) → str[source]

Decodes a bytestring containing modified UTF-8 as defined in section 4.4.7 of the JVM specification.

Parameters:s – bytestring to be converted.
Returns:A unicode representation of the original string.
jawa.util.utf.encode_modified_utf8(u: str) → bytearray[source]

Encodes a unicode string as modified UTF-8 as defined in section 4.4.7 of the JVM specification.

Parameters:u – unicode string to be converted.
Returns:A decoded bytearray.