jawa.attributes.code module

class jawa.attributes.code.CodeAttribute(table, name_index=None)[source]

Bases: jawa.attribute.Attribute

A CodeAttribute contains the executable bytecode of a single method.

As a quick example, lets make a “HelloWorld” class with a single method that simply returns when it’s called:

from jawa import ClassFile
from jawa.util.bytecode import Instruction

cf = ClassFile.create('HelloWorld')

main = cf.methods.create(
    # The name of the method
    'main',
    # The signature of the method
    '([Ljava/lang/String;)V',
    # Tell Jawa to automatically create an empty CodeAttribute for
    # us to use.
    code=True
)
main.code.max_locals = 1
main.access_flags.acc_static = True
main.code.assemble([
    Instruction.from_mnemonic('return')
])

# Save it to disk so we can run it with the JVM.
with open('HelloWorld.class', 'wb') as fout:
    cf.save(fout)
ADDED_IN = '1.0.2'
MINIMUM_CLASS_VERSION = (45, 3)
assemble(code)[source]

Assembles an iterable of Instruction objects into a method’s code body.

disassemble(*, transforms=None) → Iterator[jawa.util.bytecode.Instruction][source]

Disassembles this method, yielding an iterable of Instruction objects.

pack()[source]

The CodeAttribute in packed byte string form.

unpack(info)[source]

Read the CodeAttribute from the byte string info.

Note

Advanced usage only. You will typically never need to call this method as it will be called for you when loading a ClassFile.

Parameters:info – A byte string containing an unparsed CodeAttribute.
class jawa.attributes.code.CodeException(start_pc, end_pc, handler_pc, catch_type)

Bases: tuple

catch_type

Alias for field number 3

end_pc

Alias for field number 1

handler_pc

Alias for field number 2

start_pc

Alias for field number 0