contrib/jwrapper: collect super-classes data
[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 import opts
23
24 import jtype_converter
25
26 class JavaType
27 super Cloneable
28
29 # Identifiers composing the namespace and class name
30 #
31 # An array of all the names that would be separated by `.`.
32 # Each name may contain `$`.
33 var identifier = new Array[String]
34
35 var generic_params: nullable Array[JavaType] = null
36
37 # Is this a void return type?
38 var is_void = false
39
40 # Is this type a vararg?
41 var is_vararg = false is writable
42
43 # Is this type based on an anonymous class?
44 var is_anonymous: Bool is lazy do
45 for id in identifier do
46 for part in id.split("$") do
47 if part.chars.first.is_digit then return true
48 end
49 end
50 return false
51 end
52
53 # Has some generic type to be resolved (T extends foo => T is resolved to foo)
54 var has_unresolved_types = false
55
56 # Dimension of primitive array: `int[][]` is 2d
57 var array_dimension = 0
58
59 fun is_primitive_array: Bool do return array_dimension > 0
60
61 fun has_generic_params: Bool do return not generic_params == null
62
63 fun return_cast: String do return converter.cast_as_return(self.id)
64
65 fun param_cast: String
66 do
67 if self.has_generic_params then
68 return converter.cast_as_param(self.generic_params[0].id)
69 end
70
71 return converter.cast_as_param(self.id)
72 end
73
74 # Name to give an extern class wrapping this type
75 fun extern_name: String
76 do
77 var name
78 var prefix = extern_class_prefix
79 if prefix == null then
80 # Use the namespace, e.g. java.lang.String -> Java_lang_String
81 assert not identifier.is_empty
82 if identifier.length == 1 then
83 name = identifier.last
84 else
85 var first = identifier.first
86 var last = identifier.last
87 var mid = identifier.subarray(1, identifier.length-2)
88 name = first.simple_capitalized + "_"
89 if mid.not_empty then name += mid.join("_") + "_"
90 name += last
91 end
92 else
93 # Use the prefix and the short class name
94 # e.g. given the prefix Native: java.lang.String -> NativeString
95 name = prefix + id
96 end
97
98 if is_primitive_array then
99 name += "_" + "Array" * array_dimension
100 end
101
102 name = name.replace("-", "_")
103 name = name.replace("$", "_")
104 return name
105 end
106
107 # Short name of the class, mangled to remove `$` (e.g. `Set`)
108 fun id: String do return identifier.last.replace("$", "")
109
110 # Full name of this class as used in an importation (e.g. `java.lang.Set`)
111 fun package_name: String do return identifier.join(".")
112
113 # Name of this class for the extern declaration in Nit (e.g. `java.lang.Set[]`)
114 fun extern_equivalent: String do return package_name + "[]" * array_dimension
115
116 # Full name of this class with arrays and generic values (e.g. `java.lang.Set<E>[]`)
117 redef fun to_s do
118 var id = self.package_name
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 # Get a copy of `self`
131 redef fun clone
132 do
133 var jtype = new JavaType
134 jtype.identifier = identifier
135 jtype.generic_params = generic_params
136 jtype.is_void = is_void
137 jtype.is_vararg = is_vararg
138 jtype.array_dimension = array_dimension
139 return jtype
140 end
141
142 # Comparison based on fully qualified named
143 redef fun ==(other) do return other isa JavaType and
144 self.package_name == other.package_name and
145 self.array_dimension == other.array_dimension
146
147 redef fun hash do return self.package_name.hash
148 end
149
150 class NitType
151 # Nit class name
152 var identifier: String
153
154 # If this NitType was found in `lib/android`, contains the module name to import
155 var mod: nullable NitModule
156
157 # Is this type known, wrapped and available in Nit?
158 var is_known: Bool = true
159
160 redef fun to_s do return identifier
161 end
162
163 # Model of a single Java class
164 class JavaClass
165 # Type of this class
166 var class_type: JavaType
167
168 # Attributes of this class
169 var attributes = new HashMap[String, JavaAttribute]
170
171 # Methods of this class organized by their name
172 var methods = new MultiHashMap[String, JavaMethod]
173
174 # Constructors of this class
175 var constructors = new Array[JavaConstructor]
176
177 # Importations from this class
178 var imports = new HashSet[NitModule]
179
180 # Interfaces implemented by this class
181 var implements = new HashSet[JavaType]
182
183 # Super classes of this class
184 var extends = new HashSet[JavaType]
185
186 redef fun to_s do return class_type.to_s
187
188 # Resolve the types in `other` in the context of this class
189 private fun resolve_types_of(other: JavaClass)
190 do
191 # Methods
192 for mid, method in other.methods do
193 for signature in method do
194 self.resolve(signature.return_type, signature.generic_params)
195 for param in signature.params do self.resolve(param, signature.generic_params)
196 end
197 end
198
199 # Constructors
200 for signature in other.constructors do
201 for param in signature.params do self.resolve(param, signature.generic_params)
202 end
203
204 # Attributes
205 for aid, attribute in other.attributes do
206 self.resolve attribute.java_type
207 end
208 end
209
210 # Resolve `java_type` in the context of this class
211 #
212 # Replace, in place, parameter types by their bound.
213 private fun resolve(java_type: JavaType, property_generic_params: nullable Array[JavaType])
214 do
215 # Skip types with a full package name
216 if java_type.identifier.length != 1 then return
217
218 # Skip primitive types
219 if converter.type_map.keys.has(java_type.id) then return
220
221 # Gather the generic parameters of the method, then the class
222 var params = new Array[JavaType]
223 if property_generic_params != null then params.add_all property_generic_params
224 var class_generic_params = class_type.generic_params
225 if class_generic_params != null then params.add_all class_generic_params
226
227 # Skip if there is not parameters usable to resolve
228 if params.is_empty then return
229
230 for param in params do
231 if param.identifier == java_type.identifier then
232 # Found a marching parameter type
233 # TODO use a more precise bound
234 java_type.identifier = ["java", "lang", "Object"]
235 return
236 end
237 end
238 end
239
240 redef fun hash do return class_type.hash
241 redef fun ==(o) do return o isa JavaClass and o.class_type == class_type
242 end
243
244 # Model of all the Java class analyzed in one run
245 class JavaModel
246
247 # All analyzed classes
248 var classes = new HashMap[String, JavaClass]
249
250 # Add a class in `classes`
251 fun add_class(jclass: JavaClass)
252 do
253 var key = jclass.class_type.package_name
254 classes[key] = jclass
255 end
256
257 # Unknown types, not already wrapped and not in this pass
258 var unknown_types = new HashMap[JavaType, NitType]
259
260 # Wrapped types, or classes analyzed in this pass
261 var known_types = new HashMap[JavaType, NitType]
262
263 # Get the `NitType` corresponding to the `JavaType`
264 #
265 # Also registers types so they can be reused and
266 # to keep track of unknown types.
267 fun java_to_nit_type(jtype: JavaType): NitType
268 do
269 # Check cache
270 if known_types.keys.has(jtype) then return known_types[jtype]
271 if unknown_types.keys.has(jtype) then return unknown_types[jtype]
272
273 # Is it a compatible primitive type?
274 if not jtype.is_primitive_array then
275 var name = converter.to_nit_type(jtype.id)
276 if name != null then
277 # We got a Nit equivalent
278 var nit_type = new NitType(name)
279 known_types[jtype] = nit_type
280 return nit_type
281 end
282 end
283
284 # Is being wrapped in this pass?
285 var key = jtype.package_name
286 if classes.keys.has(key) then
287 if jtype.array_dimension <= opt_arrays.value then
288 var nit_type = new NitType(jtype.extern_name)
289 known_types[jtype] = nit_type
290 return nit_type
291 end
292 end
293
294 # Search in lib
295 var nit_type = find_extern_class[jtype.extern_equivalent]
296 if nit_type != null then
297 known_types[jtype] = nit_type
298 return nit_type
299 end
300
301 # Unknown type
302 nit_type = new NitType(jtype.extern_name)
303 nit_type.is_known = false
304 unknown_types[jtype] = nit_type
305 return nit_type
306 end
307
308 # Resolve the types in methods and attributes of this class
309 fun resolve_types
310 do
311 for id, java_class in classes do
312 java_class.resolve_types_of java_class
313
314 # Ask nester classes for resolve too
315 var matches = id.search_all("$")
316 for match in matches do
317 var nester_name = id.substring(0, match.from)
318 if classes.keys.has(nester_name) then
319 var nester = classes[nester_name]
320 nester.resolve_types_of java_class
321 end
322 end
323 end
324 end
325 end
326
327 # A property to a Java class
328 abstract class JavaProperty
329
330 # Is this property marked static?
331 var is_static: Bool
332 end
333
334 # A Java method, with its signature
335 class JavaMethod
336 super JavaProperty
337
338 # Type returned by the method
339 var return_type: JavaType
340
341 # Type of the arguments of the method
342 var params: Array[JavaType]
343
344 # Generic parameters of this method
345 var generic_params: Array[JavaType]
346
347 redef fun ==(o) do return o isa JavaMethod and o.is_static == is_static and o.params == params
348 redef fun hash do return params.hash
349 end
350
351 # An attribute in a Java class
352 class JavaAttribute
353 super JavaProperty
354
355 # Type of the attribute
356 var java_type: JavaType
357 end
358
359 # A Java method, with its signature
360 class JavaConstructor
361 # Type of the parameters of this constructor
362 var params: Array[JavaType]
363
364 # Generic parameters of this constructor
365 var generic_params: Array[JavaType]
366 end
367
368 # A Nit module, use to import the referenced extern classes
369 class NitModule
370 # Relative path to the module
371 var path: String
372
373 # Name of the module
374 var name: String is lazy do return path.basename(".nit")
375
376 redef fun to_s do return self.name
377 redef fun ==(other) do return other isa NitModule and self.path == other.path
378 redef fun hash do return self.path.hash
379 end
380
381 redef class Sys
382 # Collection of Java classes already wrapped in the library
383 #
384 # * The key uses `JavaType.to_s`.
385 # * The value is the corresponding `NitType`.
386 var find_extern_class: DefaultMap[String, nullable NitType] is lazy do
387 var map = new DefaultMap[String, nullable NitType](null)
388 var modules = new HashMap[String, NitModule]
389
390 var lib_paths = opt_libs.value
391 if lib_paths == null then lib_paths = new Array[String]
392
393 if lib_paths.has("auto") then
394 lib_paths.remove "auto"
395 var nit_dir = "NIT_DIR".environ
396 if nit_dir.is_empty then
397 # Simple heuristic to find the Nit lib
398 var dir = sys.program_name.dirname / "../../../lib/"
399 dir = dir.simplify_path
400 if dir.file_exists then lib_paths.add dir.simplify_path
401 end
402 end
403
404 if lib_paths.is_empty then return map
405
406 # Use grep to find all extern classes implemented in Java
407 var grep_regex = "extern class [a-zA-Z0-9_]\\\+[ ]\\\+in[ ]\\\+\"Java\""
408 var grep_args = ["-r", "--with-filename", grep_regex]
409 grep_args.add_all lib_paths
410
411 var grep = new ProcessReader("grep", grep_args...)
412 var lines = grep.read_lines
413 grep.close
414 grep.wait
415
416 # Sort out the modules, Nit class names and Java types
417 var regex = """(.+):\\s*extern +class +([a-zA-Z0-9_]+) *in *"Java" *`\\{(.+)`\\}""".to_re
418 for line in lines do
419 var matches = line.search_all(regex)
420 for match in matches do
421 var path = match[1].to_s
422 var nit_name = match[2].to_s
423 var java_name = match[3].to_s.trim
424
425 # Debug code
426 # print "+ Found {nit_name}: {java_name} at {path}"
427
428 var mod = modules.get_or_null(path)
429 if mod == null then
430 mod = new NitModule(path)
431 modules[path] = mod
432 end
433
434 map[java_name] = new NitType(nit_name, mod)
435 end
436 end
437
438 return map
439 end
440
441 # Option to set `extern_class_prefix`
442 var opt_extern_class_prefix = new OptionString("Prefix to extern classes (By default uses the full namespace)", "-p")
443
444 # Prefix used to name extern classes, if `null` use the full namespace
445 var extern_class_prefix: nullable String is lazy do return opt_extern_class_prefix.value
446
447 # Libraries to search for existing wrappers
448 var opt_libs = new OptionArray("Paths to libraries with wrappers of Java classes ('auto' to use the full Nit lib)", "-i")
449
450 # Generate the primitive array version of each class up to the given depth
451 var opt_arrays = new OptionInt("Depth of the primitive array for each wrapped class (default: 1)", 1, "-a")
452 end
453
454 redef class Text
455 # Get a copy of `self` where the first letter is capitalized
456 fun simple_capitalized: String
457 do
458 if is_empty then return to_s
459
460 var c = chars.first.to_upper
461 var s = c.to_s + substring_from(1)
462 return s
463 end
464 end