Merge: example: add 24 game task of Rosetta code
[nit.git] / contrib / jwrapper / src / code_generator.nit
index ed456f6..ee0942f 100644 (file)
@@ -23,15 +23,23 @@ class CodeGenerator
 
        var with_attributes: Bool
        var comment_unknown_types: Bool
-       var file_out: OFStream
+       var file_out: FileWriter
        var java_class: JavaClass
        var nb_params: Int
-       var module_name: String
+       var module_name: nullable String = null
 
        init (file_name: String, jclass: JavaClass, with_attributes, comment: Bool)
        do
-               file_out = new OFStream.open(file_name)
-               module_name = file_name.substring(0, file_name.search(".nit").from)
+               file_out = new FileWriter.open(file_name)
+
+               var nit_ext = ".nit"
+               if file_name.has_suffix(nit_ext) then
+                       # Output file ends with .nit, we expect it to be a valid name
+                       module_name = file_name.strip_extension(nit_ext)
+
+                       # Otherwise, it may be anything so do not declare a module
+               end
+
                self.java_class = jclass
                self.with_attributes = with_attributes
                self.comment_unknown_types = comment
@@ -71,7 +79,11 @@ class CodeGenerator
                end
 
                file_out.write(gen_licence)
-               file_out.write("module {module_name}\n")
+
+               var module_name = module_name
+               if module_name != null then file_out.write "module {module_name}\n"
+
+               file_out.write("\n")
                file_out.write(imports.join(""))
                file_out.write("\n")
                file_out.write(class_content.join(""))
@@ -211,16 +223,16 @@ class CodeGenerator
 
                # FIXME : This huge `if` block is only necessary to copy primitive arrays as long as there's no better way to do it
                if comment == "#" then
-                       temp.add(" in \"Java\" `\{\n{comment}\t\trecv.{jmethod_id}({java_params});\n{comment}\t`\}\n")
+                       temp.add(" in \"Java\" `\{\n{comment}\t\tself.{jmethod_id}({java_params});\n{comment}\t`\}\n")
                # Methods with return type
                else if return_type != null then
-                       temp.add(" in \"Java\" `\{\n{comment}\t\treturn {jreturn_type.return_cast}recv.{jmethod_id}({java_params});\n{comment}\t`\}\n")
+                       temp.add(" in \"Java\" `\{\n{comment}\t\treturn {jreturn_type.return_cast}self.{jmethod_id}({java_params});\n{comment}\t`\}\n")
                # Methods without return type
                else if jreturn_type.is_void then
-                       temp.add(" in \"Java\" `\{\n{comment}\t\trecv.{jmethod_id}({java_params});\n{comment}\t`\}\n")
+                       temp.add(" in \"Java\" `\{\n{comment}\t\tself.{jmethod_id}({java_params});\n{comment}\t`\}\n")
                # No copy
                else
-                       temp.add(" in \"Java\" `\{\n{comment}\t\trecv.{jmethod_id}({java_params});\n{comment}\t`\}\n")
+                       temp.add(" in \"Java\" `\{\n{comment}\t\tself.{jmethod_id}({java_params});\n{comment}\t`\}\n")
                end
 
                return temp.join("")