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