1ef7d7f1cdd55b5b4e0a0f59ad27c7fa7267f36c
[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 redef class Sys
21 # Converter between Java and Nit type
22 var converter = new JavaTypeConverter
23 end
24
25 class JavaTypeConverter
26
27 var type_map = new HashMap[String, String]
28 var param_cast_map = new HashMap[String, String]
29 var return_cast_map = new HashMap[String, String]
30
31 init
32 do
33 # Java type to nit type
34 type_map["byte"] = "Int"
35 type_map["Byte"] = "Int"
36 type_map["short"] = "Int"
37 type_map["Short"] = "Int"
38 type_map["int"] = "Int"
39 type_map["Integer"] = "Int"
40 type_map["long"] = "Int"
41 type_map["Long"] = "Int"
42 type_map["char"] = "Char"
43 type_map["Character"] = "Char"
44 type_map["float"] = "Float"
45 type_map["Float"] = "Float"
46 type_map["double"] = "Float"
47 type_map["Double"] = "Float"
48 type_map["boolean"] = "Bool"
49 type_map["Boolean"] = "Bool"
50 type_map["Object"] = "JavaObject"
51 type_map["String"] = "JavaString"
52 type_map["CharSequence"] = "JavaString"
53
54
55 # Cast if the type is given as a parameter
56 param_cast_map["byte"] = "(byte)"
57 param_cast_map["Byte"] = "(Byte)"
58 param_cast_map["short"] = "(short)"
59 param_cast_map["Short"] = "(short)"
60 param_cast_map["float"] = "(float)"
61 param_cast_map["Float"] = "(float)"
62 param_cast_map["int"] = "(int)"
63 param_cast_map["Integer"] = "(int)"
64
65 # Cast if the type is given as a return value
66 return_cast_map["CharSequence"] = "(String)"
67 end
68
69 fun to_nit_type(java_type: String): nullable String
70 do
71 return self.type_map.get_or_null(java_type)
72 end
73
74 fun cast_as_param(java_type: String): String
75 do
76 return self.param_cast_map.get_or_default(java_type, "")
77 end
78
79 fun cast_as_return(java_type: String): String
80 do
81 return self.return_cast_map.get_or_default(java_type, "")
82 end
83 end