Class: Duby::JavaSource::ClassBuilder

Inherits:
Object
  • Object
show all
Includes:
Compiler::JVM::JVMLogger, Helper
Defined in:
lib/duby/jvm/source_generator/builder.rb

Direct Known Subclasses

InterfaceBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Compiler::JVM::JVMLogger

#log

Methods included from Helper

#block, #dedent, #indent, #init_value, #print, #puts

Constructor Details

#initialize(builder, name, superclass, interfaces) ⇒ ClassBuilder

Returns a new instance of ClassBuilder.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/duby/jvm/source_generator/builder.rb', line 131

def initialize(builder, name, superclass, interfaces)
  @builder = builder
  @package = builder.package
  if @package
    @name = "#{@package}.#{name}"
  else
    @name = name
  end
  if name =~ %r{[/.]}
    pieces = name.split(%r{[/.]})
    name = pieces.pop
    @package = pieces.join('.')
  end
  @class_name = name
  @superclass = superclass || JVMTypes::Object
  @interfaces = interfaces
  @filename = "#{name}.java"
  @filename = "#{package.tr('.', '/')}/#{@filename}" if @package
  @out = Output.new
  @stopped = false
  @methods = []
  @fields = {}
  start
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



129
130
131
# File 'lib/duby/jvm/source_generator/builder.rb', line 129

def class_name
  @class_name
end

#filenameObject (readonly)

Returns the value of attribute filename.



129
130
131
# File 'lib/duby/jvm/source_generator/builder.rb', line 129

def filename
  @filename
end

#interfacesObject (readonly)

Returns the value of attribute interfaces.



130
131
132
# File 'lib/duby/jvm/source_generator/builder.rb', line 130

def interfaces
  @interfaces
end

#nameObject (readonly)

Returns the value of attribute name.



129
130
131
# File 'lib/duby/jvm/source_generator/builder.rb', line 129

def name
  @name
end

#outObject (readonly)

Returns the value of attribute out.



129
130
131
# File 'lib/duby/jvm/source_generator/builder.rb', line 129

def out
  @out
end

#packageObject (readonly)

Returns the value of attribute package.



129
130
131
# File 'lib/duby/jvm/source_generator/builder.rb', line 129

def package
  @package
end

#superclassObject (readonly)

Returns the value of attribute superclass.



129
130
131
# File 'lib/duby/jvm/source_generator/builder.rb', line 129

def superclass
  @superclass
end

Instance Method Details

#compilerObject



156
157
158
# File 'lib/duby/jvm/source_generator/builder.rb', line 156

def compiler
  @builder.compiler
end

#declare_field(name, type, static) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/duby/jvm/source_generator/builder.rb', line 192

def declare_field(name, type, static)
  return if @fields[name]
  static = static ? 'static' : ''
  value = init_value(type)
  puts "private #{static} #{type.to_source} #{name} = #{value};"
  @fields[name] = true
end

#generateObject



227
228
229
230
# File 'lib/duby/jvm/source_generator/builder.rb', line 227

def generate
  stop
  @out.to_s
end

#mainObject



187
188
189
190
# File 'lib/duby/jvm/source_generator/builder.rb', line 187

def main
  public_static_method('main', JVMTypes::Void, [],
                       [JVMTypes::String.array_type, 'argv'])
end

#public_constructor(exceptions, *args) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/duby/jvm/source_generator/builder.rb', line 219

def public_constructor(exceptions, *args)
  @methods << MethodBuilder.new(self,
                                :name => class_name,
                                :args => args,
                                :exceptions => exceptions)
  @methods[-1]
end

#public_method(name, type, exceptions, *args) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/duby/jvm/source_generator/builder.rb', line 200

def public_method(name, type, exceptions, *args)
  @methods << MethodBuilder.new(self,
                                :name => name,
                                :return => type,
                                :args => args,
                                :exceptions => exceptions)
  @methods[-1]
end

#public_static_method(name, type, exceptions, *args) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/duby/jvm/source_generator/builder.rb', line 209

def public_static_method(name, type, exceptions, *args)
  @methods << MethodBuilder.new(self,
                                :name => name,
                                :return => type,
                                :args => args,
                                :static => true,
                                :exceptions => exceptions)
  @methods[-1]
end

#startObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/duby/jvm/source_generator/builder.rb', line 160

def start
  puts "// Generated from #{@builder.filename}"
  puts "package #{package};" if package
  print "public class #{class_name} extends #{superclass.name}"
  unless @interfaces.empty?
    print " implements "
    @interfaces.each_with_index do |interface, index|
      print ', ' unless index == 0
      print interface.to_source
    end
  end
  puts " {"
  indent
end

#stopObject



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/duby/jvm/source_generator/builder.rb', line 175

def stop
  return if @stopped
  @methods.each do |method|
    @out << method.out
  end
  log "Class #{name} complete (#{@out.to_s.size})"
  @stopped = true
  dedent
  puts "}"
  log "Class #{name} complete (#{@out.to_s.size})"
end