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 # Has some generic type to be resolved (T extends foo => T is resolved to foo)
44 var has_unresolved_types
= false
46 # Dimension of primitive array: `int[][]` is 2d
47 var array_dimension
= 0
49 fun is_primitive_array
: Bool do return array_dimension
> 0
51 fun has_generic_params
: Bool do return not generic_params
== null
53 fun return_cast
: String do return converter
.cast_as_return
(self.id
)
55 fun param_cast
: String
57 if self.has_generic_params
then
58 return converter
.cast_as_param
(self.generic_params
[0].id
)
61 return converter
.cast_as_param
(self.id
)
64 # Name to give an extern class wrapping this type
65 fun extern_name
: String
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
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
("_") + "_"
83 # Use the prefix and the short class name
84 # e.g. given the prefix Native: java.lang.String -> NativeString
88 if is_primitive_array
then
89 name
+= "_" + "Array" * array_dimension
92 name
= name
.replace
("-", "_")
93 name
= name
.replace
("$", "_")
97 # Short name of the class, mangled to remove `$` (e.g. `Set`)
98 fun id
: String do return identifier
.last
.replace
("$", "")
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
(".")
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
106 # Full name of this class with arrays and generic values (e.g. `java.lang.Set<E>[]`)
108 var id
= self.package_name
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(", ")}>"
120 # Get a copy of `self`
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
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
137 redef fun hash
do return self.package_name
.hash
142 var identifier
: String
144 # If this NitType was found in `lib/android`, contains the module name to import
145 var mod
: nullable NitModule
147 # Is this type known, wrapped and available in Nit?
148 var is_known
: Bool = true
150 redef fun to_s
do return identifier
153 # Model of a single Java class
156 var class_type
: JavaType
158 # Attributes of this class
159 var attributes
= new HashMap[String, JavaAttribute]
161 # Methods of this class organized by their name
162 var methods
= new MultiHashMap[String, JavaMethod]
164 # Constructors of this class
165 var constructors
= new Array[JavaConstructor]
167 # Importations from this class
168 var imports
= new HashSet[NitModule]
170 redef fun to_s
do return class_type
.to_s
172 # Resolve the types in `other` in the context of this class
173 private fun resolve_types_of
(other
: JavaClass)
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
)
184 for signature
in other
.constructors
do
185 for param
in signature
.params
do self.resolve
(param
, signature
.generic_params
)
189 for aid
, attribute
in other
.attributes
do
190 self.resolve attribute
.java_type
194 # Resolve `java_type` in the context of this class
196 # Replace, in place, parameter types by their bound.
197 private fun resolve
(java_type
: JavaType, property_generic_params
: nullable Array[JavaType])
199 # Skip types with a full package name
200 if java_type
.identifier
.length
!= 1 then return
202 # Skip primitive types
203 if converter
.type_map
.keys
.has
(java_type
.id
) then return
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
211 # Skip if there is not parameters usable to resolve
212 if params
.is_empty
then return
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"]
225 # Model of all the Java class analyzed in one run
228 # All analyzed classes
229 var classes
= new HashMap[String, JavaClass]
231 # Add a class in `classes`
232 fun add_class
(jclass
: JavaClass)
234 var key
= jclass
.class_type
.package_name
235 classes
[key
] = jclass
238 # Unknown types, not already wrapped and not in this pass
239 var unknown_types
= new HashMap[JavaType, NitType]
241 # Wrapped types, or classes analyzed in this pass
242 var known_types
= new HashMap[JavaType, NitType]
244 # Get the `NitType` corresponding to the `JavaType`
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
251 if known_types
.keys
.has
(jtype
) then return known_types
[jtype
]
252 if unknown_types
.keys
.has
(jtype
) then return unknown_types
[jtype
]
254 # Is it a compatible primitive type?
255 if not jtype
.is_primitive_array
then
256 var name
= converter
.to_nit_type
(jtype
.id
)
258 # We got a Nit equivalent
259 var nit_type
= new NitType(name
)
260 known_types
[jtype
] = nit_type
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
276 var nit_type
= find_extern_class
[jtype
.extern_equivalent
]
277 if nit_type
!= null then
278 known_types
[jtype
] = nit_type
283 nit_type
= new NitType(jtype
.extern_name
)
284 nit_type
.is_known
= false
285 unknown_types
[jtype
] = nit_type
289 # Resolve the types in methods and attributes of this class
292 for id
, java_class
in classes
do
293 java_class
.resolve_types_of java_class
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
308 # A property to a Java class
309 abstract class JavaProperty
311 # Is this property marked static?
315 # A Java method, with its signature
319 # Type returned by the method
320 var return_type
: JavaType
322 # Type of the arguments of the method
323 var params
: Array[JavaType]
325 # Generic parameters of this method
326 var generic_params
: Array[JavaType]
329 # An attribute in a Java class
333 # Type of the attribute
334 var java_type
: JavaType
337 # A Java method, with its signature
338 class JavaConstructor
339 # Type of the parameters of this constructor
340 var params
: Array[JavaType]
342 # Generic parameters of this constructor
343 var generic_params
: Array[JavaType]
346 # A Nit module, use to import the referenced extern classes
348 # Relative path to the module
352 var name
: String is lazy
do return path
.basename
(".nit")
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
360 # Collection of Java classes already wrapped in the library
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]
368 var lib_paths
= opt_libs
.value
369 if lib_paths
== null then lib_paths
= new Array[String]
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
382 if lib_paths
.is_empty
then return map
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
389 var grep
= new ProcessReader("grep", grep_args
...)
390 var lines
= grep
.read_lines
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
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
404 # print "+ Found {nit_name}: {java_name} at {path}"
406 var mod
= modules
.get_or_null
(path
)
408 mod
= new NitModule(path
)
412 map
[java_name
] = new NitType(nit_name
, mod
)
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")
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
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")
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")
433 # Get a copy of `self` where the first letter is capitalized
434 fun simple_capitalized
: String
436 if is_empty
then return to_s
438 var c
= chars
.first
.to_upper
439 var s
= c
.to_s
+ substring_from
(1)