jawa.cf module

ClassFile reader & writer.

The jawa.cf module provides tools for working with JVM .class ClassFiles.

class jawa.cf.ClassFile(source: IO = None)[source]

Bases: object

Implements the JVM ClassFile (files typically ending in .class).

To open an existing ClassFile:

>>> with open('HelloWorld.class', 'rb') as fin:
...    cf = ClassFile(fin)

To save a newly created or modified ClassFile:

>>> cf = ClassFile.create('HelloWorld')
>>> with open('HelloWorld.class', 'wb') as out:
...    cf.save(out)

create() sets up some reasonable defaults equivalent to:

public class HelloWorld extends java.lang.Object{
Parameters:source – any file-like object providing .read().
MAGIC = 3405691582

The JVM ClassFile magic number.


Returns the bootstrap methods table from the BootstrapMethods attribute, if one exists. If it does not, one will be created.

Returns:Table of BootstrapMethod objects.
classloader = None

The ClassLoader bound to this ClassFile, if any.


The ConstantPool for this class.

classmethod create(this: str, super_: str = 'java/lang/Object') → jawa.cf.ClassFile[source]

A utility which sets up reasonable defaults for a new public class.

  • this – The name of this class.
  • super – The name of this class’s superclass.

A list of direct superinterfaces of this class as indexes into the constant pool, in left-to-right order.

save(source: IO)[source]

Saves the class to the file-like object source.

Parameters:source – Any file-like object providing write().

The ConstantClass which represents this class’s superclass.


The ConstantClass which represents this class.


The ClassVersion for this class.


>>> cf = ClassFile.create('HelloWorld')
>>> cf.version = 51, 0
>>> print(cf.version)
ClassVersion(major=51, minor=0)
>>> print(cf.version.major)
class jawa.cf.ClassVersion[source]

Bases: jawa.cf.ClassVersion

ClassFile file format version.


A human-readable string identifying this version.

If the version is unknown, None is returned instead.