Permalink
Browse files

fix main in files w/ classes of same name contained in them

if we're in a file w/ a name that's the same as a class contained by it,
and we've decided to generate a main for that class from statements in the file
outside the class body,
don't close the class until after the main method has been generated
  • Loading branch information...
1 parent 3b69287 commit 5c2b47c698044938996c09f6db94feb11968d56f @baroquebobcat baroquebobcat committed Aug 21, 2011
Showing with 28 additions and 2 deletions.
  1. +11 −1 lib/mirah/jvm/compiler/java_source.rb
  2. +2 −1 lib/mirah/jvm/source_generator/builder.rb
  3. +15 −0 test/jvm/test_main_method.rb
View
12 lib/mirah/jvm/compiler/java_source.rb
@@ -56,7 +56,17 @@ def file_builder(filename)
def output_type
"source files"
end
-
+
+ def define_class(class_def, expression)
+ with(:type => class_def.inferred_type,
+ :class => class_def.inferred_type.define(@file),
+ :static => false) do
+ annotate(@class, class_def.annotations)
+ class_def.body.compile(self, false) if class_def.body
+ @class.stop unless @method && @method.name == 'main' && @class == @method.klass
+ end
+ end
+
def define_method(node)
base_define_method(node, false) do |method, _|
with :method => method do
View
3 lib/mirah/jvm/source_generator/builder.rb
@@ -341,10 +341,11 @@ def build_method(name, visibility, static, exceptions, type, *args)
class MethodBuilder
include Helper
- attr_accessor :name, :type, :out
+ attr_accessor :name, :type, :out, :klass
def initialize(cls, options)
@class = cls
+ @klass = cls
@compiler = cls.compiler
@out = Output.new
@visibility = options[:visibility]
View
15 test/jvm/test_main_method.rb
@@ -0,0 +1,15 @@
+class TestMainMethod < Test::Unit::TestCase
+ def test_main_generation_for_file_with_class_of_same_name
+ code = <<-EOC
+ class WithMain
+ end
+ puts 'bar'
+ EOC
+
+ main_class, = compile code, 'with_main'
+
+ assert_output "bar\n" do
+ main_class.main(nil)
+ end
+ end
+end

0 comments on commit 5c2b47c

Please sign in to comment.