Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Created August 20, 2012 22:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinaisan/3408774 to your computer and use it in GitHub Desktop.
Save shinaisan/3408774 to your computer and use it in GitHub Desktop.
A short sample of BinData Ruby gem to pp a constant pool of a Java class file.
require 'bindata'
require 'pp'
class ConstantClassInfo < BinData::Record
uint16be :name_index
end
class ConstantFieldrefInfo < BinData::Record
uint16be :class_index
uint16be :name_and_type_index
end
class ConstantMethodrefInfo < BinData::Record
uint16be :class_index
uint16be :name_and_type_index
end
class ConstantInterfaceMethodrefInfo < BinData::Record
uint16be :class_index
uint16be :name_and_type_index
end
class ConstantStringInfo < BinData::Record
uint16be :string_index
end
class ConstantIntegerInfo < BinData::Record
uint32be :bytes
end
class ConstantFloatInfo < BinData::Record
uint32be :bytes
end
class ConstantLongInfo < BinData::Record
uint32be :high_bytes
uint32be :low_bytes
end
class ConstantDoubleInfo < BinData::Record
uint32be :high_bytes
uint32be :low_bytes
end
class ConstantNameAndTypeInfo < BinData::Record
uint16be :name_index
uint16be :descriptor_index
end
class ConstantUtf8Info < BinData::Record
uint16be :len
string :bytes, :read_length => :len
end
class ConstantMethodHandleInfo < BinData::Record
uint8 :reference_kind
uint16be :reference_index
end
class ConstantMethodTypeInfo < BinData::Record
uint16be :descriptor_index
end
class ConstantInvokeDynamicInfo < BinData::Record
uint16be :bootstrap_method_attr_index
uint16be :name_and_type_index
end
class CpInfo < BinData::Record
uint8 :tag
choice :info, :selection => :tag do
constant_class_info 7
constant_fieldref_info 9
constant_methodref_info 10
constant_interface_methodref_info 11
constant_string_info 8
constant_integer_info 3
constant_float_info 4
constant_long_info 5
constant_double_info 6
constant_name_and_type_info 12
constant_utf8_info 1
constant_method_type_info 16
constant_invoke_dynamic_info 18
end
end
class ClassFile < BinData::Record
endian :big
uint32 :magic
uint16 :minor_version
uint16 :major_version
uint16 :constant_pool_count
array :cp_info, :type => :cp_info, :initial_length => lambda { constant_pool_count - 1}
end
io = File.open("Hello.class")
c = ClassFile.read(io)
pp c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment