3. Java Class Files#

Although, the previously introduced ABI implementations to Objective-C and Swift inspect executable files, this “ABI-like” module operates directly on Java class files. The general structure of the Java class file won’t be discussed here and is fairly well documented on Oracle’s documentation platform [1] .

Example usage of the provided API#
 1from umbrella.java import JavaClassFile
 2
 3with open("/path/to/file.class", "rb") as fp:
 4    file = JavaClassFile(fp)
 5
 6    # get the plain class name
 7    simple_name = file.get_simple_name()
 8
 9    # iterator over all defined methods
10    for method in file.jclass.methods:
11        # retrieve the method's name
12        name = file.get_constant_value(method.name_index)
13
14    # iterate over all defined fields
15    for field in file.jclass.fields:
16        name = file.get_constant_value(field.name_index)
17        access_flags: List[str] = field.access_flags.flags
18
19    # list all class attributes
20    for attribute in file.jclass.attributes:
21        tag = attribute.name
22        # based on the tag, each attribute should be handled differently
23        ...

3.1. Structs#

Constant Pool

An overview of all used structs that occur in the constant pool.

Access Flags

A list of all defined access flags for different types of constants or attributes.

Attribute Structs

All defined attribute structs (except StackFrameMap).

class umbrella.java.ClassFile[source]#

Main structure to store all class-related data

magic: bytes = b'\xca\xfe\xba\xbe'#

The magic item supplies the magic number identifying the class file format; it has the value 0xCAFEBABE.

minor_version: uint16_t#

The values of the minor_version and major_version items represent the minor and major version numbers of this class file. These numbers together determine the class file format version. When a class file has a major version number of M and a minor version number of m, the format version of its class file is denoted as M.m. Consequently, class file format versions can be ordered lexicographically, for example, 1.5 < 2.0 < 2.1.

major_version: uint16_t#

The major version, as described above.

constant_pool: ConstantPool#

The constant pool for this class file.

access_flags: AccessFlags#

The value of the access_flags item is a mask of flags used to denote access permissions to and properties of this class or interface

this_class: uint16_t#

The value of the this_class item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class or interface defined by this class file.

super_class: uint16_t#

For a class, the value of the super_class item either must be zero or must be a valid index into the constant_pool table. If the value of the super_class item is nonzero, the constant_pool entry at that index must be a CONSTANT_Class_info structure representing the direct superclass of the class defined by this class file.

num_interfaces: uint16_t#

The value of the interfaces_count item gives the number of direct superinterfaces of this class or interface type.

interfaces: t.List[uint16_t]#

Each value in the interfaces array must be a valid index into the constant_pool table.

num_fields: uint16_t#

The value of the fields_count item gives the number of field_info structures in the fields table. The field_info structures represent all fields, both class variables and instance variables, declared by this class or interface type.

fields: t.List[attribute.FieldInfo]#

Each value in the fields table must be a field_info structure giving a complete description of a field in this class or interface

num_methods: uint16_t#

The value of the methods_count item gives the number of method_info structures in the methods table.

methods: t.List[attribute.MethodInfo]#

Each value in the methods table must be a method_info structure giving a complete description of a method in this class or interface.

num_attributes: uint16_t#

The value of the attributes_count item gives the number of attributes in the attributes table of this class.

attributes: t.List[attribute.AttributeInfo]#

Each value of the attributes table must be an AttributeInfo structure.

get_name() str[source]#
get_super_class_name() str | None[source]#
get_constant(index: int)[source]#
get_interface_names() List[str][source]#
class umbrella.java.JavaClassFile(_JavaClassFile__stream: str)[source]#
property jclass: ClassFile#

The underlying java class file

get_simple_name() str[source]#

Returns only the class name.

Returns:

the simple name of the underlying class

Return type:

str

get_package_name() str[source]#

Generates the package name for the underlying class.

Returns:

the package name (e.g. "com.example.foo")

Return type:

str

get_source_name() str | None[source]#

Returns the source code file name (if present)

Returns:

the source code file name

Return type:

t.Optional[str]

get_attribute(kind: str) AttributeInfo | None[source]#

Tries to retrieve the first attribute with the given kind.

Parameters:

kind (str) – the attribute info tag (string)

Returns:

the first occurrence of the given attribute kind

Return type:

t.Optional[AttributeInfo]

get_constant_value(index: int) str | int | float | Any[source]#

Returns the value associated to a constant entry.

Parameters:

index (int) – the constant pool relative index (starts with 1)

Returns:

the value or info object of no “value” field is defined

Return type:

t.Union[str, int, float, t.Any]

Footnotes