068a3304fe41080d0c6c39415e29ed574ac95783
[nit.git] / contrib / jwrapper / src / types.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 # Contains the java and nit type representation used to convert java to nit code
18 module types
19
20 import jtype_converter
21
22 class JavaType
23 private var converter = new JavaTypeConverter
24 var identifier: Array[String] = new Array[String]
25 var generic_params: nullable Array[JavaType] = null
26 var is_void = false
27 var array_dimension = 0
28
29 fun collections_list: Array[String] is cached do return ["List", "ArrayList", "LinkedList", "Vector", "Set", "SortedSet", "HashSet", "TreeSet", "LinkedHashSet", "Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
30 fun iterable: Array[String] is cached do return ["ArrayList", "Set", "HashSet", "LinkedHashSet", "LinkedList", "Stack", "TreeSet", "Vector"]
31 fun maps: Array[String] is cached do return ["Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
32 fun has_generic_params: Bool do return not generic_params == null
33 fun is_primitive_array: Bool do return array_dimension > 0
34 fun full_id: String do return identifier.join(".")
35 fun id: String do return identifier.last
36
37 fun return_cast: String
38 do
39 if self.has_generic_params then
40 return converter.cast_as_return(self.generic_params[0].id)
41 end
42
43 return converter.cast_as_return(self.id)
44 end
45
46 fun param_cast: String
47 do
48 if self.has_generic_params then
49 return converter.cast_as_param(self.generic_params[0].id)
50 end
51
52 return converter.cast_as_param(self.id)
53 end
54
55 fun to_nit_type: NitType
56 do
57 var nit_type: NitType
58
59 if self.is_primitive_array then
60 return self.convert_primitive_array
61 end
62
63 var type_id = converter.to_nit_type(self.id)
64
65 if type_id == null then
66 nit_type = new NitType(self.full_id)
67 nit_type.is_complete = false
68 else
69 nit_type = new NitType(type_id)
70 end
71
72 if not self.has_generic_params then return nit_type
73
74 nit_type.generic_params = new Array[NitType]
75
76 for param in generic_params do
77 var nit_param = param.to_nit_type
78
79 nit_type.generic_params.add(nit_param)
80
81 if not nit_param.is_complete then nit_type.is_complete = false
82 end
83
84 return nit_type
85 end
86
87 fun convert_primitive_array: NitType
88 do
89 var nit_type = new NitType("Array")
90
91 var last_nit_type = nit_type
92
93 for i in [1..array_dimension] do
94 var temp: NitType
95 last_nit_type.generic_params = new Array[NitType]
96
97 if i == array_dimension then
98 var temp_type = converter.to_nit_type(self.id)
99
100 if temp_type == null then
101 temp_type = self.full_id
102 nit_type.is_complete = false
103 end
104
105 temp = new NitType(temp_type)
106 else
107 temp = new NitType("Array")
108 end
109
110 last_nit_type.generic_params.add(temp)
111
112 last_nit_type = temp
113 end
114
115 return nit_type
116 end
117
118 fun is_iterable: Bool do return iterable.has(self.id)
119
120 fun is_collection: Bool do return is_primitive_array or collections_list.has(self.id)
121
122 fun is_map: Bool do return maps.has(self.id)
123
124 redef fun to_s: String
125 do
126 var id = self.full_id
127
128 if self.is_primitive_array then
129 for i in [0..array_dimension[ do
130 id += "[]"
131 end
132 else if self.has_generic_params then
133 var gen_list = new Array[String]
134
135 for param in generic_params do
136 gen_list.add(param.to_s)
137 end
138
139 id += "<{gen_list.join(", ")}>"
140 end
141
142 return id
143 end
144
145 fun to_cast(jtype: String, is_param: Bool): String
146 do
147 if is_param then
148 return converter.cast_as_param(jtype)
149 end
150
151 return converter.cast_as_return(jtype)
152 end
153 end
154
155 class NitType
156 var identifier: String
157 var arg_id: String
158 var generic_params: nullable Array[NitType] = null
159
160 # Returns `true` if all types have been successfully converted to Nit type
161 var is_complete: Bool = true
162
163 fun has_generic_params: Bool do return not generic_params == null
164 fun maps: Array[String] is cached do return ["HashMap", "RBTreeMap"]
165
166 fun id: String do return identifier
167
168 init (id: String)
169 do
170 self.identifier = id
171 end
172
173 fun is_map: Bool do return maps.has(self.identifier)
174
175 redef fun to_s: String
176 do
177 var id = self.identifier
178
179 if self.has_generic_params then
180 var gen_list = new Array[String]
181
182 for param in generic_params do
183 gen_list.add(param.to_s)
184 end
185
186 id += "[{gen_list.join(", ")}]"
187 end
188
189 return id
190 end
191 end