contrib/jwrapper: remove hack to copy arrays
[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 fun to_nit_type(java_type: String): nullable String
65 do
66 return self.type_map.get_or_null(java_type)
67 end
68
69 fun cast_as_param(java_type: String): String
70 do
71 return self.param_cast_map.get_or_default(java_type, "")
72 end
73
74 fun cast_as_return(java_type: String): String
75 do
76 return self.return_cast_map.get_or_default(java_type, "")
77 end
78 end