Class: JavaClass::ClassFile::JavaClassHeader

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

Overview

Parse and disassemble Java class files, similar to the javap command. Provides all information of a Java class file. This is just a container for all kind of specialised elements. The constuctor parses and creates all contained elements.

See

java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html

See

en.wikipedia.org/wiki/Class

Author

Peter Kofler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ JavaClassHeader

Create a header with the binary data from the class file.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/javaclass/classfile/java_class_header.rb', line 27

def initialize(data)
  
  #  ClassFile {
  #    u4 magic; - ok
  #    u2 minor_version; - ok
  #    u2 major_version; - ok
  #    u2 constant_pool_count; - ok
  #    cp_info constant_pool[constant_pool_count-1]; - ok
  #    u2 access_flags; - ok
  #    u2 this_class; - ok
  #    u2 super_class; - ok
  # TODO implement function for fields and methods (JVM spec)
  #    u2 interfaces_count;
  #    u2 interfaces[interfaces_count];
  #    u2 fields_count;
  #    field_info fields[fields_count];
  #    u2 methods_count;
  #    method_info methods[methods_count];
  #    u2 attributes_count;
  #    attribute_info attributes[attributes_count];
  #  }
  # TODO Java 1.0 - "private protected" fields.
  
  @magic = ClassMagic.new(data)
  @version = ClassVersion.new(data)
  
  @constant_pool = ConstantPool.new(data)
  pos = 8 + @constant_pool.size
  
  @access_flags = AccessFlags.new(data, pos)
  pos += 2
  
  idx = data.u2(pos)
  pos += 2
  @this_class_idx = idx
  
  @references = References.new(@constant_pool, @this_class_idx)
  
  idx = data.u2(pos)
  pos += 2
  @super_class_idx = idx
end

Instance Attribute Details

#access_flagsObject (readonly)

Returns the value of attribute access_flags.



23
24
25
# File 'lib/javaclass/classfile/java_class_header.rb', line 23

def access_flags
  @access_flags
end

#constant_poolObject (readonly)

Returns the value of attribute constant_pool.



22
23
24
# File 'lib/javaclass/classfile/java_class_header.rb', line 22

def constant_pool
  @constant_pool
end

#magicObject (readonly)

Returns the value of attribute magic.



20
21
22
# File 'lib/javaclass/classfile/java_class_header.rb', line 20

def magic
  @magic
end

#referencesObject (readonly)

Returns the value of attribute references.



24
25
26
# File 'lib/javaclass/classfile/java_class_header.rb', line 24

def references
  @references
end

#versionObject (readonly)

Returns the value of attribute version.



21
22
23
# File 'lib/javaclass/classfile/java_class_header.rb', line 21

def version
  @version
end

Instance Method Details

#dumpObject

Return a debug output of this class that looks similar to javap output.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/javaclass/classfile/java_class_header.rb', line 85

def dump
  d = []
  mod = @access_flags.public? ? 'public ' : ''
  ext = super_class ? "extends #{super_class.to_classname}" : ''
  d << "#{mod}class #{this_class.to_classname} #{ext}" 
  # d << "  SourceFile: \"#{read from LineNumberTable?}\""
  d += @version.dump 
  d += @constant_pool.dump
  d << ''
  d << '{'
  d << '}'
  d
end

#super_classObject

Return the name of the superclass of this class or nil.



76
77
78
79
80
81
82
# File 'lib/javaclass/classfile/java_class_header.rb', line 76

def super_class
  if @super_class_idx > 0
    @constant_pool[@super_class_idx].to_s.to_javaname
  else
    nil
  end
end

#this_classObject

Return the name of this class.



71
72
73
# File 'lib/javaclass/classfile/java_class_header.rb', line 71

def this_class
  @constant_pool[@this_class_idx].to_s.to_javaname
end