5dcd96da5c05c4cf8904c5f58e1ea4264ae6fa9b
[nit.git] / contrib / jwrapper / src / model.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 # Copyright 2015 Alexis Laferrière <alexis.laf@xymus.net>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Contains the java and nit type representation used to convert java to nit code
19 module model
20
21 import more_collections
22
23 import jtype_converter
24
25 class JavaType
26 private var converter: JavaTypeConverter
27 var identifier = new Array[String]
28 var generic_params: nullable Array[JavaType] = null
29 var is_void = false
30
31 # Has some generic type to be resolved (T extends foo => T is resolved to foo)
32 var has_unresolved_types = false
33
34 # Dimension of primitive array: `int[][]` is 2d
35 var array_dimension = 0
36
37 fun is_primitive_array: Bool do return array_dimension > 0
38
39 fun has_generic_params: Bool do return not generic_params == null
40 fun full_id: String do return identifier.join(".")
41 fun id: String do return identifier.last.replace("$", "")
42
43 init(converter: JavaTypeConverter) do self.converter = converter
44
45 fun return_cast: String do return converter.cast_as_return(self.id)
46
47 fun param_cast: String
48 do
49 if self.has_generic_params then
50 return converter.cast_as_param(self.generic_params[0].id)
51 end
52
53 return converter.cast_as_param(self.id)
54 end
55
56 fun to_nit_type: NitType
57 do
58 var nit_type: NitType
59 var type_id = null
60
61 if not is_primitive_array then
62 type_id = converter.to_nit_type(self.id)
63 end
64
65 if type_id == null then
66 nit_type = self.extern_name
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 is_collection: Bool do return is_primitive_array or collections_list.has(self.id)
88
89 fun is_wrapped: Bool do return find_extern_class != null
90
91 fun extern_name: NitType
92 do
93 if is_wrapped then return new NitType.with_module(find_extern_class.as(not null).first, find_extern_class.as(not null).second)
94
95 var name
96 if is_primitive_array then
97 # Primitive arrays have a special naming convention
98 name = "Java" + extern_class_name.join.capitalized + "Array"
99 else
100 name = "Java" + extern_class_name.join
101 end
102
103 var nit_type = new NitType(name)
104 nit_type.is_complete = false
105 return nit_type
106 end
107
108 fun to_cast(jtype: String, is_param: Bool): String
109 do
110 if is_param then
111 return converter.cast_as_param(jtype)
112 end
113
114 return converter.cast_as_return(jtype)
115 end
116
117 redef fun to_s
118 do
119 var id = self.full_id
120
121 if self.is_primitive_array then
122 id += "[]" * array_dimension
123 else if self.has_generic_params then
124 var params = [for param in generic_params do param.to_s]
125 id += "<{params.join(", ")}>"
126 end
127
128 return id
129 end
130
131 # To fully qualified package name
132 # Cuts the primitive array `[]`
133 fun to_package_name: String
134 do
135 var str = self.to_s
136 var len = str.length
137
138 return str.substring(0, len - (2*array_dimension))
139 end
140
141 fun resolve_types(conversion_map: HashMap[String, Array[String]])
142 do
143 if identifier.length == 1 then
144 var resolved_id = conversion_map.get_or_null(self.id)
145 if resolved_id != null then self.identifier = new Array[String].from(resolved_id)
146 end
147
148 if self.has_generic_params then
149 for params in generic_params do params.resolve_types(conversion_map)
150 end
151 end
152
153 private fun extern_class_name: Array[String]
154 do
155 var class_name = new Array[String]
156 class_name.add(self.id)
157
158 if not self.has_generic_params then return class_name
159
160 class_name.add "Of"
161
162 for param in generic_params do class_name.add_all param.extern_class_name
163
164 return class_name
165 end
166
167 # Search inside `lib/android` directory for already wrapped classes
168 # If found, contains the class identifier and the Nit Module name
169 var find_extern_class: nullable Couple[String, NitModule] is lazy do
170
171 var regex = "extern class [a-zA-Z1-9]\\\+[ ]\\\+in[ ]\\\+\"Java\"[ ]*`\{[ ]*" + self.to_s + "\\\+[ ]*`\}"
172 var nit_dir = "NIT_DIR".environ
173 var grep = new ProcessReader("grep", "-r", regex, nit_dir/"lib/android/", nit_dir/"lib/java/")
174 var to_eat = ["private", "extern", "class"]
175
176 var output = grep.read_line
177
178 var output_class = output.substring_from(output.index_of(':') + 1)
179 var tokens = output_class.split(" ")
180
181 var nclass_name = ""
182
183 for token in tokens do
184 if to_eat.has(token) then continue
185 nclass_name = token
186 break
187 end
188
189 if nclass_name == "" then return null
190
191 var str = output.substring(0, output.search(".nit").from)
192 str = str.substring_from(str.last_index_of('/') + 1)
193 var mod = new NitModule(str)
194
195 return new Couple[String, NitModule](nclass_name, mod)
196 end
197
198 # Comparison based on fully qualified named and generic params
199 # Ignores primitive array so `a.b.c[][] == a.b.c`
200 redef fun ==(other)
201 do
202 if other isa JavaType then
203 return self.repr == other.repr
204 end
205 return false
206 end
207
208 redef fun hash do return self.repr.hash
209
210 private fun repr: String
211 do
212 var id = self.full_id
213
214 if self.has_generic_params then
215 var gen_list = new Array[String]
216
217 for param in generic_params do
218 gen_list.add(param.to_s)
219 end
220
221 id += "<{gen_list.join(", ")}>"
222 end
223
224 return id
225 end
226
227 var collections_list: Array[String] is lazy do return ["List", "ArrayList", "LinkedList", "Vector", "Set", "SortedSet", "HashSet", "TreeSet", "LinkedHashSet", "Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
228 var iterable: Array[String] is lazy do return ["ArrayList", "Set", "HashSet", "LinkedHashSet", "LinkedList", "Stack", "TreeSet", "Vector"]
229 var maps: Array[String] is lazy do return ["Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
230 end
231
232 class NitType
233 var identifier: String
234 var arg_id: String
235 var generic_params: nullable Array[NitType] = null
236
237 # If this NitType was found in `lib/android`, contains the module name to import
238 var mod: nullable NitModule
239
240 # Returns `true` if all types have been successfully converted to Nit type
241 var is_complete: Bool = true
242
243 fun has_generic_params: Bool do return not generic_params == null
244
245 fun id: String do return identifier
246
247 init (id: String)
248 do
249 self.identifier = id
250 end
251
252 init with_generic_params(id: String, gen_params: String...)
253 do
254 self.init(id)
255 self.generic_params = new Array[NitType]
256 for param in gen_params do self.generic_params.add new NitType(param)
257 end
258
259 init with_module(id: String, mod: NitModule)
260 do
261 self.init(id)
262 self.mod = mod
263 end
264
265 redef fun to_s: String
266 do
267 var id = self.identifier
268
269 if self.has_generic_params then
270 var gen_list = new Array[String]
271
272 for param in generic_params do
273 gen_list.add(param.to_s)
274 end
275
276 id += "[{gen_list.join(", ")}]"
277 end
278
279 return id
280 end
281 end
282
283 # Model of a single Java class
284 class JavaClass
285 # Type of this class
286 var class_type = new JavaType(new JavaTypeConverter)
287
288 # Attributes of this class
289 var attributes = new HashMap[String, JavaType]
290
291 # Methods of this class organized by their name
292 var methods = new MultiHashMap[String, JavaMethod]
293
294 # Importations from this class
295 var imports = new HashSet[NitModule]
296 end
297
298 # Model of all the Java class analyzed in one run
299 class JavaModel
300 # Unknown Java types used in `classes`
301 var unknown_types = new HashSet[JavaType]
302
303 # All analyzed classes
304 var classes = new Array[JavaClass]
305 end
306
307 # A Java method, with its signature
308 class JavaMethod
309 # Type returned by the method
310 var return_type: JavaType
311
312 # Type of the arguments of the method
313 var params: Array[JavaType]
314 end
315
316 # A Nit module, use to import the referenced extern classes
317 class NitModule
318 # Name of the module
319 var name: String
320
321 redef fun ==(other): Bool do return self.to_s == other.to_s
322 redef fun to_s: String do return self.name
323 redef fun hash: Int do return self.name.hash
324 end