Class | JavaClass::ClassFile::ConstantPool |
In: |
lib/javaclass/classfile/constant_pool.rb
|
Parent: | Object |
Container of the constant pool‘s constants.
Author: | Peter Kofler |
CONSTANT_TYPE_TAGS | = | { 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, } | Types of constants by their tag. |
size | [R] | Size of the whole constant pool in bytes. |
Parse the constant pool from the bytes data beginning at position start (which is usually 8).
# File lib/javaclass/classfile/constant_pool.rb, line 34 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
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 51 def[](index) check_index(index) @pool[index] end
Return the constant class from index‘th pool item.
# File lib/javaclass/classfile/constant_pool.rb, line 84 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
Return a debug output of the whole pool.
# File lib/javaclass/classfile/constant_pool.rb, line 79 def dump [" Constant pool:"] + @pool.keys.sort.collect { |k| "const ##{k} = #{self[k].dump}"} end
Return the constant field from index‘th pool item.
# File lib/javaclass/classfile/constant_pool.rb, line 92 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
Return an array of all constants of the given tags types.
# File lib/javaclass/classfile/constant_pool.rb, line 69 def find(*tags) items.find_all { |item| tags.include? item.tag } end
Return an array of the ordered list of constants.
# File lib/javaclass/classfile/constant_pool.rb, line 64 def items @pool.keys.sort.collect { |k| self[k] } end
Return the constant method from index‘th pool item.
# File lib/javaclass/classfile/constant_pool.rb, line 100 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