4. Smali Visitor API

class smali.visitor.VisitorBase(delegate: VisitorBase = None)

Base class for Smali-Class visitor classes.

Parameters:

delegate (BaseVisitor subclass, optional) – A delegate visitor, defaults to None

visit_comment(text: str) None

Visits a comment string.

Important: if you want to visit inline comments (EOL comments) use #visit_eol_comment() instead.

Parameters:

text (str) – the comment’s text without the leading ‘#’

visit_end() None

Called at then end of an annotation.

visit_eol_comment(text: str) None

Visits an inlined comment (EOL comment)

Parameters:

text (str) – the text without the leading ‘#’

class smali.visitor.ClassVisitor(delegate: VisitorBase = None)

Base class for Smali class visitors.

visit_annotation(access_flags: int, signature: str) AnnotationVisitor

Prepares to visit an annotation.

Parameters:
  • access_flags (int) – the annotations access flags (zero on most cases)

  • signature (str) – the class signature

visit_class(name: str, access_flags: int) None

Called when the class definition has been parsed.

Parameters:
  • name (str) – the class name (type descriptor, e.g. “Lcom/example/A;”)

  • access_flags (int) – different access flags (PUBLIC, FINAL, …)

visit_debug(enabled: int) None

Visits a .debug directive.

Parameters:

enabled (int) – whether debugging symbols are enabled.

visit_field(name: str, access_flags: int, field_type: str, value=None) FieldVisitor

Called when a global field definition has been parsed.

Parameters:
  • name (str) – the field’s name

  • access_flags (str) – the access flags like PUBLIC, FINAL, …

  • field_type (str) – the field’s type (can be primitive)

  • value (_type_) – the field’s value

visit_implements(interface: str) None

Colled upon an implements directive.

Parameters:

interface (str) – the class name (internal name)

visit_inner_class(name: str, access_flags: int) ClassVisitor

Called when the class definition has been parsed.

Parameters:
  • name (str) – the class name (type descriptor, e.g. “Lcom/example/A;”)

  • access_flags (int) – different access flags (PUBLIC, FINAL, …)

visit_method(name: str, access_flags: int, parameters: list, return_type: str) MethodVisitor

Called when a method definition has been parsed.

Parameters:
  • name (str) – the method’s name

  • access_flags (int) – the access flags (PUBLIC, PRIVATE, …)

  • parameters (list) – the parameter list (internal names)

  • return_type (str) – the return type (internal name)

Returns:

a MethodVisitor that handles method parsing events

Return type:

MethodVisitor

visit_source(source: str) None

Visits the source type of the smali file.

Parameters:

source (str) – the source type

visit_super(super_class: str) None

Called when a .super statement has been parsed.

Parameters:

super_class (str) – the super class name as type descriptor

class smali.visitor.AnnotationVisitor(delegate: VisitorBase = None)

Base class for annotation visitors.

visit_array(name: str, values: list) None

Visits an array of values.

Parameters:
  • name (str) – the value name

  • values (list) – the array’s values

visit_enum(name: str, owner: str, const: str, value_type: str) None

Visits an enum value

Parameters:
  • owner (str) – the declaring class

  • name (str) – the annotation value name

  • const (str) – the enum constant name

  • value_type (str) – the value type

visit_subannotation(name: str, access_flags: int, signature: str) AnnotationVisitor

Prepares to visit an internal annotation.

Parameters:
  • name (str) – the annotation value name

  • access_flags (int) – the annotations access flags (zero on most cases)

  • signature (str) – the class signature

visit_value(name: str, value) None

Visits a simple annotation value.

Parameters:
  • name (str) – the value’s name

  • value (_type_) – the value

class smali.visitor.FieldVisitor(delegate: VisitorBase = None)

Base class for field visitors.

visit_annotation(access_flags: int, signature: str) AnnotationVisitor

Prepares to visit an annotation.

Parameters:
  • access_flags (int) – the annotations access flags (zero on most cases)

  • signature (str) – the class signature

class smali.visitor.MethodVisitor(delegate: MethodVisitor = None)

Base class for method visitors.

visit_annotation(access_flags: int, signature: str) AnnotationVisitor

Prepares to visit an annotation.

Parameters:
  • access_flags (int) – the annotations access flags (zero on most cases)

  • signature (str) – the class signature

visit_array_data(length: str, value_list: list) None

Called on an ‘.array-data’ statement.

Parameters:
  • length (str) – the array’s length

  • value_list (list) – the array’s values

visit_block(name: str) None

Called when a goto-block definition is parsed.

Parameters:

name (str) – the block’s name

visit_catch(exc_name: str, blocks: tuple) None

Called on a .catch statement.

The blocks contain the two enclosing goto blocks and the returning definition:

.catch <name> { <try_start> .. <try_end> } <catch_handler>
Parameters:
  • exc_name (str) – the exception descriptor

  • blocks (tuple) – the goto-blocks definition

visit_catchall(exc_name: str, blocks: tuple) None

Called on a .catchall statement.

The blocks contain the two enclosing goto blocks and the returning definition:

.catchall <name> { <try_start> .. <try_end> } <catch_handler>
Parameters:
  • exc_name (str) – the exception descriptor

  • blocks (tuple) – the goto-blocks definition

visit_goto(block_name: str) None

Visits ‘goto’ statements.

Parameters:

block_name (str) – the destination block name

visit_instruction(ins_name: str, args: list) None

Visits common instructions with one or two parameters.

Parameters:
  • ins_name (str) – the instruction name

  • args (list) – the argument list

visit_invoke(inv_type: str, args: list, owner: str, method: str) None

Handles an ‘invoke-’ statement.

This method is called whenever an ‘invoke-’ statement hias been parsed. That includes ‘invoke-virtual’ as well as ‘invoke-direct’.

The provided metho string contains the method signature which can be passed into the Type constructor.

Parameters:
  • inv_type (str) – the invocation type (direct, virtual, …)

  • args (list) – the argument list

  • owner (str) – the owner class of the referenced method

  • method (str) – the method to call

visit_line(number: int) None

Called when a line definition is parsed.

Parameters:

name (str) – the line number

visit_local(register: str, name: str, descriptor: str, full_descriptor: str) None

Handles debug information packed into .local statements.

Parameters:
  • register (str) – the variable register

  • name (str) – the variable name

  • descriptor (str) – the type descriptor

  • full_descriptor (str) – the java descriptor

visit_locals(local_count: int) None

Called on a .locals statement.

The execution context of this method should be the same as of visit_registers.

Parameters:

locals (int) – the amount of local variables

visit_packed_switch(value: str, blocks: list) None

Handles the packed-switch statement.

Parameters:
  • value (str) – the value which will be “switched”

  • blocks (list[str]) – the block ids

visit_param(register: str, name: str) None

Called on a .param statement

Parameters:
  • register (str) – the register

  • name (str) – the parameter’s name

visit_prologue() None

Visits a .prologue statement.

Note that this call comes without any arguments.

visit_registers(registers: int) None

Called on a ‘.registers’ statement.

The execution context of this method should be the same as of ‘visit_locals’.

Parameters:

registers (int) – the amount of local variables

visit_restart(register: str) None

Visits a .restart statement.

Parameters:

register (str) – the register

visit_return(ret_type: str, args: list) None

Handles ‘return-’ statements.

Parameters:
  • ret_type (str) – the return type, e.g. “object” or “void”, …

  • args (list) – the argument list

visit_sparse_switch(branches: dict) None

Visits a .sparse-switch statement.

The branches takes the original case value as their key and the block_id as their value.

Parameters:

branches (dict) – the switch branches