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