1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 # Copyright 2015 Alexis Laferrière <alexis.laf@xymus.net>
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # Model of the parsed Java classes and their corresponding Nit types
19 module model
is serialize
21 import more_collections
26 import jtype_converter
31 # Identifiers composing the namespace and class name
33 # An array of all the names that would be separated by `.`.
34 # Each name may contain `$`.
35 var identifier
= new Array[String]
37 var generic_params
: nullable Array[JavaType] = null
39 # Is this a void return type?
42 # Is this type a vararg?
43 var is_vararg
= false is writable
45 # Is this type based on an anonymous class?
46 var is_anonymous
: Bool is lazy
do
47 for id
in identifier
do
48 for part
in id
.split
("$") do
49 if part
.chars
.first
.is_digit
then return true
55 # Has some generic type to be resolved (T extends foo => T is resolved to foo)
56 var has_unresolved_types
= false
58 # Dimension of primitive array: `int[][]` is 2d
59 var array_dimension
= 0
61 fun is_primitive_array
: Bool do return array_dimension
> 0
63 fun has_generic_params
: Bool do return not generic_params
== null
65 fun return_cast
: String do return converter
.cast_as_return
(self.id
)
67 fun param_cast
: String
69 if self.has_generic_params
then
70 return converter
.cast_as_param
(self.generic_params
[0].id
)
73 return converter
.cast_as_param
(self.id
)
76 # Name to give an extern class wrapping this type
77 fun extern_name
: String
80 var prefix
= extern_class_prefix
81 if prefix
== null then
82 # Use the namespace, e.g. java.lang.String -> Java_lang_String
83 assert not identifier
.is_empty
84 if identifier
.length
== 1 then
85 name
= identifier
.last
87 var first
= identifier
.first
88 var last
= identifier
.last
89 var mid
= identifier
.subarray
(1, identifier
.length-2
)
90 name
= first
.simple_capitalized
+ "_"
91 if mid
.not_empty
then name
+= mid
.join
("_") + "_"
95 # Use the prefix and the short class name
96 # e.g. given the prefix Native: java.lang.String -> CString
100 if is_primitive_array
then
101 name
+= "_" + "Array" * array_dimension
104 name
= name
.replace
("-", "_")
105 name
= name
.replace
("$", "_")
109 # Short name of the class, mangled to remove `$` (e.g. `Set`)
110 var id
: String is lazy
, noserialize
do return identifier
.last
.replace
("$", "")
112 # Full name of this class as used in java code (e.g. `java.lang.Set`)
113 var java_full_name
: String is lazy
, noserialize
do return identifier
.join
(".").replace
("$", ".")
115 # Full name of this class as used by jni (e.g. `android.graphics.BitmapFactory$Options`)
116 var jni_full_name
: String is lazy
, noserialize
do return identifier
.join
(".")
118 # Name of this class for the extern declaration in Nit (e.g. `java.lang.Set[]`)
119 var extern_equivalent
: String is lazy
, noserialize
do return jni_full_name
+ "[]" * array_dimension
121 # Full name of this class with arrays and generic values (e.g. `java.lang.Set<E>[]`)
123 var id
= self.java_full_name
125 if self.is_primitive_array
then
126 id
+= "[]" * array_dimension
127 else if self.has_generic_params
then
128 var params
= [for param
in generic_params
do param
.to_s
]
129 id
+= "<{params.join(", ")}>"
135 # Get a copy of `self`
138 var jtype
= new JavaType
139 jtype
.identifier
= identifier
140 jtype
.generic_params
= generic_params
141 jtype
.is_void
= is_void
142 jtype
.is_vararg
= is_vararg
143 jtype
.array_dimension
= array_dimension
147 # Comparison based on fully qualified named
148 redef fun ==(other
) do return other
isa JavaType and
149 self.java_full_name
== other
.java_full_name
and
150 self.array_dimension
== other
.array_dimension
152 redef fun hash
do return self.java_full_name
.hash
157 var identifier
: String
159 # If this NitType was found in `lib/android`, contains the module name to import
160 var mod
: nullable NitModuleRef
162 # Is this type known, wrapped and available in Nit?
163 var is_known
: Bool = true
165 redef fun to_s
do return identifier
168 # Model of a single Java class
171 var class_type
: JavaType
173 # Attributes of this class
174 var attributes
= new HashMap[String, JavaAttribute]
176 # Methods of this class organized by their name
177 var methods
= new MultiHashMap[String, JavaMethod]
179 # Methods signatures introduced by this class
180 var local_intro_methods
= new MultiHashMap[String, JavaMethod]
182 # Constructors of this class
183 var constructors
= new Array[JavaConstructor]
185 # Importations from this class
186 var imports
= new HashSet[NitModuleRef]
188 # Interfaces implemented by this class
189 var implements
= new HashSet[JavaType]
191 # Super classes of this class
192 var extends
= new HashSet[JavaType]
194 # Position of self in `model.class_hierarchy`
195 var in_hierarchy
: nullable POSetElement[JavaClass] = null is noserialize
197 redef fun to_s
do return class_type
.to_s
199 # Resolve the types in `other` in the context of this class
200 private fun resolve_types_of
(other
: JavaClass)
203 for mid
, method
in other
.methods
do
204 for signature
in method
do
205 self.resolve
(signature
.return_type
, signature
.generic_params
)
206 for param
in signature
.params
do self.resolve
(param
, signature
.generic_params
)
211 for signature
in other
.constructors
do
212 for param
in signature
.params
do self.resolve
(param
, signature
.generic_params
)
216 for aid
, attribute
in other
.attributes
do
217 self.resolve attribute
.java_type
221 # Resolve `java_type` in the context of this class
223 # Replace, in place, parameter types by their bound.
224 private fun resolve
(java_type
: JavaType, property_generic_params
: nullable Array[JavaType])
226 # Skip types with a full package name
227 if java_type
.identifier
.length
!= 1 then return
229 # Skip primitive types
230 if converter
.type_map
.keys
.has
(java_type
.id
) then return
232 # Gather the generic parameters of the method, then the class
233 var params
= new Array[JavaType]
234 if property_generic_params
!= null then params
.add_all property_generic_params
235 var class_generic_params
= class_type
.generic_params
236 if class_generic_params
!= null then params
.add_all class_generic_params
238 # Skip if there is not parameters usable to resolve
239 if params
.is_empty
then return
241 for param
in params
do
242 if param
.identifier
== java_type
.identifier
then
243 # Found a marching parameter type
244 # TODO use a more precise bound
245 java_type
.identifier
= ["java", "lang", "Object"]
251 redef fun hash
do return class_type
.hash
252 redef fun ==(o
) do return o
isa JavaClass and o
.class_type
== class_type
255 # Model of all the Java class analyzed in one run
258 # Classes analyzed in this pass
259 var classes
= new HashMap[String, JavaClass]
261 # All classes, from this pass and from other passes
262 var all_classes
: HashMap[String, JavaClass] is noserialize
, lazy
do
263 var classes
= new HashMap[String, JavaClass]
264 classes
.add_all
self.classes
266 for model_path
in sys
.opt_load_models
.value
do
267 if not model_path
.file_exists
then
268 print_error
"Error: model file '{model_path}' does not exist"
272 var file
= model_path
.to_path
.open_ro
273 var d
= new MsgPackDeserializer(file
)
274 var model
= d
.deserialize
277 if d
.errors
.not_empty
then
278 print_error
"Error: failed to deserialize model file '{model_path}' with:\n* {d.errors.join("\n* ")}"
282 if not model
isa JavaModel then
283 print_error
"Error: model file contained a '{if model == null then "null" else model.class_name}'"
287 classes
.add_all model
.classes
293 # Does this model have access to the `java.lang.Object`?
294 var knows_the_object_class
: Bool = all_classes
.keys
.has
("java.lang.Object") is lazy
, noserialize
296 # Add a class in `classes`
297 fun add_class
(jclass
: JavaClass)
299 var key
= jclass
.class_type
.java_full_name
300 classes
[key
] = jclass
303 # Unknown types, not already wrapped and not in this pass
304 var unknown_types
= new HashMap[JavaType, NitType] is noserialize
306 # Wrapped types, or classes analyzed in this pass
307 var known_types
= new HashMap[JavaType, NitType] is noserialize
309 # Get the `NitType` corresponding to the `JavaType`
311 # Also registers types so they can be reused and
312 # to keep track of unknown types.
313 fun java_to_nit_type
(jtype
: JavaType): NitType
316 if known_types
.keys
.has
(jtype
) then return known_types
[jtype
]
317 if unknown_types
.keys
.has
(jtype
) then return unknown_types
[jtype
]
319 # Is it a compatible primitive type?
320 if not jtype
.is_primitive_array
then
321 var name
= converter
.to_nit_type
(jtype
.id
)
323 # We got a Nit equivalent
324 var nit_type
= new NitType(name
)
325 known_types
[jtype
] = nit_type
330 # Is being wrapped in this pass?
331 var key
= jtype
.java_full_name
332 if classes
.keys
.has
(key
) then
333 if jtype
.array_dimension
<= opt_arrays
.value
then
334 var nit_type
= new NitType(jtype
.extern_name
)
335 known_types
[jtype
] = nit_type
341 var nit_type
= find_extern_class
[jtype
.extern_equivalent
]
342 if nit_type
!= null then
343 known_types
[jtype
] = nit_type
348 nit_type
= new NitType(jtype
.extern_name
)
349 nit_type
.is_known
= false
350 unknown_types
[jtype
] = nit_type
354 # Resolve the types in methods and attributes of this class
357 for id
, java_class
in classes
do
358 java_class
.resolve_types_of java_class
360 # Ask nester classes for resolve too
361 var matches
= id
.search_all
("$")
362 for match
in matches
do
363 var nester_name
= id
.substring
(0, match
.from
)
364 if classes
.keys
.has
(nester_name
) then
365 var nester
= classes
[nester_name
]
366 nester
.resolve_types_of java_class
372 # Specialization hierarchy of `classes`
373 var class_hierarchy
= new POSet[JavaClass] is noserialize
375 # Fill `class_hierarchy`
376 fun build_class_hierarchy
378 var object_type
= new JavaType
379 object_type
.identifier
= ["java","lang","Object"]
382 for name
, java_class
in all_classes
do
383 # Skip anonymous classes
384 if java_class
.class_type
.is_anonymous
then continue
386 java_class
.in_hierarchy
= class_hierarchy
.add_node
(java_class
)
388 # Collect explicit super classes
389 var super_classes
= new Array[JavaType]
390 super_classes
.add_all java_class
.implements
391 super_classes
.add_all java_class
.extends
393 # Remove unavailable super classes
394 for super_type
in super_classes
.reverse_iterator
do
395 # Is the super class available?
396 if not all_classes
.keys
.has
(super_type
.java_full_name
) then super_classes
.remove
(super_type
)
399 # If the is no explicit supers, add `java.lang.Object` (if it is known)
400 if super_classes
.is_empty
and java_class
.class_type
!= object_type
and
401 knows_the_object_class
then
402 super_classes
.add object_type
405 for super_type
in super_classes
do
406 # Is the super class available?
407 if not all_classes
.keys
.has
(super_type
.java_full_name
) then continue
409 var super_class
= all_classes
[super_type
.java_full_name
]
410 class_hierarchy
.add_edge
(java_class
, super_class
)
414 # Flatten classes from the top one (Object like)
415 var linearized
= class_hierarchy
.linearize
(class_hierarchy
)
418 for java_class
in linearized
do
419 var greaters
= java_class
.in_hierarchy
.greaters
421 for mid
, signatures
in java_class
.methods
do
422 for signature
in signatures
do
423 if signature
.is_static
then continue
425 # Check if this signature exists in a parent
426 for parent
in greaters
do
427 if parent
== java_class
then continue
429 if not parent
.methods
.keys
.has
(mid
) then continue
431 if parent
.methods
[mid
].has
(signature
) then continue label
434 # This is an introduction! register it
435 java_class
.local_intro_methods
[mid
].add signature
443 # A property to a Java class
444 abstract class JavaProperty
446 # Is this property marked static?
450 # A Java method, with its signature
454 # Type returned by the method
455 var return_type
: JavaType
457 # Type of the arguments of the method
458 var params
: Array[JavaType]
460 # Generic parameters of this method
461 var generic_params
: Array[JavaType]
463 redef fun ==(o
) do return o
isa JavaMethod and o
.is_static
== is_static
and o
.params
== params
464 redef fun hash
do return params
.hash
467 # An attribute in a Java class
471 # Type of the attribute
472 var java_type
: JavaType
475 # A Java method, with its signature
476 class JavaConstructor
477 # Type of the parameters of this constructor
478 var params
: Array[JavaType]
480 # Generic parameters of this constructor
481 var generic_params
: Array[JavaType]
484 # A Nit module, use to import the referenced extern classes
486 # Relative path to the module
490 var name
: String is lazy
, noserialize
do return path
.basename
(".nit")
492 redef fun to_s
do return self.name
493 redef fun ==(other
) do return other
isa NitModuleRef and self.path
== other
.path
494 redef fun hash
do return self.path
.hash
498 # Collection of Java classes already wrapped in the library
500 # * The key uses `JavaType.to_s`.
501 # * The value is the corresponding `NitType`.
502 var find_extern_class
: DefaultMap[String, nullable NitType] is lazy
do
503 var map
= new DefaultMap[String, nullable NitType](null)
504 var modules
= new HashMap[String, NitModuleRef]
506 var lib_paths
= opt_libs
.value
507 if lib_paths
== null then lib_paths
= new Array[String]
509 if lib_paths
.has
("auto") then
510 lib_paths
.remove
"auto"
511 var nit_dir
= "NIT_DIR".environ
512 if nit_dir
.is_empty
then
513 # Simple heuristic to find the Nit lib
514 var dir
= sys
.program_name
.dirname
/ "../../../lib/"
515 dir
= dir
.simplify_path
516 if dir
.file_exists
then lib_paths
.add dir
.simplify_path
520 if lib_paths
.is_empty
then return map
522 # Use grep to find all extern classes implemented in Java
523 var grep_regex
= "extern class [a-zA-Z0-9_]\\\+[ ]\\\+in[ ]\\\+\"Java\
""
524 var grep_args
= ["-r", "--with-filename", grep_regex
]
525 grep_args
.add_all lib_paths
527 var grep
= new ProcessReader("grep", grep_args
...)
528 var lines
= grep
.read_lines
532 # Sort out the modules, Nit class names and Java types
533 var regex
= """(.+):\\s*extern +class +([a-zA-Z0-9_]+) *in *"Java" *`\\{(.+)`\\}""".to_re
535 var matches
= line
.search_all
(regex
)
536 for match
in matches
do
537 var path
= match
[1].to_s
538 var nit_name
= match
[2].to_s
539 var java_name
= match
[3].to_s
.trim
542 # print "+ Found {nit_name}: {java_name} at {path}"
544 var mod
= modules
.get_or_null
(path
)
546 mod
= new NitModuleRef(path
)
550 map
[java_name
] = new NitType(nit_name
, mod
)
557 # Option to set `extern_class_prefix`
558 var opt_extern_class_prefix
= new OptionString("Prefix to extern classes (By default uses the full namespace)", "-p")
560 # Prefix used to name extern classes, if `null` use the full namespace
561 var extern_class_prefix
: nullable String is lazy
, noserialize
do return opt_extern_class_prefix
.value
563 # Libraries to search for existing wrappers
564 var opt_libs
= new OptionArray("Paths to libraries with wrappers of Java classes ('auto' to use the full Nit lib)", "-i")
566 # Generate the primitive array version of each class up to the given depth
567 var opt_arrays
= new OptionInt("Depth of the primitive array for each wrapped class (default: 1)", 1, "-a")
569 # Generate the primitive array version of each class up to the given depth
570 var opt_load_models
= new OptionArray("Saved models to search for super-classes", "-m")
573 redef abstract class Text
574 # Get a copy of `self` where the first letter is capitalized
575 fun simple_capitalized
: String
577 if is_empty
then return to_s
579 var c
= chars
.first
.to_upper
580 var s
= c
.to_s
+ substring_from
(1)