1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Contains the java and nit type representation used to convert java to nit code
20 import jtype_converter
23 private var converter
: JavaTypeConverter
24 var identifier
= new Array[String]
25 var generic_params
: nullable Array[JavaType] = null
28 # Has some generic type to be resolved (T extends foo => T is resolved to foo)
29 var has_unresolved_types
= false
31 # Dimension of primitive array: `int[][]` is 2d
32 var array_dimension
= 0
34 fun is_primitive_array
: Bool do return array_dimension
> 0
36 fun has_generic_params
: Bool do return not generic_params
== null
37 fun full_id
: String do return identifier
.join
(".")
38 fun id
: String do return identifier
.last
.replace
("$", "")
40 init(converter
: JavaTypeConverter) do self.converter
= converter
42 fun return_cast
: String do return converter
.cast_as_return
(self.id
)
44 fun param_cast
: String
46 if self.has_generic_params
then
47 return converter
.cast_as_param
(self.generic_params
[0].id
)
50 return converter
.cast_as_param
(self.id
)
53 fun to_nit_type
: NitType
58 if not is_primitive_array
then
59 type_id
= converter
.to_nit_type
(self.id
)
62 if type_id
== null then
63 nit_type
= self.extern_name
64 nit_type
.is_complete
= false
66 nit_type
= new NitType(type_id
)
69 if not self.has_generic_params
then return nit_type
71 nit_type
.generic_params
= new Array[NitType]
73 for param
in generic_params
do
74 var nit_param
= param
.to_nit_type
76 nit_type
.generic_params
.add
(nit_param
)
78 if not nit_param
.is_complete
then nit_type
.is_complete
= false
84 fun is_iterable
: Bool do return iterable
.has
(self.id
)
86 fun is_collection
: Bool do return is_primitive_array
or collections_list
.has
(self.id
)
88 fun is_map
: Bool do return maps
.has
(self.id
)
90 fun is_wrapped
: Bool do return find_extern_class
!= null
92 fun extern_name
: NitType
94 if is_wrapped
then return new NitType.with_module
(find_extern_class
.as(not null).first
, find_extern_class
.as(not null).second
)
97 if is_primitive_array
then
98 # Primitive arrays have a special naming convention
99 name
= "Native" + extern_class_name
.join
("").capitalized
+ "Array"
101 name
= "Native" + extern_class_name
.join
("")
104 var nit_type
= new NitType(name
)
105 nit_type
.is_complete
= false
109 fun to_cast
(jtype
: String, is_param
: Bool): String
112 return converter
.cast_as_param
(jtype
)
115 return converter
.cast_as_return
(jtype
)
118 redef fun to_s
: String
120 var id
= self.full_id
122 if self.is_primitive_array
then
123 for i
in [0..array_dimension
[ do
126 else if self.has_generic_params
then
127 var gen_list
= new Array[String]
129 for param
in generic_params
do
130 gen_list
.add
(param
.to_s
)
133 id
+= "<{gen_list.join(", ")}>"
139 # To fully qualified package name
140 # Cuts the primitive array `[]`
141 fun to_package_name
: String
146 return str
.substring
(0, len
- (2*array_dimension
))
149 fun resolve_types
(conversion_map
: HashMap[String, Array[String]])
151 if identifier
.length
== 1 then
152 var resolved_id
= conversion_map
.get_or_null
(self.id
)
153 if resolved_id
!= null then self.identifier
= new Array[String].from
(resolved_id
)
156 if self.has_generic_params
then
157 for params
in generic_params
do params
.resolve_types
(conversion_map
)
161 private fun extern_class_name
: Array[String]
163 var class_name
= new Array[String]
164 class_name
.add
(self.id
)
166 if not self.has_generic_params
then return class_name
170 for param
in generic_params
do class_name
.add_all param
.extern_class_name
175 # Search inside `lib/android` directory for already wrapped classes
176 # If found, contains the class identifier and the Nit Module name
177 var find_extern_class
: nullable Couple[String, NitModule] is lazy
do
179 var regex
= "extern class [a-zA-Z1-9]\\\+[ ]\\\+in[ ]\\\+\"Java\
"[ ]*`\{[ ]*" + self.to_s + "\\\+[ ]*`\}"
180 var nit_dir
= "NIT_DIR".environ
181 var grep
= new IProcess("grep", "-r", regex
, nit_dir
/"lib/android/", nit_dir
/"lib/java/")
182 var to_eat
= ["private", "extern", "class"]
184 var output
= grep
.read_line
186 var output_class
= output
.substring_from
(output
.index_of
(':') + 1)
187 var tokens
= output_class
.split
(" ")
191 for token
in tokens
do
192 if to_eat
.has
(token
) then continue
197 if nclass_name
== "" then return null
199 var str
= output
.substring
(0, output
.search
(".nit").from
)
200 str
= str
.substring_from
(str
.last_index_of
('/') + 1)
201 var mod
= new NitModule(str
)
203 return new Couple[String, NitModule](nclass_name
, mod
)
206 # Comparison based on fully qualified named and generic params
207 # Ignores primitive array so `a.b.c[][] == a.b.c`
210 if other
isa JavaType then
211 return self.repr
== other
.repr
216 redef fun hash
do return self.repr
.hash
218 private fun repr
: String
220 var id
= self.full_id
222 if self.has_generic_params
then
223 var gen_list
= new Array[String]
225 for param
in generic_params
do
226 gen_list
.add
(param
.to_s
)
229 id
+= "<{gen_list.join(", ")}>"
235 fun collections_list
: Array[String] is cached
do return ["List", "ArrayList", "LinkedList", "Vector", "Set", "SortedSet", "HashSet", "TreeSet", "LinkedHashSet", "Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
236 fun iterable
: Array[String] is cached
do return ["ArrayList", "Set", "HashSet", "LinkedHashSet", "LinkedList", "Stack", "TreeSet", "Vector"]
237 fun maps
: Array[String] is cached
do return ["Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
241 var identifier
: String
243 var generic_params
: nullable Array[NitType] = null
245 # If this NitType was found in `lib/android`, contains the module name to import
246 var mod
: nullable NitModule
248 # Returns `true` if all types have been successfully converted to Nit type
249 var is_complete
: Bool = true
251 fun has_generic_params
: Bool do return not generic_params
== null
252 fun maps
: Array[String] is cached
do return ["HashMap", "RBTreeMap"]
254 fun id
: String do return identifier
261 init with_generic_params
(id
: String, gen_params
: String...)
264 self.generic_params
= new Array[NitType]
265 for param
in gen_params
do self.generic_params
.add
new NitType(param
)
268 init with_module
(id
: String, mod
: NitModule)
274 fun is_map
: Bool do return maps
.has
(self.identifier
)
276 redef fun to_s
: String
278 var id
= self.identifier
280 if self.has_generic_params
then
281 var gen_list
= new Array[String]
283 for param
in generic_params
do
284 gen_list
.add
(param
.to_s
)
287 id
+= "[{gen_list.join(", ")}]"
295 var class_type
= new JavaType(new JavaTypeConverter)
296 var attributes
= new HashMap[String, JavaType]
298 # Methods of this class organized by their name
299 var methods
= new HashMap[String, Array[JavaMethod]]
301 var unknown_types
= new HashSet[JavaType]
302 var imports
= new HashSet[NitModule]
304 fun add_method
(id
: String, return_type
: JavaType, params
: Array[JavaType])
306 var signatures
= methods
.get_or_default
(id
, new Array[JavaMethod])
307 signatures
.add
(new JavaMethod(return_type
, new Array[JavaType].from
(params
)))
308 methods
[id
] = signatures
312 # A Java method, with its signature
314 # Type returned by the method
315 var return_type
: JavaType
317 # Type of the arguments of the method
318 var params
: Array[JavaType]
321 # A Nit module, use to import the referenced extern classes
326 redef fun ==(other
): Bool do return self.to_s
== other
.to_s
327 redef fun to_s
: String do return self.name
328 redef fun hash
: Int do return self.name
.hash