From cd1057c243b3309651dfd810ee0265213020b28c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Mon, 5 Jan 2015 14:50:25 -0500 Subject: [PATCH] contrib/jwrapper: remove hack to copy arrays MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- contrib/jwrapper/src/code_generator.nit | 154 +----------------------------- contrib/jwrapper/src/jtype_converter.nit | 22 ----- contrib/jwrapper/src/types.nit | 39 +------- 3 files changed, 5 insertions(+), 210 deletions(-) diff --git a/contrib/jwrapper/src/code_generator.nit b/contrib/jwrapper/src/code_generator.nit index 6c52a62..258ee67 100644 --- a/contrib/jwrapper/src/code_generator.nit +++ b/contrib/jwrapper/src/code_generator.nit @@ -207,16 +207,8 @@ class CodeGenerator nit_signature.add ": {return_type} " end - var param_to_copy = param_to_copy(jparam_list, nit_types) - var temp = new Array[String] - if nb_params > 1 then - comment = "#" - temp.add("\t# NOT SUPPORTED: more than one parameter to copy\n") - temp.add("\t# Has to be implemented manually\n") - end - temp.add(comment + nit_signature.join("")) # FIXME : This huge `if` block is only necessary to copy primitive arrays as long as there's no better way to do it @@ -224,31 +216,10 @@ class CodeGenerator temp.add(" in \"Java\" `\{\n{comment}\t\trecv.{jmethod_id}({java_params});\n{comment}\t`\}\n") # Methods with return type else if return_type != null then - if jreturn_type.is_primitive_array then - # Copy one parameter and the return value - if param_to_copy != null then - var rtype_couple = new Couple[JavaType, NitType](jreturn_type, return_type) - temp.add(code_warehouse.param_return_copy(rtype_couple, param_to_copy, jmethod_id, java_params)) - # Copy the return type - else - temp.add(code_warehouse.return_type_copy(jreturn_type, return_type, jmethod_id, java_params)) - end - # Copy the parameter - else if param_to_copy != null then - temp.add(code_warehouse.param_type_copy(param_to_copy.first, param_to_copy.second, jmethod_id, java_params, true)) - # No copy - else - temp.add(" in \"Java\" `\{\n{comment}\t\treturn {jreturn_type.return_cast} recv.{jmethod_id}({java_params});\n{comment}\t`\}\n") - end + temp.add(" in \"Java\" `\{\n{comment}\t\treturn {jreturn_type.return_cast} recv.{jmethod_id}({java_params});\n{comment}\t`\}\n") # Methods without return type else if jreturn_type.is_void then - # Copy one parameter - if param_to_copy != null then - temp.add(code_warehouse.param_type_copy(param_to_copy.first, param_to_copy.second, jmethod_id, java_params, false)) - # No copy - else - temp.add(" in \"Java\" `\{\n{comment}\t\trecv.{jmethod_id}({java_params});\n{comment}\t`\}\n") - end + temp.add(" in \"Java\" `\{\n{comment}\t\trecv.{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") @@ -256,132 +227,11 @@ class CodeGenerator return temp.join("") end - - # Only one primitive array parameter can be copied - # If there's none or more than one then `null` is returned - fun param_to_copy(jtypes: Array[JavaType], ntypes: Array[NitType]): nullable Couple[JavaType, NitType] - do - var counter = 0 - var couple = null - for i in [0..jtypes.length[ do - if jtypes[i].is_primitive_array then - counter += 1 - couple = new Couple[JavaType, NitType](jtypes[i], ntypes[i]) - end - end - - nb_params = counter - - if counter > 1 then return null - return couple - end end # Contains raw code mostly used to copy collections class CodeWarehouse - # Collection as return value - fun return_type_copy(java_type: JavaType, nit_type: NitType, jmethod_id, params_id: String): String - do - var narray_id = "nit_array" - var loop_ = create_loop(java_type, nit_type, false, "java_array", narray_id) - var imports = create_imports(nit_type, false) - - return """{{{imports}}} in "Java" `{ - {{{java_type.to_s}}} java_array = recv.{{{jmethod_id}}}({{{params_id}}}); - int {{{narray_id}}} = new_{{{nit_type.id}}}_of_{{{nit_type.generic_params.join("_")}}}(); - - {{{loop_}}} - - return {{{narray_id}}}; - `} -""" - end - - # Collection as parameter - fun param_type_copy(java_type: JavaType, nit_type: NitType, jmethod_id, params_id: String, has_return: Bool): String - do - var narray_id = "nit_array" - var jarray_id = "java_array" - var loop_ = create_loop(java_type, nit_type, true, jarray_id, narray_id) - var imports = create_imports(nit_type, true) - var jinstanciation = create_array_instance(java_type, nit_type, jarray_id) - var return_str = "" - - if has_return then - return_str = "return " - end - - params_id = params_id.replace(nit_type.arg_id, jarray_id) - - return """{{{imports}}} in "Java" `{ - {{{jinstanciation}}} - int {{{narray_id}}} = new_{{{nit_type.id}}}_of_{{{nit_type.generic_params.join("_")}}}(); - - {{{loop_}}} - - {{{return_str}}}recv.{{{jmethod_id}}}({{{params_id}}}); - `} -""" - end - - # One collection parameter and the return type will be copied - fun param_return_copy(return_types, param_types: Couple[JavaType, NitType], jmethod_id, params_id: String): String - do - var narray_id = "nit_array" - var narray_id2 = "nit_array2" - - var r_jtype = return_types.first - var r_ntype = return_types.second - - var p_jtype = param_types.first - var p_ntype = param_types.second - - var r_loop = create_loop(r_jtype, r_ntype, false, "java_array", narray_id) - var p_loop = create_loop(p_jtype, p_ntype, true, "java_array2", narray_id2) - - var imports = new Array[String] - - # Avoid import duplication - if p_ntype.to_s != r_ntype.to_s then - imports.add create_imports(p_ntype, true) - end - - imports.add create_imports(r_ntype, false) - - params_id = params_id.replace(p_ntype.arg_id, narray_id) - - var jinstanciation = create_array_instance(p_jtype, p_ntype, "java_array") - - return """{{{imports.join(", ")}}} in "Java" `{ - {{{jinstanciation}}} - - {{{p_loop}}} - - {{{r_jtype.to_s}}} java_array2 = recv.{{{jmethod_id}}}({{{params_id}}}); - int {{{narray_id2}}} = new_{{{r_ntype.id}}}_of_{{{r_ntype.generic_params.join("_")}}}(); - - {{{r_loop}}} - - return {{{narray_id2}}}; - `} -""" - end - - private fun create_array_instance(java_type: JavaType, nit_type: NitType, jarray_id: String): String - do - var jtype = java_type.to_s - var instanciation = "" - - if java_type.is_primitive_array then - instanciation = "{jtype} {jarray_id} = new {java_type.full_id}[(int)Array_of_{nit_type.generic_params[0]}_length({nit_type.arg_id})];" - else - instanciation = "{jtype} {jarray_id} = new {jtype}();" - end - - return instanciation - end - private fun create_imports(nit_type: NitType, is_param: Bool): String do var imports = "" diff --git a/contrib/jwrapper/src/jtype_converter.nit b/contrib/jwrapper/src/jtype_converter.nit index c5a8b29..f70d6ed 100644 --- a/contrib/jwrapper/src/jtype_converter.nit +++ b/contrib/jwrapper/src/jtype_converter.nit @@ -61,28 +61,6 @@ class JavaTypeConverter return_cast_map["CharSequence"] = "(String)" end - init with_collections - do - self.init - # Collections - type_map["List"] = "Array" - type_map["ArrayList"] = "Array" - type_map["LinkedList"] = "List" - type_map["Vector"] = "Array" - - type_map["Set"] = "HashSet" - type_map["SortedSet"] = "Still have to make my mind on this one" - type_map["HashSet"] = "HashSet" - type_map["TreeSet"] = "HashSet" - type_map["LinkedHashSet"] = "HashSet" - type_map["Map"] = "HashMap" - type_map["SortedMap"] = "RBTreeMap" - type_map["HashMap"] = "HashMap" - type_map["TreeMap"] = "RBTreeMap" - type_map["Hashtable"] = "HashMap" - type_map["LinkedHashMap"] = "HashMap" - end - fun to_nit_type(java_type: String): nullable String do return self.type_map.get_or_null(java_type) diff --git a/contrib/jwrapper/src/types.nit b/contrib/jwrapper/src/types.nit index 3a14d8d..efe1d01 100644 --- a/contrib/jwrapper/src/types.nit +++ b/contrib/jwrapper/src/types.nit @@ -53,13 +53,12 @@ class JavaType fun to_nit_type: NitType do var nit_type: NitType + var type_id = null - if self.is_primitive_array then - return self.convert_primitive_array + if not is_primitive_array then + type_id = converter.to_nit_type(self.id) end - var type_id = converter.to_nit_type(self.id) - if type_id == null then nit_type = self.extern_name nit_type.is_complete = false @@ -82,38 +81,6 @@ class JavaType return nit_type end - fun convert_primitive_array: NitType - do - var nit_type = new NitType("Array") - - var last_nit_type = nit_type - - for i in [1..array_dimension] do - var temp: NitType - last_nit_type.generic_params = new Array[NitType] - - if i == array_dimension then - var temp_type = converter.to_nit_type(self.id) - - if temp_type == null then - temp = self.extern_name - nit_type.is_complete = false - if temp.mod != null then nit_type.mod = temp.mod - else - temp = new NitType(temp_type) - end - else - temp = new NitType("Array") - end - - last_nit_type.generic_params.add(temp) - - last_nit_type = temp - end - - return nit_type - end - fun is_iterable: Bool do return iterable.has(self.id) fun is_collection: Bool do return is_primitive_array or collections_list.has(self.id) -- 1.7.9.5