1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 # Copyright 2015 Alexis Laferrière <alexis.laf@xymus.net>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Services to convert java type to nit type and get casts if needed
19 module jtype_converter
24 # Converter between Java and Nit type
25 var converter
= new JavaTypeConverter is lazy
27 # Option to treat base Java objects as an equivalent to those in Nit?
29 # This concerns `Byte, Integer, Float, etc`.
30 var opt_cast_objects
= new OptionBool("Convert base objects as their primitive equivalent", "-c")
33 class JavaTypeConverter
35 var type_map
= new HashMap[String, String]
36 var param_cast_map
= new HashMap[String, String]
37 var return_cast_map
= new HashMap[String, String]
41 # Java type to nit type
42 type_map
["byte"] = "Int"
43 type_map
["short"] = "Int"
44 type_map
["int"] = "Int"
45 type_map
["long"] = "Int"
46 type_map
["char"] = "Char"
47 type_map
["float"] = "Float"
48 type_map
["double"] = "Float"
49 type_map
["boolean"] = "Bool"
51 # Cast if the type is given as a parameter
52 param_cast_map
["byte"] = "(byte)"
53 param_cast_map
["short"] = "(short)"
54 param_cast_map
["float"] = "(float)"
55 param_cast_map
["int"] = "(int)"
57 if opt_cast_objects
.value
then
58 type_map
["Byte"] = "Int"
59 type_map
["Short"] = "Int"
60 type_map
["Integer"] = "Int"
61 type_map
["Long"] = "Int"
62 type_map
["Character"] = "Char"
63 type_map
["Float"] = "Float"
64 type_map
["Double"] = "Float"
65 type_map
["Boolean"] = "Bool"
66 type_map
["CharSequence"] = "JavaString"
68 param_cast_map
["Byte"] = "(Byte)"
69 param_cast_map
["Short"] = "(short)"
70 param_cast_map
["Float"] = "(float)"
71 param_cast_map
["Integer"] = "(int)"
73 # Cast if the type is given as a return value
74 return_cast_map
["CharSequence"] = "(String)"
78 fun to_nit_type
(java_type
: String): nullable String
80 return self.type_map
.get_or_null
(java_type
)
83 fun cast_as_param
(java_type
: String): String
85 return self.param_cast_map
.get_or_default
(java_type
, "")
88 fun cast_as_return
(java_type
: String): String
90 return self.return_cast_map
.get_or_default
(java_type
, "")