Implemented Attributes

Standard attributes are defined in Section 4.7 - Attributes. In addition to these attributes, various compilers, debuggers, obfuscators, and other tools may insert additional unknown attributes into a ClassFile.

Not all standard attributes are currently implemented. When an unknown attribute is encountered, an UnknownAttribute object is created instead.

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)

Note

Not all Method objects will have an associated CodeAttribute - methods that are flagged as acc_native or acc_abstract will never have one.

assemble(code)[source]

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

attributes

An AttributeTable containing all of the attributes associated with this CodeAttribute.

code
disassemble()[source]

Disassembles this method, yielding an iterable of Instruction objects.

exception_table
info

The CodeAttribute in packed byte string form.

max_locals

The maximum number of locals.

max_stack

The maximum size of the stack.

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

jawa.attributes.constant_value module

class jawa.attributes.constant_value.ConstantValueAttribute(table, value=None, name_index=None)[source]

Bases: jawa.attribute.Attribute

constantvalue
info
unpack(info)[source]

jawa.attributes.source_file module

class jawa.attributes.source_file.SourceFileAttribute(table, sourcefile=None, name_index=None)[source]

Bases: jawa.attribute.Attribute

info
sourcefile
unpack(info)[source]