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

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

Constructor Details

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

Returns a new instance of ClassBuilder.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/duby/jvm/source_generator/builder.rb', line 139

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.



137
138
139
# File 'lib/duby/jvm/source_generator/builder.rb', line 137

def class_name
  @class_name
end

#filenameObject (readonly)

Returns the value of attribute filename.



137
138
139
# File 'lib/duby/jvm/source_generator/builder.rb', line 137

def filename
  @filename
end

#interfacesObject (readonly)

Returns the value of attribute interfaces.



138
139
140
# File 'lib/duby/jvm/source_generator/builder.rb', line 138

def interfaces
  @interfaces
end

#nameObject (readonly)

Returns the value of attribute name.



137
138
139
# File 'lib/duby/jvm/source_generator/builder.rb', line 137

def name
  @name
end

#outObject (readonly)

Returns the value of attribute out.



137
138
139
# File 'lib/duby/jvm/source_generator/builder.rb', line 137

def out
  @out
end

#packageObject (readonly)

Returns the value of attribute package.



137
138
139
# File 'lib/duby/jvm/source_generator/builder.rb', line 137

def package
  @package
end

#superclassObject (readonly)

Returns the value of attribute superclass.



137
138
139
# File 'lib/duby/jvm/source_generator/builder.rb', line 137

def superclass
  @superclass
end

Instance Method Details

#compilerObject



164
165
166
# File 'lib/duby/jvm/source_generator/builder.rb', line 164

def compiler
  @builder.compiler
end

#declare_field(name, type, static, access = 'private', annotations = []) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/duby/jvm/source_generator/builder.rb', line 206

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

#finish_declarationObject



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

def finish_declaration
  return if @declaration_finished
  @declaration_finished = true
  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

#generateObject



247
248
249
250
# File 'lib/duby/jvm/source_generator/builder.rb', line 247

def generate
  stop
  @out.to_s
end

#mainObject



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

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

#public_constructor(exceptions, *args) ⇒ Object



238
239
240
241
242
243
244
245
# File 'lib/duby/jvm/source_generator/builder.rb', line 238

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

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



215
216
217
218
219
220
221
222
223
224
# File 'lib/duby/jvm/source_generator/builder.rb', line 215

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

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



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/duby/jvm/source_generator/builder.rb', line 226

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

#startObject



168
169
170
171
# File 'lib/duby/jvm/source_generator/builder.rb', line 168

def start
  puts "// Generated from #{@builder.filename}"
  puts "package #{package};" if package
end

#stopObject



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

def stop
  finish_declaration
  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