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 from_mnemonic(mnemonic, operands=None)[source]
classmethod from_opcode(opcode, operands=None)[source]
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.OperandTypes[source]

Bases: object

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.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.bytecode.opcode_table = {0: ('nop', None), 1: ('aconst_null', None), 2: ('iconst_m1', None), 3: ('iconst_0', None), 4: ('iconst_1', None), 5: ('iconst_2', None), 6: ('iconst_3', None), 7: ('iconst_4', None), 8: ('iconst_5', None), 9: ('lconst_0', None), 10: ('lconst_1', None), 11: ('fconst_0', None), 12: ('fconst_1', None), 13: ('fconst_2', None), 14: ('dconst_0', None), 15: ('dconst_1', None), 16: ('bipush', [(<Struct object>, 10)]), 17: ('sipush', [(<Struct object>, 10)]), 18: ('ldc', [(<Struct object>, 30)]), 19: ('ldc_w', [(<Struct object>, 30)]), 20: ('ldc2_w', [(<Struct object>, 30)]), 21: ('iload', [(<Struct object>, 20)]), 22: ('lload', [(<Struct object>, 20)]), 23: ('fload', [(<Struct object>, 20)]), 24: ('dload', [(<Struct object>, 20)]), 25: ('aload', [(<Struct object>, 20)]), 26: ('iload_0', None), 27: ('iload_1', None), 28: ('iload_2', None), 29: ('iload_3', None), 30: ('lload_0', None), 31: ('lload_1', None), 32: ('lload_2', None), 33: ('lload_3', None), 34: ('fload_0', None), 35: ('fload_1', None), 36: ('fload_2', None), 37: ('fload_3', None), 38: ('dload_0', None), 39: ('dload_1', None), 40: ('dload_2', None), 41: ('dload_3', None), 42: ('aload_0', None), 43: ('aload_1', None), 44: ('aload_2', None), 45: ('aload_3', None), 46: ('iaload', None), 47: ('laload', None), 48: ('faload', None), 49: ('daload', None), 50: ('aaload', None), 51: ('baload', None), 52: ('caload', None), 53: ('saload', None), 54: ('istore', [(<Struct object>, 20)]), 55: ('lstore', [(<Struct object>, 20)]), 56: ('fstore', [(<Struct object>, 20)]), 57: ('dstore', [(<Struct object>, 20)]), 58: ('astore', [(<Struct object>, 20)]), 59: ('istore_0', None), 60: ('istore_1', None), 61: ('istore_2', None), 62: ('istore_3', None), 63: ('lstore_0', None), 64: ('lstore_1', None), 65: ('lstore_2', None), 66: ('lstore_3', None), 67: ('fstore_0', None), 68: ('fstore_1', None), 69: ('fstore_2', None), 70: ('fstore_3', None), 71: ('dstore_0', None), 72: ('dstore_1', None), 73: ('dstore_2', None), 74: ('dstore_3', None), 75: ('astore_0', None), 76: ('astore_1', None), 77: ('astore_2', None), 78: ('astore_3', None), 79: ('iastore', None), 80: ('lastore', None), 81: ('fastore', None), 82: ('dastore', None), 83: ('aastore', None), 84: ('bastore', None), 85: ('castore', None), 86: ('sastore', None), 87: ('pop', None), 88: ('pop2', None), 89: ('dup', None), 90: ('dup_x1', None), 91: ('dup_x2', None), 92: ('dup2', None), 93: ('dup2_x1', None), 94: ('dup2_x2', None), 95: ('swap', None), 96: ('iadd', None), 97: ('ladd', None), 98: ('fadd', None), 99: ('dadd', None), 100: ('isub', None), 101: ('lsub', None), 102: ('fsub', None), 103: ('dsub', None), 104: ('imul', None), 105: ('lmul', None), 106: ('fmul', None), 107: ('dmul', None), 108: ('idiv', None), 109: ('ldiv', None), 110: ('fdiv', None), 111: ('ddiv', None), 112: ('irem', None), 113: ('lrem', None), 114: ('frem', None), 115: ('drem', None), 116: ('ineg', None), 117: ('lneg', None), 118: ('fneg', None), 119: ('dneg', None), 120: ('ishl', None), 121: ('lshl', None), 122: ('ishr', None), 123: ('lshr', None), 124: ('iushr', None), 125: ('lushr', None), 126: ('iand', None), 127: ('land', None), 128: ('ior', None), 129: ('lor', None), 130: ('ixor', None), 131: ('lxor', None), 132: ('iinc', [(<Struct object>, 20), (<Struct object>, 10)]), 133: ('i2l', None), 134: ('i2f', None), 135: ('i2d', None), 136: ('l2i', None), 137: ('l2f', None), 138: ('l2d', None), 139: ('f2i', None), 140: ('f2l', None), 141: ('f2d', None), 142: ('d2i', None), 143: ('d2l', None), 144: ('d2f', None), 145: ('ib2', None), 146: ('i2c', None), 147: ('i2s', None), 148: ('lcmp', None), 149: ('fcmpl', None), 150: ('fcmpg', None), 151: ('dcmpl', None), 152: ('dcmpg', None), 153: ('ifeq', [(<Struct object>, 40)]), 154: ('ifne', [(<Struct object>, 40)]), 155: ('iflt', [(<Struct object>, 40)]), 156: ('ifge', [(<Struct object>, 40)]), 157: ('ifgt', [(<Struct object>, 40)]), 158: ('ifle', [(<Struct object>, 40)]), 159: ('if_icmpeq', [(<Struct object>, 40)]), 160: ('if_icmpne', [(<Struct object>, 40)]), 161: ('if_icmplt', [(<Struct object>, 40)]), 162: ('if_icmpge', [(<Struct object>, 40)]), 163: ('if_icmpgt', [(<Struct object>, 40)]), 164: ('if_icmple', [(<Struct object>, 40)]), 165: ('if_acmpeq', [(<Struct object>, 40)]), 166: ('if_acmpne', [(<Struct object>, 40)]), 167: ('goto', [(<Struct object>, 40)]), 168: ('jsr', [(<Struct object>, 40)]), 169: ('ret', [(<Struct object>, 20)]), 170: ('tableswitch', None), 171: ('lookupswitch', None), 172: ('ireturn', None), 173: ('lreturn', None), 174: ('freturn', None), 175: ('dreturn', None), 176: ('areturn', None), 177: ('return', None), 178: ('getstatic', [(<Struct object>, 30)]), 179: ('putstatic', [(<Struct object>, 30)]), 180: ('getfield', [(<Struct object>, 30)]), 181: ('putfield', [(<Struct object>, 30)]), 182: ('invokevirtual', [(<Struct object>, 30)]), 183: ('invokespecial', [(<Struct object>, 30)]), 184: ('invokestatic', [(<Struct object>, 30)]), 185: ('invokeinterface', [(<Struct object>, 30), (<Struct object>, 10), (<Struct object>, 50)]), 187: ('new', [(<Struct object>, 30)]), 188: ('newarray', [(<Struct object>, 10)]), 189: ('anewarray', [(<Struct object>, 10)]), 190: ('arraylength', None), 191: ('athrow', None), 192: ('checkcast', [(<Struct object>, 30)]), 193: ('instanceof', [(<Struct object>, 30)]), 194: ('monitorenter', None), 195: ('monitorexit', None), 196: ('wide', None), 197: ('multianewarray', [(<Struct object>, 30), (<Struct object>, 10)]), 198: ('ifnull', [(<Struct object>, 40)]), 199: ('ifnonnull', [(<Struct object>, 40)]), 200: ('goto_w', [(<Struct object>, 40)]), 201: ('jsr_w', [(<Struct object>, 40)]), 202: ('breakpoint', None), 254: ('impdep1', None), 255: ('impdep2', None)}

An opcode to mnemonic & operand format mapping for every opcode supported by the JVM.

jawa.util.bytecode.definition_from_mnemonic(mnemonic)[source]

Returns the definition of an instruction by its mnemonic in the form: (opcode, operand_fmt)

jawa.util.bytecode.definition_from_opcode(opcode)[source]

Returns the definition of an instruction by its opcode in the form: (mnemonic, operand_fmt)

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)[source]

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

jawa.util.descriptor.method_descriptor(descriptor)[source]

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

jawa.util.descriptor.parse_descriptor(descriptor)[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

Convience 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)[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)[source]

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

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