Class: JavaClass::ClassFile::Attributes::AttributesCreator

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, start, constant_pool) ⇒ AttributesCreator

Returns a new instance of AttributesCreator.



31
32
33
34
35
36
# File 'lib/javaclass/classfile/attributes/attributes.rb', line 31

def initialize(data, start, constant_pool)
  @data = data
  @start = start
  @pos = start
  @constant_pool = constant_pool
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

Instance Method Details

#create!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/javaclass/classfile/attributes/attributes.rb', line 42

def create!
  @attributes = []

  count = @data.u2(@pos)
  @pos += 2

  (1..count).each do |i|
    attribute_name_idx = @data.u2(@pos) 
    @pos += 2
    attribute_name = @constant_pool[attribute_name_idx].string

    attribute_length = @data.u4(@pos)
    @pos += 4
    
    if attribute_name == 'SourceFile'
      @attributes << source_file(attribute_name)
    elsif attribute_name == 'InnerClasses'
      @attributes << inner_classes(attribute_name)
    else # skip 
      @pos += attribute_length 
    end

  end
end

#inner_classes(attribute_name) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/javaclass/classfile/attributes/attributes.rb', line 74

def inner_classes(attribute_name)
  number_of_classes = @data.u2(@pos)
  @pos += 2
  inner_classes = (1..number_of_classes).collect do |i|
    inner_class_info_idx = @data.u2(@pos)
    @pos += 2
    
    @pos += 4
    inner_class_access_flags = @data.u2(@pos)
    @pos += 2

    inner_class_info = @constant_pool[inner_class_info_idx]

    InnerClass.new(inner_class_info.class_name, AccessFlags.new(inner_class_access_flags))
  end
  InnerClasses.new(attribute_name, inner_classes)
end

#sizeObject



38
39
40
# File 'lib/javaclass/classfile/attributes/attributes.rb', line 38

def size
  @pos - @start
end

#source_file(attribute_name) ⇒ Object



67
68
69
70
71
72
# File 'lib/javaclass/classfile/attributes/attributes.rb', line 67

def source_file(attribute_name)
  sourcefile_idx = @data.u2(@pos)
  @pos += 2
  source_file = @constant_pool[sourcefile_idx].string
  SourceFile.new(attribute_name, source_file)
end