Pro.Class — API for parsing Java Class files

Overview

The Pro.Class module contains the API for parsing Java Class files.

Disassembling

The following code example demonstrates how to disassemble a Java Class.

from Pro.Core import *
from Pro.Class import *

def disassembleJavaClass(fname):
    c = createContainerFromFile(fname)
    if c.isNull():
        return
    obj = ClassObject()
    if not obj.Load(c) or not obj.ProcessClass():
        return
    out = NTTextBuffer()
    obj.Disassemble(out)
    print(out.buffer)

Method Enumeration

The following code example shows how to enumerate the methods in a Java Class.

from Pro.Core import *
from Pro.Class import *

def enumerateJavaClassMethods(fname):
    c = createContainerFromFile(fname)
    if c.isNull():
        return
    obj = ClassObject()
    if not obj.Load(c) or not obj.ProcessClass():
        return
    methods = obj.Methods()
    it = methods.iterator()
    while it.hasNext():
        method_offs = it.next()
        attrs, fd = obj.FieldAttributes(method_offs)
        name = obj.IndexToString(fd.name_index)
        print("offset:", hex(method_offs), "- name:", name)
        codeattr_offs = obj.FindAttribute(attrs, "Code")
        cad = CodeAttributeData()
        if codeattr_offs != 0 and obj.ParseCodeAttribute(codeattr_offs, cad):
            print("    code offset:", hex(cad.code_offset), "- code size:", hex(cad.code_length))

An example output of the code:

offset: 0x2f1 - name: <init>
    code offset: 0x307 - code size: 0x5
offset: 0x31c - name: main
    code offset: 0x332 - code size: 0x44
offset: 0x3c6 - name: <clinit>
    code offset: 0x3dc - code size: 0xb

Module API

Pro.Class module API.

Attributes:

CACC_ABSTRACT

Class access flag.

CACC_ANNOTATION

Class access flag.

CACC_ENUM

Class access flag.

CACC_FINAL

Class access flag.

CACC_INTERFACE

Class access flag.

CACC_PUBLIC

Class access flag.

CACC_SUPER

Class access flag.

CACC_SYNTHETIC

Class access flag.

CONSTANT_Class

Class constant tag.

CONSTANT_Double

Double constant tag.

CONSTANT_Fieldref

Field reference constant tag.

CONSTANT_Float

Float constant tag.

CONSTANT_Integer

Integer constant tag.

CONSTANT_InterfaceMethodref

Interface method reference constant tag.

CONSTANT_InvokeDynamic

Invoke dynamic constant tag.

CONSTANT_Long

Long constant tag.

CONSTANT_MethodHandle

Method handle constant tag.

CONSTANT_MethodType

Method type constant tag.

CONSTANT_Methodref

Method reference constant tag.

CONSTANT_NameAndType

Name and type constant tag.

CONSTANT_String

String constant tag.

CONSTANT_Utf8

UTF-8 constant tag.

FACC_ENUM

Field access flag.

FACC_FINAL

Field access flag.

FACC_PRIVATE

Field access flag.

FACC_PROTECTED

Field access flag.

FACC_PUBLIC

Field access flag.

FACC_STATIC

Field access flag.

FACC_SYNTHETIC

Field access flag.

FACC_TRANSIENT

Field access flag.

FACC_VOLATILE

Field access flag.

MACC_ABSTRACT

Method access flag.

MACC_BRIDGE

Method access flag.

MACC_FINAL

Method access flag.

MACC_NATIVE

Method access flag.

MACC_PRIVATE

Method access flag.

MACC_PROTECTED

Method access flag.

MACC_PUBLIC

Method access flag.

MACC_STATIC

Method access flag.

MACC_STRICT

Method access flag.

MACC_SYNCHRONIZED

Method access flag.

MACC_SYNTHETIC

Method access flag.

MACC_VARARGS

Method access flag.

NACC_ABSTRACT

Nested class access flag.

NACC_ANNOTATION

Nested class access flag.

NACC_ENUM

Nested class access flag.

NACC_FINAL

Nested class access flag.

NACC_INTERFACE

Nested class access flag.

NACC_PRIVATE

Nested class access flag.

NACC_PROTECTED

Nested class access flag.

NACC_PUBLIC

Nested class access flag.

NACC_STATIC

Nested class access flag.

NACC_SYNTHETIC

Nested class access flag.

Classes:

ClassObject()

This class represents a Java Class file.

CodeAttributeData()

This class contains code attribute data.

ExceptionList()

List of ExceptionTable elements.

ExceptionListIt(obj)

Iterator class for ExceptionList.

ExceptionTable()

This class represents an exception table.

FieldData()

This class represents a field data.

CACC_ABSTRACT: Final[int]

Class access flag.

Declared abstract; must not be instantiated.

CACC_ANNOTATION: Final[int]

Class access flag.

Declared as an annotation type.

CACC_ENUM: Final[int]

Class access flag.

Declared as an enum type.

CACC_FINAL: Final[int]

Class access flag.

Declared final; no subclasses allowed.

CACC_INTERFACE: Final[int]

Class access flag.

Is an interface, not a class.

CACC_PUBLIC: Final[int]

Class access flag.

Declared public; may be accessed from outside its package.

CACC_SUPER: Final[int]

Class access flag.

Treat superclass methods specially when invoked by the invokespecial instruction.

CACC_SYNTHETIC: Final[int]

Class access flag.

Declared synthetic; not present in the source code.

CONSTANT_Class: Final[int]

Class constant tag.

CONSTANT_Double: Final[int]

Double constant tag.

CONSTANT_Fieldref: Final[int]

Field reference constant tag.

CONSTANT_Float: Final[int]

Float constant tag.

CONSTANT_Integer: Final[int]

Integer constant tag.

CONSTANT_InterfaceMethodref: Final[int]

Interface method reference constant tag.

CONSTANT_InvokeDynamic: Final[int]

Invoke dynamic constant tag.

CONSTANT_Long: Final[int]

Long constant tag.

CONSTANT_MethodHandle: Final[int]

Method handle constant tag.

CONSTANT_MethodType: Final[int]

Method type constant tag.

CONSTANT_Methodref: Final[int]

Method reference constant tag.

CONSTANT_NameAndType: Final[int]

Name and type constant tag.

CONSTANT_String: Final[int]

String constant tag.

CONSTANT_Utf8: Final[int]

UTF-8 constant tag.

class ClassObject

Bases: Pro.Core.CFFObject

This class represents a Java Class file.

See also ProcessClass().

Methods:

Attributes()

Returns a list of attribute offsets.

ClassAccessFlagsDescription(flags[, code])

Retrieves the description for class access flags.

ClassIndexToString(idx[, beautify])

Converts a class index to its string representation.

Constant(offset)

Retrieves the structure of a constant from its offset.

ConstantIndexToString(idx[, beautify, opt])

Converts a constant index into its string representation.

ConstantTypeName(type)

Returns the constant type name.

Constants()

Returns the list of constant offsets.

Disassemble(out)

Disassembles the Java Class file.

DumpAttribute(out, offset[, dump_name])

Prints out an attribute.

DumpHeader(out)

Prints out the header of the Java Class file.

FieldAccessFlagsDescription(flags[, code])

Retrieves the description for field access flags.

FieldAttributes(offset)

Retrieves the attribute offset list and the field data.

Fields()

Returns the field offset list.

FindAttribute(attributes, name)

Finds an attribute by its name.

IndexToString(idx)

Converts an index into its string representation.

Interfaces()

Returns the list of interface name indexes.

MatchStringIndex(idx, str)

Matches a constant index to a string.

MethodAccessFlagsDescription(flags[, code])

Retrieves the description for method access flags.

Methods()

Returns the list of method offsets.

NameAndTypeIndexToString(idx[, beautify, …])

Converts a name and type index into a string.

NestedAccessFlagsDescription(flags[, code])

Retrieves the description for nested access flags.

ParseCodeAttribute(offset, cd)

Parses a code attribute.

ProcessClass()

Parses the Java Class file.

Attributes:

IDXSO_FieldType

Index string option: puts the return value in a comment.

IDXSO_ReturnComment

Index string option: avoids the ‘return’ string in the comment.

Attributes()Pro.Core.NTUIntVector
Returns

Returns a list of attribute offsets.

Return type

NTUIntVector

static ClassAccessFlagsDescription(flags: int, code: bool = False)str

Retrieves the description for class access flags.

Parameters
  • flags (int) – The class access flags.

  • code (bool) – If True, ignores unknown flags.

Returns

Returns the description.

Return type

str

ClassIndexToString(idx: int, beautify: bool = False)str

Converts a class index to its string representation.

Parameters
  • idx (int) – The class index.

  • beautify (bool) – If True, beautifies the returned string.

Returns

Returns the string if successful; otherwise returns an empty string.

Return type

str

Constant(offset: int)Pro.Core.CFFStruct

Retrieves the structure of a constant from its offset.

Parameters

offset (int) – The offset of the structure.

Returns

Returns the structure if successful; otherwise returns an invalid structure.

Return type

CFFStruct

ConstantIndexToString(idx: int, beautify: bool = False, opt: int = 0)str

Converts a constant index into its string representation.

Parameters
  • idx (int) – The constant index.

  • beautify (bool) – If True, beautifies the returned string.

  • opt (int) – The string index options (e.g., IDXSO_FieldType).

Returns

Returns the string if successful; otherwise returns an empty string.

Return type

str

ConstantTypeName(type: int)str

Returns the constant type name.

Parameters

type (int) – The constant type.

Returns

Returns the type name if successful; otherwise returns "?".

Return type

str

Constants()Pro.Core.NTUIntVector
Returns

Returns the list of constant offsets.

Return type

NTUIntVector

Disassemble(out: Pro.Core.NTTextStream)None

Disassembles the Java Class file.

Parameters

out (NTTextStream) – The output stream.

See also Pro.Core.NTTextStream.

DumpAttribute(out: Pro.Core.NTTextStream, offset: int, dump_name: bool = False)None

Prints out an attribute.

Parameters
  • out (NTTextStream) – The output stream.

  • offset (int) – The offset of the attribute.

  • dump_name (bool) – If True, prints out the name of the attribute.

See also Pro.Core.NTTextStream.

DumpHeader(out: Pro.Core.NTTextStream)None

Prints out the header of the Java Class file.

Parameters

out (NTTextStream) – The output stream.

See also Pro.Core.NTTextStream.

static FieldAccessFlagsDescription(flags: int, code: bool = False)str

Retrieves the description for field access flags.

Parameters
  • flags (int) – The field access flags.

  • code (bool) – If True, ignores unknown flags.

Returns

Returns the description.

Return type

str

FieldAttributes(offset: int)tuple

Retrieves the attribute offset list and the field data.

Parameters

offset (int) – The offset of the field.

Returns

Returns the attribute offset list and the field data.

Return type

tuple[NTUIntVector, FieldData]

Fields()Pro.Core.NTUIntVector
Returns

Returns the field offset list.

Return type

NTUIntVector

FindAttribute(attributes: Pro.Core.NTUIntVector, name: str)int

Finds an attribute by its name.

Parameters
  • attributes (NTUIntVector) – The list of attribute offsets.

  • name (str) – The name of the attribute to find.

Returns

Returns the offset of the attribute if present; otherwise returns 0.

Return type

int

IDXSO_FieldType: Final[int]

Index string option: puts the return value in a comment.

IDXSO_ReturnComment: Final[int]

Index string option: avoids the ‘return’ string in the comment.

IndexToString(idx: int)str

Converts an index into its string representation.

Parameters

idx (int) – The index.

Returns

Returns the string if successful; otherwise returns an empty string.

Return type

str

Interfaces()Pro.Core.NTUShortVector
Returns

Returns the list of interface name indexes.

Return type

NTUShortVector

MatchStringIndex(idx: int, str: str)bool

Matches a constant index to a string.

Parameters
  • idx (int) – The constant index.

  • str (str) – The string.

Returns

Returns True if the constant index matches the input string; otherwise returns False.

Return type

bool

static MethodAccessFlagsDescription(flags: int, code: bool = False)str

Retrieves the description for method access flags.

Parameters
  • flags (int) – The method access flags.

  • code (bool) – If True, ignores unknown flags.

Returns

Returns the description.

Return type

str

Methods()Pro.Core.NTUIntVector
Returns

Returns the list of method offsets.

Return type

NTUIntVector

NameAndTypeIndexToString(idx: int, beautify: bool = False, classidx: int = 0, opt: int = 0)str

Converts a name and type index into a string.

Parameters
  • idx (int) – The name index.

  • beautify (bool) – If True, beautifies the returned string.

  • classidx (int) – The optional class index.

  • opt (int) – The string index options (e.g., IDXSO_FieldType).

Returns

Returns the string if successful; otherwise returns an empty string.

Return type

str

static NestedAccessFlagsDescription(flags: int, code: bool = False)str

Retrieves the description for nested access flags.

Parameters
  • flags (int) – The nested access flags.

  • code (bool) – If True, ignores unknown flags.

Returns

Returns the description.

Return type

str

ParseCodeAttribute(offset: int, cd: Pro.Class.CodeAttributeData)bool

Parses a code attribute.

Parameters
  • offset (int) – The offset of the code attribute.

  • cd (CodeAttributeData) – The parsed data.

Returns

Returns True if successful; otherwise returns False.

Return type

bool

See also CodeAttributeData.

ProcessClass()bool

Parses the Java Class file.

Returns

Returns True if successful; otherwise returns False.

Return type

bool

class CodeAttributeData

This class contains code attribute data.

Attributes:

attributes

Pro.Core.NTUIntVector list of attribute offsets.

code_length

The length of the code.

code_offset

The offset of the code.

exceptions

Pro.Core.NTUIntVector list of exception offsets.

max_locals

The maximum number of local variables used by the method.

max_stack

The maximum stack size used by the method.

attributes

Pro.Core.NTUIntVector list of attribute offsets.

code_length

The length of the code.

code_offset

The offset of the code.

exceptions

Pro.Core.NTUIntVector list of exception offsets.

max_locals

The maximum number of local variables used by the method.

max_stack

The maximum stack size used by the method.

class ExceptionList

List of ExceptionTable elements.

Methods:

append(value)

Inserts value at the end of the vector.

at(i)

Returns the item at index position i in the vector.

clear()

Removes all the elements from the vector and releases the memory used by the vector.

contains(value)

Checks the presence of an element in the vector.

count(value)

Returns the number of occurrences of value in the vector.

indexOf(value[, start])

Searches for an element in the vector.

insert(i, value)

Inserts value at index position i in the vector.

isEmpty()

Checks whether the vector is empty.

iterator()

Creates an iterator for the vector.

reserve(alloc)

Reserve space for alloc elements.

resize(size)

Sets the size of the vector to size.

size()

Returns the number of items in the vector.

append(value: Pro.Class.ExceptionTable)None

Inserts value at the end of the vector.

Parameters

value (ExceptionTable) – The value to add to the list.

See also insert().

at(i: int)Pro.Class.ExceptionTable

Returns the item at index position i in the vector. i must be a valid index position in the vector (i.e., 0 <= i < size()).

Parameters

i (int) – The index of the element to return.

Returns

Returns the requested element.

Return type

ExceptionTable

clear()None

Removes all the elements from the vector and releases the memory used by the vector.

contains(value: Pro.Class.ExceptionTable)bool

Checks the presence of an element in the vector.

Parameters

value (ExceptionTable) – The value to check for.

Returns

Returns True if the vector contains an occurrence of value; otherwise returns False.

Return type

bool

See also indexOf() and count().

count(value: Pro.Class.ExceptionTable)int

Returns the number of occurrences of value in the vector.

Parameters

value (ExceptionTable) – The value to count.

Returns

Returns the number of occurrences.

Return type

int

See also indexOf() and contains().

indexOf(value: Pro.Class.ExceptionTable, start: int = 0)int

Searches for an element in the vector.

Parameters
  • value (ExceptionTable) – The value to search for.

  • start (int) – The start index.

Returns

Returns the index position of the first occurrence of value in the vector. Returns -1 if no item was found.

Return type

int

See also contains().

insert(i: int, value: Pro.Class.ExceptionTable)None

Inserts value at index position i in the vector. If i is 0, the value is prepended to the vector. If i is size(), the value is appended to the vector.

Parameters
  • i (int) – The position at which to add the value.

  • value (ExceptionTable) – The value to add.

See also append().

isEmpty()bool

Checks whether the vector is empty.

Returns

Returns True if the vector contains no items; otherwise returns False.

Return type

bool

See also size().

iterator()Pro.Class.ExceptionListIt

Creates an iterator for the vector.

Returns

Returns the iterator.

Return type

ExceptionListIt

reserve(alloc: int)None

Reserve space for alloc elements. Calling this method doesn’t change the size of the vector.

Parameters

alloc (int) – The amount of elements to reserve space for.

resize(size: int)None

Sets the size of the vector to size. If size is greater than the current size, elements are added to the end; the new elements are initialized with a default-constructed value. If size is less than the current size, elements are removed from the end.

Parameters

size (int) – The new size of the vector.

See also size().

size()int
Returns

Returns the number of items in the vector.

Return type

int

See also isEmpty().

class ExceptionListIt(obj: Pro.Class.ExceptionList)

Iterator class for ExceptionList.

Parameters

obj (ExceptionList) – The object to iterate over.

Methods:

hasNext()

Returns True if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the container; otherwise returns False.

hasPrevious()

Returns True if there is at least one item behind the iterator, i.e. the iterator is not at the front of the container; otherwise returns False.

next()

Returns the next item and advances the iterator by one position.

previous()

Returns the previous item and moves the iterator back by one position.

toBack()

Moves the iterator to the back of the container (after the last item).

toFront()

Moves the iterator to the front of the container (before the first item).

hasNext()bool
Returns

Returns True if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the container; otherwise returns False.

Return type

bool

See also hasPrevious() and next().

hasPrevious()bool
Returns

Returns True if there is at least one item behind the iterator, i.e. the iterator is not at the front of the container; otherwise returns False.

Return type

bool

See also hasNext() and previous().

next()Pro.Class.ExceptionTable
Returns

Returns the next item and advances the iterator by one position.

Return type

ExceptionTable

See also hasNext() and previous().

previous()Pro.Class.ExceptionTable
Returns

Returns the previous item and moves the iterator back by one position.

Return type

ExceptionTable

See also hasPrevious() and next().

toBack()None

Moves the iterator to the back of the container (after the last item).

See also toFront() and previous().

toFront()None

Moves the iterator to the front of the container (before the first item).

See also toBack() and next().

class ExceptionTable

This class represents an exception table.

Attributes:

catch_type

The exception class type to catch.

end_pc

The end address of the try clause.

handler_pc

The address of the exception handler.

start_pc

The start address of the try clause.

catch_type

The exception class type to catch.

end_pc

The end address of the try clause.

handler_pc

The address of the exception handler.

start_pc

The start address of the try clause.

FACC_ENUM: Final[int]

Field access flag.

Declared as an element of an enum.

FACC_FINAL: Final[int]

Field access flag.

Declared final; never directly assigned to after object construction.

FACC_PRIVATE: Final[int]

Field access flag.

Declared private; usable only within the defining class.

FACC_PROTECTED: Final[int]

Field access flag.

Declared protected; may be accessed within subclasses.

FACC_PUBLIC: Final[int]

Field access flag.

Declared public; may be accessed from outside its package.

FACC_STATIC: Final[int]

Field access flag.

Declared static.

FACC_SYNTHETIC: Final[int]

Field access flag.

Declared synthetic; not present in the source code.

FACC_TRANSIENT: Final[int]

Field access flag.

Declared transient; not written or read by a persistent object manager.

FACC_VOLATILE: Final[int]

Field access flag.

Declared volatile; cannot be cached.

class FieldData

This class represents a field data.

See also ClassObject.FieldAttributes().

Attributes:

access_flags

The access flags.

descriptor_index

The descriptor index.

name_index

The name index.

access_flags

The access flags.

descriptor_index

The descriptor index.

name_index

The name index.

MACC_ABSTRACT: Final[int]

Method access flag.

Declared abstract; no implementation is provided.

MACC_BRIDGE: Final[int]

Method access flag.

A bridge method, generated by the compiler.

MACC_FINAL: Final[int]

Method access flag.

Declared final; no subclasses allowed.

MACC_NATIVE: Final[int]

Method access flag.

Declared native; implemented in a language other than Java.

MACC_PRIVATE: Final[int]

Method access flag.

Declared private; accessible only within the defining class.

MACC_PROTECTED: Final[int]

Method access flag.

Declared protected; may be accessed within subclasses.

MACC_PUBLIC: Final[int]

Method access flag.

Declared public; may be accessed from outside its package.

MACC_STATIC: Final[int]

Method access flag.

Declared static.

MACC_STRICT: Final[int]

Method access flag.

Declared strictfp; floating-point mode is FP-strict.

MACC_SYNCHRONIZED: Final[int]

Method access flag.

Declared synchronized; invocation is wrapped by a monitor use.

MACC_SYNTHETIC: Final[int]

Method access flag.

Declared synthetic; not present in the source code.

MACC_VARARGS: Final[int]

Method access flag.

Declared with variable number of arguments.

NACC_ABSTRACT: Final[int]

Nested class access flag.

Marked or implicitly abstract in source.

NACC_ANNOTATION: Final[int]

Nested class access flag.

Declared as an annotation type.

NACC_ENUM: Final[int]

Nested class access flag.

Declared as an enum type.

NACC_FINAL: Final[int]

Nested class access flag.

Marked final in source.

NACC_INTERFACE: Final[int]

Nested class access flag.

Was an interface in source.

NACC_PRIVATE: Final[int]

Nested class access flag.

Marked private in source.

NACC_PROTECTED: Final[int]

Nested class access flag.

Marked protected in source.

NACC_PUBLIC: Final[int]

Nested class access flag.

Marked or implicitly public in source.

NACC_STATIC: Final[int]

Nested class access flag.

Marked or implicitly static in source.

NACC_SYNTHETIC: Final[int]

Nested class access flag.

Declared synthetic; not present in the source code.