contrib/jwrapper: Added code generation module with code conversion maps
[nit.git] / contrib / jwrapper / src / jtype_converter.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Services to convert java type to nit type and get casts if needed
18 module jtype_converter
19
20 class JavaTypeConverter
21
22 var type_map = new HashMap[String, String]
23 var param_cast_map = new HashMap[String, String]
24 var return_cast_map = new HashMap[String, String]
25
26 init
27 do
28 # Java type to nit type
29 type_map["byte"] = "Int"
30 type_map["Byte"] = "Int"
31 type_map["short"] = "Int"
32 type_map["Short"] = "Int"
33 type_map["int"] = "Int"
34 type_map["Integer"] = "Int"
35 type_map["long"] = "Int"
36 type_map["Long"] = "Int"
37 type_map["char"] = "Char"
38 type_map["Character"] = "Char"
39 type_map["float"] = "Float"
40 type_map["Float"] = "Float"
41 type_map["double"] = "Float"
42 type_map["Double"] = "Float"
43 type_map["boolean"] = "Bool"
44 type_map["Boolean"] = "Bool"
45 type_map["Object"] = "JavaObject"
46 type_map["Bundle"] = "NativeBundle"
47 type_map["String"] = "JavaString"
48 type_map["CharSequence"] = "JavaString"
49
50 # Collections
51 type_map["List"] = "Array"
52 type_map["ArrayList"] = "Array"
53 type_map["LinkedList"] = "List"
54 type_map["Vector"] = "Array"
55
56 type_map["Set"] = "HashSet"
57 type_map["SortedSet"] = "Still have to make my mind on this one"
58 type_map["HashSet"] = "HashSet"
59 type_map["TreeSet"] = "HashSet"
60 type_map["LinkedHashSet"] = "HashSet"
61 type_map["Map"] = "HashMap"
62 type_map["SortedMap"] = "RBTreeMap"
63 type_map["HashMap"] = "HashMap"
64 type_map["TreeMap"] = "RBTreeMap"
65 type_map["Hashtable"] = "HashMap"
66 type_map["LinkedHashMap"] = "HashMap"
67
68 # Cast if the type is given as a parameter
69 param_cast_map["byte"] = "(byte)"
70 param_cast_map["Byte"] = "(Byte)"
71 param_cast_map["short"] = "(short)"
72 param_cast_map["Short"] = "(short)"
73 param_cast_map["float"] = "(float)"
74 param_cast_map["Float"] = "(float)"
75 # FIXME: Uncomment as soon as Nit `Int` will be equivalent to Java `long`
76 # param_cast_map["int"] = "int"
77 # param_cast_map["Integer"] = "int"
78
79 # Cast if the type is given as a return value
80 return_cast_map["CharSequence"] = "(String)"
81 # FIXME: Erase as soon as the Nit `Int` type will become a Java `long`
82 return_cast_map["long"] = "(int)"
83 end
84
85 fun to_nit_type(java_type: String): nullable String
86 do
87 return self.type_map.get_or_null(java_type)
88 end
89
90 fun cast_as_param(java_type: String): String
91 do
92 return self.param_cast_map.get_or_default(java_type, "")
93 end
94
95 fun cast_as_return(java_type: String): String
96 do
97 return self.return_cast_map.get_or_default(java_type, "")
98 end
99 end