Class: JavaClass::ClassFile::ConstantPool

Inherits:
Object
  • Object
show all
Defined in:
lib/javaclass/classfile/constant_pool.rb

Overview

Container of the constant pool’s constants.

Author

Peter Kofler

Constant Summary collapse

CONSTANT_TYPE_TAGS =

Types of constants by their tag.

{
  CLASS_TAG     = 7 => Constants::ConstantClass, 
  FIELD_TAG     = 9 => Constants::ConstantField, 
  METHOD_TAG    = 10 => Constants::ConstantMethod, 
  INTERFACE_METHOD_TAG = 11 => Constants::ConstantInterfaceMethod, 
  STRING_TAG    = 8 => Constants::ConstantString, 
  INT_TAG       = 3 => Constants::ConstantInt, 
  FLOAT_TAG     = 4 => Constants::ConstantFloat, 
  LONG_TAG      = 5 => Constants::ConstantLong, 
  DOUBLE_TAG    = 6 => Constants::ConstantDouble, 
  NAME_AND_TYPE_TAG = 12 => Constants::ConstantNameAndType, 
  ASCIZ_TAG     = 1 => Constants::ConstantAsciz,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, start = 8) ⇒ ConstantPool

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/javaclass/classfile/constant_pool.rb', line 32

def initialize(data, start=8)
  @pool = {} # cnt (fixnum) => constant
  
  # parsing
  @item_count = data.u2(start)
  pos = start + 2
  cnt = 1
  while cnt <= @item_count-1
    
    type = CONSTANT_TYPE_TAGS[data.u1(pos)]
    unless type
      #puts dump.join("\n") 
      raise "const ##{cnt} = unknown constant pool tag #{data[pos]} at pos #{pos} in class"
    end
    
    constant = type.new(@pool, data, pos)
    @pool[cnt] = constant
    pos += constant.size
    cnt += constant.slots
    
  end
  
  @size = pos - start
end

Instance Attribute Details

#sizeObject (readonly)

Size of the whole constant pool in bytes.



29
30
31
# File 'lib/javaclass/classfile/constant_pool.rb', line 29

def size
  @size
end

Instance Method Details

#[](index) ⇒ Object

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



64
65
66
# File 'lib/javaclass/classfile/constant_pool.rb', line 64

def[](index)
  @pool[index]
end

#dumpObject

Return a debug output of the whole pool.



84
85
86
# File 'lib/javaclass/classfile/constant_pool.rb', line 84

def dump
  ["  Constant pool:"] + @pool.keys.sort.collect { |k| "const ##{k} = #{self[k].dump}"}
end

#find(*tags) ⇒ Object

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



74
75
76
# File 'lib/javaclass/classfile/constant_pool.rb', line 74

def find(*tags)
  items.find_all { |item| tags.include? item.tag }
end

#item_countObject

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



59
60
61
# File 'lib/javaclass/classfile/constant_pool.rb', line 59

def item_count
  @item_count-1
end

#itemsObject

Return an array of the ordered list of constants.



69
70
71
# File 'lib/javaclass/classfile/constant_pool.rb', line 69

def items
  @pool.keys.sort.collect { |k| self[k] }
end

#stringsObject

Return all string constants.



79
80
81
# File 'lib/javaclass/classfile/constant_pool.rb', line 79

def strings
  find(STRING_TAG)
end