Merge: nitunit: Use markdown2
[nit.git] / contrib / jwrapper / src / jtype_converter.nit
index 95adbb4..b574985 100644 (file)
@@ -1,6 +1,7 @@
 # This file is part of NIT (http://www.nitlanguage.org).
 #
 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
+# Copyright 2015 Alexis Laferrière <alexis.laf@xymus.net>
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # Services to convert java type to nit type and get casts if needed
 module jtype_converter
 
+import opts
+
+redef class Sys
+       # Converter between Java and Nit type
+       var converter = new JavaTypeConverter is lazy
+
+       # Option to treat base Java objects as an equivalent to those in Nit?
+       #
+       # This concerns `Byte, Integer, Float, etc`.
+       var opt_cast_objects = new OptionBool("Convert base objects as their primitive equivalent", "-c")
+end
+
 class JavaTypeConverter
 
        var type_map = new HashMap[String, String]
@@ -27,59 +40,39 @@ class JavaTypeConverter
        do
                # Java type to nit type
                type_map["byte"] = "Int"
-               type_map["Byte"] = "Int"
                type_map["short"] = "Int"
-               type_map["Short"] = "Int"
                type_map["int"] = "Int"
-               type_map["Integer"] = "Int"
                type_map["long"] = "Int"
-               type_map["Long"] = "Int"
                type_map["char"] = "Char"
-               type_map["Character"] = "Char"
                type_map["float"] = "Float"
-               type_map["Float"] = "Float"
                type_map["double"] = "Float"
-               type_map["Double"] = "Float"
                type_map["boolean"] = "Bool"
-               type_map["Boolean"] = "Bool"
-               type_map["Object"] = "JavaObject"
-               type_map["Bundle"] = "NativeBundle"
-               type_map["String"] = "JavaString"
-               type_map["CharSequence"] = "JavaString"
-
-               # 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"
 
                # Cast if the type is given as a parameter
                param_cast_map["byte"] = "(byte)"
-               param_cast_map["Byte"] = "(Byte)"
                param_cast_map["short"] = "(short)"
-               param_cast_map["Short"] = "(short)"
                param_cast_map["float"] = "(float)"
-               param_cast_map["Float"] = "(float)"
-               # FIXME: Uncomment as soon as Nit `Int` will be equivalent to Java `long`
-               # param_cast_map["int"] = "int"
-               # param_cast_map["Integer"] = "int"
+               param_cast_map["int"] = "(int)"
+
+               if opt_cast_objects.value then
+                       type_map["Byte"] = "Int"
+                       type_map["Short"] = "Int"
+                       type_map["Integer"] = "Int"
+                       type_map["Long"] = "Int"
+                       type_map["Character"] = "Char"
+                       type_map["Float"] = "Float"
+                       type_map["Double"] = "Float"
+                       type_map["Boolean"] = "Bool"
+                       type_map["CharSequence"] = "JavaString"
 
-               # Cast if the type is given as a return value
-               return_cast_map["CharSequence"] = "(String)"
-               # FIXME: Erase as soon as the Nit `Int` type will become a Java `long`
-               return_cast_map["long"] = "(int)"
+                       param_cast_map["Byte"] = "(Byte)"
+                       param_cast_map["Short"] = "(short)"
+                       param_cast_map["Float"] = "(float)"
+                       param_cast_map["Integer"] = "(int)"
+
+                       # Cast if the type is given as a return value
+                       return_cast_map["CharSequence"] = "(String)"
+               end
        end
 
        fun to_nit_type(java_type: String): nullable String
@@ -91,7 +84,7 @@ class JavaTypeConverter
        do
                return self.param_cast_map.get_or_default(java_type, "")
        end
-       
+
        fun cast_as_return(java_type: String): String
        do
                return self.return_cast_map.get_or_default(java_type, "")