# 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
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
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, "")
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