Class: JvmBytecode::Attributes::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/jvm_bytecode/attributes/attribute.rb

Direct Known Subclasses

Code, SourceFile

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cp) ⇒ Attribute

Returns a new instance of Attribute.



36
37
38
# File 'lib/jvm_bytecode/attributes/attribute.rb', line 36

def initialize(cp)
  @cp = cp
end

Class Attribute Details

.attr_nameObject (readonly)

Returns the value of attribute attr_name.



7
8
9
# File 'lib/jvm_bytecode/attributes/attribute.rb', line 7

def attr_name
  @attr_name
end

.locationsObject (readonly)

Returns the value of attribute locations.



7
8
9
# File 'lib/jvm_bytecode/attributes/attribute.rb', line 7

def locations
  @locations
end

Instance Attribute Details

#cpObject (readonly)

Returns the value of attribute cp.



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

def cp
  @cp
end

Class Method Details

.decode_serial(cp, io) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/jvm_bytecode/attributes/attribute.rb', line 25

def decode_serial(cp, io)
  n = io.read(2).unpack('S>').first
  Array.new(n) do
    name_index = io.read(2).unpack('S>').first
    fetch(cp.constant(name_index).to_s).new(cp).tap { |a| a.decode(io) }
  end
end

.define(name: nil, location:) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/jvm_bytecode/attributes/attribute.rb', line 9

def define(name: nil, location:)
  @attr_name = name || shortname.encode('UTF-8')
  @locations = location

  @@attributes ||= {}
  @@attributes[@attr_name] = self
end

.fetch(attr_name) ⇒ Object



17
18
19
# File 'lib/jvm_bytecode/attributes/attribute.rb', line 17

def fetch(attr_name)
  @@attributes[attr_name] || raise(Errors::AttributeError, "#{attr_name} is not implemented")
end

.locatable_at(loc) ⇒ Object



21
22
23
# File 'lib/jvm_bytecode/attributes/attribute.rb', line 21

def locatable_at(loc)
  @@attributes.select { |_, v| v.locations.include?(loc) }
end

Instance Method Details

#additional_bytecodeObject

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/jvm_bytecode/attributes/attribute.rb', line 40

def additional_bytecode
  raise NotImplementedError, "#{self.class}##{__method__} is not implemented!"
end

#bytecodeObject



44
45
46
47
# File 'lib/jvm_bytecode/attributes/attribute.rb', line 44

def bytecode
  bc = additional_bytecode
  [cp.utf8(self.class.attr_name), bc.length].pack('S>I>') + bc
end