contrib/jwrapper: make casting base objects (Integer, Long, etc.) optional
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 24 Jul 2015 13:07:29 +0000 (09:07 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 24 Jul 2015 16:35:12 +0000 (12:35 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/jwrapper/src/jtype_converter.nit
contrib/jwrapper/src/jwrapper.nit

index 1ef7d7f..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
+       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
@@ -32,38 +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["String"] = "JavaString"
-               type_map["CharSequence"] = "JavaString"
-
 
                # 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)"
                param_cast_map["int"] = "(int)"
-               param_cast_map["Integer"] = "(int)"
 
-               # Cast if the type is given as a return value
-               return_cast_map["CharSequence"] = "(String)"
+               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"
+
+                       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
@@ -75,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, "")
index 60a6ca3..04878da 100644 (file)
@@ -44,7 +44,7 @@ var opt_output = new OptionString("Output file", "-o")
 var opt_regex = new OptionString("Regex pattern to filter classes in Jar archives", "-r")
 var opt_help = new OptionBool("Show this help message", "-h", "--help")
 
-opts.add_option(opt_output, opt_unknown, opt_extern_class_prefix, opt_libs, opt_regex, opt_verbose, opt_help)
+opts.add_option(opt_output, opt_unknown, opt_extern_class_prefix, opt_libs, opt_regex, opt_cast_objects, opt_verbose, opt_help)
 opts.parse args
 
 if opts.errors.not_empty or opts.rest.is_empty or opt_help.value then