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