c5a8b299c1d145efc8a89133853acc3d67cee4d7
[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["String"] = "JavaString"
47 type_map["CharSequence"] = "JavaString"
48
49
50 # Cast if the type is given as a parameter
51 param_cast_map["byte"] = "(byte)"
52 param_cast_map["Byte"] = "(Byte)"
53 param_cast_map["short"] = "(short)"
54 param_cast_map["Short"] = "(short)"
55 param_cast_map["float"] = "(float)"
56 param_cast_map["Float"] = "(float)"
57 param_cast_map["int"] = "(int)"
58 param_cast_map["Integer"] = "(int)"
59
60 # Cast if the type is given as a return value
61 return_cast_map["CharSequence"] = "(String)"
62 end
63
64 init with_collections
65 do
66 self.init
67 # Collections
68 type_map["List"] = "Array"
69 type_map["ArrayList"] = "Array"
70 type_map["LinkedList"] = "List"
71 type_map["Vector"] = "Array"
72
73 type_map["Set"] = "HashSet"
74 type_map["SortedSet"] = "Still have to make my mind on this one"
75 type_map["HashSet"] = "HashSet"
76 type_map["TreeSet"] = "HashSet"
77 type_map["LinkedHashSet"] = "HashSet"
78 type_map["Map"] = "HashMap"
79 type_map["SortedMap"] = "RBTreeMap"
80 type_map["HashMap"] = "HashMap"
81 type_map["TreeMap"] = "RBTreeMap"
82 type_map["Hashtable"] = "HashMap"
83 type_map["LinkedHashMap"] = "HashMap"
84 end
85
86 fun to_nit_type(java_type: String): nullable String
87 do
88 return self.type_map.get_or_null(java_type)
89 end
90
91 fun cast_as_param(java_type: String): String
92 do
93 return self.param_cast_map.get_or_default(java_type, "")
94 end
95
96 fun cast_as_return(java_type: String): String
97 do
98 return self.return_cast_map.get_or_default(java_type, "")
99 end
100 end