Class: Android::Dex::ClassInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/android/dex/info.rb

Overview

class information in dex

Constant Summary collapse

NO_INDEX =

no index flag

0xffffffff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_def, dex) ⇒ ClassInfo

Returns a new instance of ClassInfo.

Parameters:

  • class_def (Dex::ClassDefItem)
  • dex (Dex)

    dex class instance



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/android/dex/info.rb', line 60

def initialize(class_def, dex)
  @class_def = class_def
  @dex = dex
  @access_flags = ClassAccessFlag.new(@class_def[:access_flags])
  @class_data = @class_def.class_data_item
  @static_fields = @instance_fields = @direct_methods = @virtual_methods = []
  unless @class_data.nil?
    @static_fields = cls2info(@class_data[:static_fields], FieldInfo, :field_idx_diff)
    @instance_fields = cls2info(@class_data[:instance_fields], FieldInfo, :field_idx_diff)
    @direct_methods = cls2info(@class_data[:direct_methods], MethodInfo, :method_idx_diff)
    @virtual_methods = cls2info(@class_data[:virtual_methods], MethodInfo, :method_idx_diff)
  end
end

Instance Attribute Details

#access_flagsClassAccessFlag (readonly)

Returns:



17
18
19
# File 'lib/android/dex/info.rb', line 17

def access_flags
  @access_flags
end

#class_dataDexObject::ClassDataItem (readonly)



28
29
30
# File 'lib/android/dex/info.rb', line 28

def class_data
  @class_data
end

#class_defDexObject::ClassDefItem (readonly)



30
31
32
# File 'lib/android/dex/info.rb', line 30

def class_def
  @class_def
end

#direct_methodsArray<MethodInfo> (readonly)

Returns direct methods.

Returns:



23
24
25
# File 'lib/android/dex/info.rb', line 23

def direct_methods
  @direct_methods
end

#instance_fieldsArray<FieldInfo> (readonly)

Returns instance fields.

Returns:



21
22
23
# File 'lib/android/dex/info.rb', line 21

def instance_fields
  @instance_fields
end

#nameString (readonly)

return full namespace of class

sample value like ‘Landroidx/activity/Cancellable;`

Returns:

  • (String)

    class name



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/android/dex/info.rb', line 12

class ClassInfo
  # no index flag
  NO_INDEX = 0xffffffff

  # @return [ClassAccessFlag]
  attr_reader :access_flags
  # @return [Array<FieldInfo>] static fields
  attr_reader :static_fields
  # @return [Array<FieldInfo>] instance fields
  attr_reader :instance_fields
  # @return [Array<MethodInfo>] direct methods
  attr_reader :direct_methods
  # @return [Array<MethodInfo>] virtual methods
  attr_reader :virtual_methods

  # @return [DexObject::ClassDataItem]
  attr_reader :class_data
  # @return [DexObject::ClassDefItem]
  attr_reader :class_def

  # return full namespace of class
  #
  # sample value like `Landroidx/activity/Cancellable;`
  #
  # @return [String] class name
  def name
    @name ||= @dex.type_resolve(@class_def[:class_idx])
  end

  def super_class
    @super_class ||= if @class_def[:superclass_idx] != NO_INDEX
        @dex.type_resolve(@class_def[:superclass_idx])
      else
        nil
      end
  end

  # Convert name to human readable string
  #
  # From `Landroidx/activity/Cancellable;` to `androidx.activity.Cancellable`
  #
  # @return [String] human readable name
  def type
    @type ||= name.gsub('/', '.')[1..-2]
  end

  # @param [Dex::ClassDefItem] class_def
  # @param [Dex] dex dex class instance
  def initialize(class_def, dex)
    @class_def = class_def
    @dex = dex
    @access_flags = ClassAccessFlag.new(@class_def[:access_flags])
    @class_data = @class_def.class_data_item
    @static_fields = @instance_fields = @direct_methods = @virtual_methods = []
    unless @class_data.nil?
      @static_fields = cls2info(@class_data[:static_fields], FieldInfo, :field_idx_diff)
      @instance_fields = cls2info(@class_data[:instance_fields], FieldInfo, :field_idx_diff)
      @direct_methods = cls2info(@class_data[:direct_methods], MethodInfo, :method_idx_diff)
      @virtual_methods = cls2info(@class_data[:virtual_methods], MethodInfo, :method_idx_diff)
    end
  end

  # @return [String] class difinition
  def definition
    ret = "#{access_flags} class #{name}"
    super_class.nil? ? ret : ret + " extends #{super_class}"
  end

  private

  def cls2info(arr, cls, idx_key)
    idx = 0
    ret = []
    arr.each do |item|
      idx += item[idx_key]
      ret << cls.new(item, idx, @dex)
    end
    ret
  end
end

#static_fieldsArray<FieldInfo> (readonly)

Returns static fields.

Returns:



19
20
21
# File 'lib/android/dex/info.rb', line 19

def static_fields
  @static_fields
end

#super_classString (readonly)

Returns super class name.

Returns:

  • (String)

    super class name



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/android/dex/info.rb', line 12

class ClassInfo
  # no index flag
  NO_INDEX = 0xffffffff

  # @return [ClassAccessFlag]
  attr_reader :access_flags
  # @return [Array<FieldInfo>] static fields
  attr_reader :static_fields
  # @return [Array<FieldInfo>] instance fields
  attr_reader :instance_fields
  # @return [Array<MethodInfo>] direct methods
  attr_reader :direct_methods
  # @return [Array<MethodInfo>] virtual methods
  attr_reader :virtual_methods

  # @return [DexObject::ClassDataItem]
  attr_reader :class_data
  # @return [DexObject::ClassDefItem]
  attr_reader :class_def

  # return full namespace of class
  #
  # sample value like `Landroidx/activity/Cancellable;`
  #
  # @return [String] class name
  def name
    @name ||= @dex.type_resolve(@class_def[:class_idx])
  end

  def super_class
    @super_class ||= if @class_def[:superclass_idx] != NO_INDEX
        @dex.type_resolve(@class_def[:superclass_idx])
      else
        nil
      end
  end

  # Convert name to human readable string
  #
  # From `Landroidx/activity/Cancellable;` to `androidx.activity.Cancellable`
  #
  # @return [String] human readable name
  def type
    @type ||= name.gsub('/', '.')[1..-2]
  end

  # @param [Dex::ClassDefItem] class_def
  # @param [Dex] dex dex class instance
  def initialize(class_def, dex)
    @class_def = class_def
    @dex = dex
    @access_flags = ClassAccessFlag.new(@class_def[:access_flags])
    @class_data = @class_def.class_data_item
    @static_fields = @instance_fields = @direct_methods = @virtual_methods = []
    unless @class_data.nil?
      @static_fields = cls2info(@class_data[:static_fields], FieldInfo, :field_idx_diff)
      @instance_fields = cls2info(@class_data[:instance_fields], FieldInfo, :field_idx_diff)
      @direct_methods = cls2info(@class_data[:direct_methods], MethodInfo, :method_idx_diff)
      @virtual_methods = cls2info(@class_data[:virtual_methods], MethodInfo, :method_idx_diff)
    end
  end

  # @return [String] class difinition
  def definition
    ret = "#{access_flags} class #{name}"
    super_class.nil? ? ret : ret + " extends #{super_class}"
  end

  private

  def cls2info(arr, cls, idx_key)
    idx = 0
    ret = []
    arr.each do |item|
      idx += item[idx_key]
      ret << cls.new(item, idx, @dex)
    end
    ret
  end
end

#virtual_methodsArray<MethodInfo> (readonly)

Returns virtual methods.

Returns:



25
26
27
# File 'lib/android/dex/info.rb', line 25

def virtual_methods
  @virtual_methods
end

Instance Method Details

#definitionString

Returns class difinition.

Returns:

  • (String)

    class difinition



75
76
77
78
# File 'lib/android/dex/info.rb', line 75

def definition
  ret = "#{access_flags} class #{name}"
  super_class.nil? ? ret : ret + " extends #{super_class}"
end

#typeString

Convert name to human readable string

From ‘Landroidx/activity/Cancellable;` to `androidx.activity.Cancellable`

Returns:

  • (String)

    human readable name



54
55
56
# File 'lib/android/dex/info.rb', line 54

def type
  @type ||= name.gsub('/', '.')[1..-2]
end