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 # Contains the java and nit type representation used to convert java to nit code
21 import more_collections
24 import jtype_converter
29 # Identifiers composing the namespace and class name
31 # An array of all the names that would be separated by `.`.
32 # Each name may contain `$`.
33 var identifier
= new Array[String]
35 var generic_params
: nullable Array[JavaType] = null
37 # Is this a void return type?
40 # Is this type a vararg?
41 var is_vararg
= false is writable
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
53 # Has some generic type to be resolved (T extends foo => T is resolved to foo)
54 var has_unresolved_types
= false
56 # Dimension of primitive array: `int[][]` is 2d
57 var array_dimension
= 0
59 fun is_primitive_array
: Bool do return array_dimension
> 0
61 fun has_generic_params
: Bool do return not generic_params
== null
63 fun return_cast
: String do return converter
.cast_as_return
(self.id
)
65 fun param_cast
: String
67 if self.has_generic_params
then
68 return converter
.cast_as_param
(self.generic_params
[0].id
)
71 return converter
.cast_as_param
(self.id
)
74 # Name to give an extern class wrapping this type
75 fun extern_name
: String
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
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
("_") + "_"
93 # Use the prefix and the short class name
94 # e.g. given the prefix Native: java.lang.String -> NativeString
98 if is_primitive_array
then
99 name
+= "_" + "Array" * array_dimension
102 name
= name
.replace
("-", "_")
103 name
= name
.replace
("$", "_")
107 # Short name of the class, mangled to remove `$` (e.g. `Set`)
108 fun id
: String do return identifier
.last
.replace
("$", "")
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
(".")
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
116 # Full name of this class with arrays and generic values (e.g. `java.lang.Set<E>[]`)
118 var id
= self.package_name
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(", ")}>"
130 # Get a copy of `self`
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
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
147 redef fun hash
do return self.package_name
.hash
152 var identifier
: String
154 # If this NitType was found in `lib/android`, contains the module name to import
155 var mod
: nullable NitModule
157 # Is this type known, wrapped and available in Nit?
158 var is_known
: Bool = true
160 redef fun to_s
do return identifier
163 # Model of a single Java class
166 var class_type
: JavaType
168 # Attributes of this class
169 var attributes
= new HashMap[String, JavaAttribute]
171 # Methods of this class organized by their name
172 var methods
= new MultiHashMap[String, JavaMethod]
174 # Constructors of this class
175 var constructors
= new Array[JavaConstructor]
177 # Importations from this class
178 var imports
= new HashSet[NitModule]
180 # Interfaces implemented by this class
181 var implements
= new HashSet[JavaType]
183 # Super classes of this class
184 var extends
= new HashSet[JavaType]
186 redef fun to_s
do return class_type
.to_s
188 # Resolve the types in `other` in the context of this class
189 private fun resolve_types_of
(other
: JavaClass)
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
)
200 for signature
in other
.constructors
do
201 for param
in signature
.params
do self.resolve
(param
, signature
.generic_params
)
205 for aid
, attribute
in other
.attributes
do
206 self.resolve attribute
.java_type
210 # Resolve `java_type` in the context of this class
212 # Replace, in place, parameter types by their bound.
213 private fun resolve
(java_type
: JavaType, property_generic_params
: nullable Array[JavaType])
215 # Skip types with a full package name
216 if java_type
.identifier
.length
!= 1 then return
218 # Skip primitive types
219 if converter
.type_map
.keys
.has
(java_type
.id
) then return
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
227 # Skip if there is not parameters usable to resolve
228 if params
.is_empty
then return
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"]
240 redef fun hash
do return class_type
.hash
241 redef fun ==(o
) do return o
isa JavaClass and o
.class_type
== class_type
244 # Model of all the Java class analyzed in one run
247 # All analyzed classes
248 var classes
= new HashMap[String, JavaClass]
250 # Add a class in `classes`
251 fun add_class
(jclass
: JavaClass)
253 var key
= jclass
.class_type
.package_name
254 classes
[key
] = jclass
257 # Unknown types, not already wrapped and not in this pass
258 var unknown_types
= new HashMap[JavaType, NitType]
260 # Wrapped types, or classes analyzed in this pass
261 var known_types
= new HashMap[JavaType, NitType]
263 # Get the `NitType` corresponding to the `JavaType`
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
270 if known_types
.keys
.has
(jtype
) then return known_types
[jtype
]
271 if unknown_types
.keys
.has
(jtype
) then return unknown_types
[jtype
]
273 # Is it a compatible primitive type?
274 if not jtype
.is_primitive_array
then
275 var name
= converter
.to_nit_type
(jtype
.id
)
277 # We got a Nit equivalent
278 var nit_type
= new NitType(name
)
279 known_types
[jtype
] = nit_type
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
295 var nit_type
= find_extern_class
[jtype
.extern_equivalent
]
296 if nit_type
!= null then
297 known_types
[jtype
] = nit_type
302 nit_type
= new NitType(jtype
.extern_name
)
303 nit_type
.is_known
= false
304 unknown_types
[jtype
] = nit_type
308 # Resolve the types in methods and attributes of this class
311 for id
, java_class
in classes
do
312 java_class
.resolve_types_of java_class
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
327 # A property to a Java class
328 abstract class JavaProperty
330 # Is this property marked static?
334 # A Java method, with its signature
338 # Type returned by the method
339 var return_type
: JavaType
341 # Type of the arguments of the method
342 var params
: Array[JavaType]
344 # Generic parameters of this method
345 var generic_params
: Array[JavaType]
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
351 # An attribute in a Java class
355 # Type of the attribute
356 var java_type
: JavaType
359 # A Java method, with its signature
360 class JavaConstructor
361 # Type of the parameters of this constructor
362 var params
: Array[JavaType]
364 # Generic parameters of this constructor
365 var generic_params
: Array[JavaType]
368 # A Nit module, use to import the referenced extern classes
370 # Relative path to the module
374 var name
: String is lazy
do return path
.basename
(".nit")
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
382 # Collection of Java classes already wrapped in the library
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]
390 var lib_paths
= opt_libs
.value
391 if lib_paths
== null then lib_paths
= new Array[String]
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
404 if lib_paths
.is_empty
then return map
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
411 var grep
= new ProcessReader("grep", grep_args
...)
412 var lines
= grep
.read_lines
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
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
426 # print "+ Found {nit_name}: {java_name} at {path}"
428 var mod
= modules
.get_or_null
(path
)
430 mod
= new NitModule(path
)
434 map
[java_name
] = new NitType(nit_name
, mod
)
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")
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
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")
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")
455 # Get a copy of `self` where the first letter is capitalized
456 fun simple_capitalized
: String
458 if is_empty
then return to_s
460 var c
= chars
.first
.to_upper
461 var s
= c
.to_s
+ substring_from
(1)