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
27 var identifier
= new Array[String]
28 var generic_params
: nullable Array[JavaType] = null
30 # Is this a void return type?
33 # Is this type a vararg?
34 var is_vararg
= false is writable
36 # Has some generic type to be resolved (T extends foo => T is resolved to foo)
37 var has_unresolved_types
= false
39 # Dimension of primitive array: `int[][]` is 2d
40 var array_dimension
= 0
42 fun is_primitive_array
: Bool do return array_dimension
> 0
44 fun has_generic_params
: Bool do return not generic_params
== null
45 fun full_id
: String do return identifier
.join
(".")
46 fun id
: String do return identifier
.last
.replace
("$", "")
48 fun return_cast
: String do return converter
.cast_as_return
(self.id
)
50 fun param_cast
: String
52 if self.has_generic_params
then
53 return converter
.cast_as_param
(self.generic_params
[0].id
)
56 return converter
.cast_as_param
(self.id
)
59 # Name to give an extern class wrapping this type
60 fun extern_name
: String
63 var prefix
= extern_class_prefix
64 if prefix
== null then
65 # Use the namespace, e.g. java.lang.String -> Java_lang_String
66 assert not identifier
.is_empty
67 if identifier
.length
== 1 then
68 name
= identifier
.last
70 var first
= identifier
.first
71 var last
= identifier
.last
72 var mid
= identifier
.subarray
(1, identifier
.length-2
)
73 name
= first
.simple_capitalized
+ "_"
74 if mid
.not_empty
then name
+= mid
.join
("_") + "_"
78 # Use the prefix and the short class name
79 # e.g. given the prefix Native: java.lang.String -> NativeString
83 name
= name
.replace
("-", "_")
84 name
= name
.replace
("$", "_")
92 if self.is_primitive_array
then
93 id
+= "[]" * array_dimension
94 else if self.has_generic_params
then
95 var params
= [for param
in generic_params
do param
.to_s
]
96 id
+= "<{params.join(", ")}>"
102 # To fully qualified package name
103 # Cuts the primitive array `[]`
104 fun to_package_name
: String
109 return str
.substring
(0, len
- (2*array_dimension
))
112 fun resolve_types
(conversion_map
: HashMap[String, Array[String]])
114 if identifier
.length
== 1 then
115 var resolved_id
= conversion_map
.get_or_null
(self.id
)
116 if resolved_id
!= null then self.identifier
= new Array[String].from
(resolved_id
)
119 if self.has_generic_params
then
120 for params
in generic_params
do params
.resolve_types
(conversion_map
)
124 # Comparison based on fully qualified named
125 redef fun ==(other
) do return other
isa JavaType and
126 self.full_id
== other
.full_id
and
127 self.is_primitive_array
== other
.is_primitive_array
129 redef fun hash
do return self.full_id
.hash
134 var identifier
: String
136 # If this NitType was found in `lib/android`, contains the module name to import
137 var mod
: nullable NitModule
139 # Is this type known, wrapped and available in Nit?
140 var is_known
: Bool = true
142 redef fun to_s
do return identifier
145 # Model of a single Java class
148 var class_type
: JavaType
150 # Attributes of this class
151 var attributes
= new HashMap[String, JavaType]
153 # Methods of this class organized by their name
154 var methods
= new MultiHashMap[String, JavaMethod]
156 # Constructors of this class
157 var constructors
= new Array[JavaConstructor]
159 # Importations from this class
160 var imports
= new HashSet[NitModule]
162 redef fun to_s
do return class_type
.to_s
165 # Model of all the Java class analyzed in one run
168 # All analyzed classes
169 var classes
= new HashMap[String, JavaClass]
171 # Add a class in `classes`
172 fun add_class
(jclass
: JavaClass)
174 var key
= jclass
.class_type
.full_id
175 classes
[key
] = jclass
178 # Unknown types, not already wrapped and not in this pass
179 private var unknown_types
= new HashMap[JavaType, NitType]
181 # Wrapped types, or classes analyzed in this pass
182 private var known_types
= new HashMap[JavaType, NitType]
184 # Get the `NitType` corresponding to the `JavaType`
186 # Also registers types so they can be reused and
187 # to keep track of unknown types.
188 fun java_to_nit_type
(jtype
: JavaType): NitType
191 if known_types
.keys
.has
(jtype
) then return known_types
[jtype
]
192 if unknown_types
.keys
.has
(jtype
) then return unknown_types
[jtype
]
194 # Is it a compatible primitive type?
195 if not jtype
.is_primitive_array
then
196 var name
= converter
.to_nit_type
(jtype
.id
)
198 # We got a Nit equivalent
199 var nit_type
= new NitType(name
)
200 known_types
[jtype
] = nit_type
205 # Is being wrapped in this pass?
206 var key
= jtype
.full_id
207 if classes
.keys
.has
(key
) then
208 var nit_type
= new NitType(jtype
.extern_name
)
209 known_types
[jtype
] = nit_type
215 var nit_type
= find_extern_class
[jtype
.full_id
]
216 if nit_type
!= null then
217 known_types
[jtype
] = nit_type
222 nit_type
= new NitType(jtype
.extern_name
)
223 nit_type
.is_known
= false
224 unknown_types
[jtype
] = nit_type
229 # A property to a Java class
230 abstract class JavaProperty
232 # Is this property marked static?
236 # A Java method, with its signature
240 # Type returned by the method
241 var return_type
: JavaType
243 # Type of the arguments of the method
244 var params
: Array[JavaType]
247 # A Java method, with its signature
248 class JavaConstructor
249 # Type of the parameters of this constructor
250 var params
: Array[JavaType]
253 # A Nit module, use to import the referenced extern classes
255 # Relative path to the module
259 var name
: String is lazy
do return path
.basename
(".nit")
261 redef fun to_s
do return self.name
262 redef fun ==(other
) do return other
isa NitModule and self.path
== other
.path
263 redef fun hash
do return self.path
.hash
267 # Collection of Java classes already wrapped in the library
269 # * The key is from `JavaType.full_id`.
270 # * The value is the corresponding `NitType`.
271 var find_extern_class
: DefaultMap[String, nullable NitType] is lazy
do
272 var map
= new DefaultMap[String, nullable NitType](null)
273 var modules
= new HashMap[String, NitModule]
275 var lib_paths
= opt_libs
.value
276 if lib_paths
== null then lib_paths
= new Array[String]
278 if lib_paths
.has
("auto") then
279 lib_paths
.remove
"auto"
280 var nit_dir
= "NIT_DIR".environ
281 if nit_dir
.is_empty
then
282 # Simple heuristic to find the Nit lib
283 var dir
= sys
.program_name
.dirname
/ "../../../lib/"
284 dir
= dir
.simplify_path
285 if dir
.file_exists
then lib_paths
.add dir
.simplify_path
289 if lib_paths
.is_empty
then return map
291 # Use grep to find all extern classes implemented in Java
292 var grep_regex
= "extern class [a-zA-Z0-9_]\\\+[ ]\\\+in[ ]\\\+\"Java\
""
293 var grep_args
= ["-r", "--with-filename", grep_regex
]
294 grep_args
.add_all lib_paths
296 var grep
= new ProcessReader("grep", grep_args
...)
297 var lines
= grep
.read_lines
301 # Sort out the modules, Nit class names and Java types
302 var regex
= """(.+):\\s*extern +class +([a-zA-Z0-9_]+) *in *"Java" *`\\{ *([a-zA-Z0-9.$/]+) *`\\}""".to_re
304 var matches
= line
.search_all
(regex
)
305 for match
in matches
do
306 var path
= match
[1].to_s
307 var nit_name
= match
[2].to_s
308 var java_name
= match
[3].to_s
311 # print "+ Found {nit_name}:{java_name} at {path}"
313 var mod
= modules
.get_or_null
(path
)
315 mod
= new NitModule(path
)
319 map
[java_name
] = new NitType(nit_name
, mod
)
326 # Option to set `extern_class_prefix`
327 var opt_extern_class_prefix
= new OptionString("Prefix to extern classes (By default uses the full namespace)", "-p")
329 # Prefix used to name extern classes, if `null` use the full namespace
330 var extern_class_prefix
: nullable String is lazy
do return opt_extern_class_prefix
.value
332 # Libraries to search for existing wrappers
333 var opt_libs
= new OptionArray("Paths to libraries with wrappers of Java classes ('auto' to use the full Nit lib)", "-i")
337 # Get a copy of `self` where the first letter is capitalized
338 fun simple_capitalized
: String
340 if is_empty
then return to_s
342 var c
= chars
.first
.to_upper
343 var s
= c
.to_s
+ substring_from
(1)