Class: JvmBytecode::Method

Inherits:
Object
  • Object
show all
Includes:
AccessFlag
Defined in:
lib/jvm_bytecode/method.rb

Constant Summary collapse

ACCESS_FLAGS =
{
  public: 0x0001,
  private: 0x0002,
  protected: 0x0004,
  static: 0x0008,
  final: 0x0010,
  syncrhonized: 0x0020,
  bridge: 0x0040,
  varargs: 0x0080,
  native: 0x0100,
  abstract: 0x0400,
  strict: 0x0800,
  synthetic: 0x1000
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AccessFlag

#access_flag, #all_access_flag, #readable_access_flag, #set_access_flag

Constructor Details

#initialize(cp) ⇒ Method

Returns a new instance of Method.



27
28
29
30
31
32
# File 'lib/jvm_bytecode/method.rb', line 27

def initialize(cp)
  @cp = cp
  @name = 0
  @descriptor = 0
  @attributes = []
end

Class Method Details

.decode_serial(cp, io) ⇒ Object



21
22
23
24
25
# File 'lib/jvm_bytecode/method.rb', line 21

def self.decode_serial(cp, io)
  Array.new(io.read(2).unpack('S>').first) do 
    new(cp).tap { |m| m.decode(io) }
  end
end

Instance Method Details

#bytecodeObject



49
50
51
# File 'lib/jvm_bytecode/method.rb', line 49

def bytecode
  [access_flag, @name, @descriptor].pack('S>*') + @attributes.join_bytecodes
end

#code(&block) ⇒ Object



42
43
44
45
46
47
# File 'lib/jvm_bytecode/method.rb', line 42

def code(&block)
  @attributes
    .push(Attributes::Code.new(@cp))
    .last
    .instance_eval(&block)
end

#decode(io) ⇒ Object



53
54
55
56
57
58
# File 'lib/jvm_bytecode/method.rb', line 53

def decode(io)
  acc_flag, @name, @descriptor = io.read(6).unpack('S>3')
  set_access_flag(acc_flag)

  @attributes = Attributes::Attribute.decode_serial(@cp, io)
end

#descriptor(d) ⇒ Object



38
39
40
# File 'lib/jvm_bytecode/method.rb', line 38

def descriptor(d)
  @descriptor = @cp.index_or_utf8(d)
end

#name(n) ⇒ Object



34
35
36
# File 'lib/jvm_bytecode/method.rb', line 34

def name(n)
  @name = @cp.index_or_utf8(n)
end

#to_hashObject



60
61
62
63
64
65
66
67
# File 'lib/jvm_bytecode/method.rb', line 60

def to_hash
  {
    name_index: @name,
    descriptor_index: @descriptor,
    access_flag: access_flag,
    attributes: @attributes.map(&:to_hash)
  }
end