contrib/jwrapper: default name of Java primitive arrays are Java?Array
[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: JavaTypeConverter
24 var identifier = new Array[String]
25 var generic_params: nullable Array[JavaType] = null
26 var is_void = false
27
28 # Has some generic type to be resolved (T extends foo => T is resolved to foo)
29 var has_unresolved_types = false
30
31 # Dimension of primitive array: `int[][]` is 2d
32 var array_dimension = 0
33
34 fun is_primitive_array: Bool do return array_dimension > 0
35
36 fun has_generic_params: Bool do return not generic_params == null
37 fun full_id: String do return identifier.join(".")
38 fun id: String do return identifier.last.replace("$", "")
39
40 init(converter: JavaTypeConverter) do self.converter = converter
41
42 fun return_cast: String do return converter.cast_as_return(self.id)
43
44 fun param_cast: String
45 do
46 if self.has_generic_params then
47 return converter.cast_as_param(self.generic_params[0].id)
48 end
49
50 return converter.cast_as_param(self.id)
51 end
52
53 fun to_nit_type: NitType
54 do
55 var nit_type: NitType
56 var type_id = null
57
58 if not is_primitive_array then
59 type_id = converter.to_nit_type(self.id)
60 end
61
62 if type_id == null then
63 nit_type = self.extern_name
64 nit_type.is_complete = false
65 else
66 nit_type = new NitType(type_id)
67 end
68
69 if not self.has_generic_params then return nit_type
70
71 nit_type.generic_params = new Array[NitType]
72
73 for param in generic_params do
74 var nit_param = param.to_nit_type
75
76 nit_type.generic_params.add(nit_param)
77
78 if not nit_param.is_complete then nit_type.is_complete = false
79 end
80
81 return nit_type
82 end
83
84 fun is_iterable: Bool do return iterable.has(self.id)
85
86 fun is_collection: Bool do return is_primitive_array or collections_list.has(self.id)
87
88 fun is_map: Bool do return maps.has(self.id)
89
90 fun is_wrapped: Bool do return find_extern_class != null
91
92 fun extern_name: NitType
93 do
94 if is_wrapped then return new NitType.with_module(find_extern_class.as(not null).first, find_extern_class.as(not null).second)
95
96 var name
97 if is_primitive_array then
98 # Primitive arrays have a special naming convention
99 name = "Native" + extern_class_name.join("").capitalized + "Array"
100 else
101 name = "Native" + extern_class_name.join("")
102 end
103
104 var nit_type = new NitType(name)
105 nit_type.is_complete = false
106 return nit_type
107 end
108
109 fun to_cast(jtype: String, is_param: Bool): String
110 do
111 if is_param then
112 return converter.cast_as_param(jtype)
113 end
114
115 return converter.cast_as_return(jtype)
116 end
117
118 redef fun to_s: String
119 do
120 var id = self.full_id
121
122 if self.is_primitive_array then
123 for i in [0..array_dimension[ do
124 id += "[]"
125 end
126 else if self.has_generic_params then
127 var gen_list = new Array[String]
128
129 for param in generic_params do
130 gen_list.add(param.to_s)
131 end
132
133 id += "<{gen_list.join(", ")}>"
134 end
135
136 return id
137 end
138
139 # To fully qualified package name
140 # Cuts the primitive array `[]`
141 fun to_package_name: String
142 do
143 var str = self.to_s
144 var len = str.length
145
146 return str.substring(0, len - (2*array_dimension))
147 end
148
149 fun resolve_types(conversion_map: HashMap[String, Array[String]])
150 do
151 if identifier.length == 1 then
152 var resolved_id = conversion_map.get_or_null(self.id)
153 if resolved_id != null then self.identifier = new Array[String].from(resolved_id)
154 end
155
156 if self.has_generic_params then
157 for params in generic_params do params.resolve_types(conversion_map)
158 end
159 end
160
161 private fun extern_class_name: Array[String]
162 do
163 var class_name = new Array[String]
164 class_name.add(self.id)
165
166 if not self.has_generic_params then return class_name
167
168 class_name.add "Of"
169
170 for param in generic_params do class_name.add_all param.extern_class_name
171
172 return class_name
173 end
174
175 # Search inside `lib/android` directory for already wrapped classes
176 # If found, contains the class identifier and the Nit Module name
177 var find_extern_class: nullable Couple[String, NitModule] = find_extern_class_fun is lazy
178
179 private fun find_extern_class_fun: nullable Couple[String, NitModule]
180 do
181 var regex = "extern class Native[a-zA-Z1-9]\\\+[ ]\\\+in[ ]\\\+\"Java\"[ ]*`\{[ ]*" + self.to_s + "\\\+[ ]*`\}"
182 var grep = new IProcess("grep", "-r", regex, "{"NIT_DIR".environ}/lib/android/")
183 var to_eat = ["private", "extern", "class"]
184
185 var output = grep.read_line
186
187 var output_class = output.substring_from(output.index_of(':') + 1)
188 var tokens = output_class.split(" ")
189
190 var nclass_name = ""
191
192 for token in tokens do
193 if to_eat.has(token) then continue
194 nclass_name = token
195 break
196 end
197
198 if nclass_name == "" then return null
199
200 var str = output.substring(0, output.search(".nit").from)
201 str = str.substring_from(str.last_index_of('/') + 1)
202 var mod = new NitModule(str)
203
204 return new Couple[String, NitModule](nclass_name, mod)
205 end
206
207 # Comparison based on fully qualified named and generic params
208 # Ignores primitive array so `a.b.c[][] == a.b.c`
209 redef fun ==(other)
210 do
211 if other isa JavaType then
212 return self.repr == other.repr
213 end
214 return false
215 end
216
217 redef fun hash do return self.repr.hash
218
219 private fun repr: String
220 do
221 var id = self.full_id
222
223 if self.has_generic_params then
224 var gen_list = new Array[String]
225
226 for param in generic_params do
227 gen_list.add(param.to_s)
228 end
229
230 id += "<{gen_list.join(", ")}>"
231 end
232
233 return id
234 end
235
236 fun collections_list: Array[String] is cached do return ["List", "ArrayList", "LinkedList", "Vector", "Set", "SortedSet", "HashSet", "TreeSet", "LinkedHashSet", "Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
237 fun iterable: Array[String] is cached do return ["ArrayList", "Set", "HashSet", "LinkedHashSet", "LinkedList", "Stack", "TreeSet", "Vector"]
238 fun maps: Array[String] is cached do return ["Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
239 end
240
241 class NitType
242 var identifier: String
243 var arg_id: String
244 var generic_params: nullable Array[NitType] = null
245
246 # If this NitType was found in `lib/android`, contains the module name to import
247 var mod: nullable NitModule
248
249 # Returns `true` if all types have been successfully converted to Nit type
250 var is_complete: Bool = true
251
252 fun has_generic_params: Bool do return not generic_params == null
253 fun maps: Array[String] is cached do return ["HashMap", "RBTreeMap"]
254
255 fun id: String do return identifier
256
257 init (id: String)
258 do
259 self.identifier = id
260 end
261
262 init with_generic_params(id: String, gen_params: String...)
263 do
264 self.init(id)
265 self.generic_params = new Array[NitType]
266 for param in gen_params do self.generic_params.add new NitType(param)
267 end
268
269 init with_module(id: String, mod: NitModule)
270 do
271 self.init(id)
272 self.mod = mod
273 end
274
275 fun is_map: Bool do return maps.has(self.identifier)
276
277 redef fun to_s: String
278 do
279 var id = self.identifier
280
281 if self.has_generic_params then
282 var gen_list = new Array[String]
283
284 for param in generic_params do
285 gen_list.add(param.to_s)
286 end
287
288 id += "[{gen_list.join(", ")}]"
289 end
290
291 return id
292 end
293 end
294
295 class JavaClass
296 var class_type = new JavaType(new JavaTypeConverter)
297 var attributes = new HashMap[String, JavaType]
298 var methods = new HashMap[String, Array[JReturnAndParams]]
299 var unknown_types = new HashSet[JavaType]
300 var imports = new HashSet[NitModule]
301
302 fun add_method(id: String, return_type: JavaType, params: Array[JavaType])
303 do
304 var ret_and_params = methods.get_or_default(id, new Array[JReturnAndParams])
305
306 ret_and_params.add(new JReturnAndParams(return_type, new Array[JavaType].from(params)))
307 methods[id] = ret_and_params
308 end
309 end
310
311 class JReturnAndParams
312 var return_type: JavaType
313 var params: Array[JavaType]
314
315 init(return_type: JavaType, params: Array[JavaType])
316 do
317 self.return_type = return_type
318 self.params = params
319 end
320 end
321
322 class NitModule
323 var value: String
324
325 init(str: String) do value = str
326
327 redef fun ==(other): Bool do return self.to_s == other.to_s
328 redef fun to_s: String do return self.value
329 redef fun hash: Int do return self.value.hash
330 end