class JavaClass::ClassFile::ConstantPool

Container of the constant pool's constants.

Author

Peter Kofler

Constants

CONSTANT_TYPE_TAGS

Types of constants by their tag.

Attributes

size[R]

Size of the whole constant pool in bytes.

Public Class Methods

new(data, start=8) click to toggle source

Parse the constant pool from the bytes data beginning at position start (which is usually 8).

# File lib/javaclass/classfile/constant_pool.rb, line 40
def initialize(data, start=8)
  creator = PoolCreator.new(data, start)
  creator.create!
  
  @pool = creator.pool # cnt (Fixnum) => constant class
  @item_count = creator.item_count
  
  @size = @pool.values.inject(0) { |sum, constant| sum + constant.size } + 2
end

Public Instance Methods

[](index) click to toggle source

Return the index'th pool item. index is the real index in the pool which may skip numbers.

# File lib/javaclass/classfile/constant_pool.rb, line 57
def[](index)
  check_index(index)
  @pool[index]
end
class_item(index) click to toggle source

Return the constant class from index'th pool item.

# File lib/javaclass/classfile/constant_pool.rb, line 90
def class_item(index)
  if self[index] && !self[index].const_class?
    raise ClassFormatError, "inconsistent constant pool entry #{index} for class, should be Constant Class"
  end
  self[index]
end
dump() click to toggle source

Return a debug output of the whole pool.

# File lib/javaclass/classfile/constant_pool.rb, line 85
def dump
  ["  Constant pool:"] + @pool.keys.sort.collect { |k| "const ##{k} = #{self[k].dump}"}
end
field_item(index) click to toggle source

Return the constant field from index'th pool item.

# File lib/javaclass/classfile/constant_pool.rb, line 98
def field_item(index)
  if self[index] && !self[index].const_field?
    raise ClassFormatError, "inconsistent constant pool entry #{index} for field, should be Constant Field"
  end
  self[index]
end
find(*tags) click to toggle source

Return an array of all constants of the given tags types.

# File lib/javaclass/classfile/constant_pool.rb, line 75
def find(*tags)
  items.find_all { |item| tags.include? item.tag }
end
item_count() click to toggle source

Return the number of pool items. This number might be larger than items available, because long and double constants take two slots.

# File lib/javaclass/classfile/constant_pool.rb, line 52
def item_count
  @item_count - 1
end
items() click to toggle source

Return an array of the ordered list of constants.

# File lib/javaclass/classfile/constant_pool.rb, line 70
def items
  @pool.keys.sort.collect { |k| self[k] }
end
method_item(index) click to toggle source

Return the constant method from index'th pool item.

# File lib/javaclass/classfile/constant_pool.rb, line 106
def method_item(index)
  if self[index] && !self[index].const_method?
    raise ClassFormatError, "inconsistent constant pool entry #{index} for method, should be Constant Method"
  end
  self[index]
end
strings() click to toggle source

Return all string constants.

# File lib/javaclass/classfile/constant_pool.rb, line 80
def strings
  find(STRING_TAG)
end

Private Instance Methods

check_index(index) click to toggle source
# File lib/javaclass/classfile/constant_pool.rb, line 62
def check_index(index)
  if index < 0 || index > item_count
    raise IndexError, "index #{index} is out of bounds of constant pool"
  end
end