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.

bootstrap_methods

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.

constants

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.

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

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().
super_

The ConstantClass which represents this class’s superclass.

this

The ConstantClass which represents this class.

version

The ClassVersion for this class.

Example:

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

Bases: jawa.cf.ClassVersion

ClassFile file format version.

human

A human-readable string identifying this version.

If the version is unknown, None is returned instead.