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 fun is_collection
: Bool do return is_primitive_array
or collections_list
.has
(self.id
)
61 fun is_wrapped
: Bool do return find_extern_class
[full_id
] != null
63 # Name to give an extern class wrapping this type
64 fun extern_name
: String
67 var prefix
= extern_class_prefix
68 if prefix
== null then
69 # Use the namespace, e.g. java.lang.String -> Java_lang_String
70 assert not identifier
.is_empty
71 if identifier
.length
== 1 then
72 name
= identifier
.last
74 var first
= identifier
.first
75 var last
= identifier
.last
76 var mid
= identifier
.subarray
(1, identifier
.length-2
)
77 name
= first
.simple_capitalized
+ "_"
78 if mid
.not_empty
then name
+= mid
.join
("_") + "_"
82 # Use the prefix and the short class name
83 # e.g. given the prefix Native: java.lang.String -> NativeString
87 name
= name
.replace
("-", "_")
88 name
= name
.replace
("$", "_")
92 fun to_cast
(jtype
: String, is_param
: Bool): String
95 return converter
.cast_as_param
(jtype
)
98 return converter
.cast_as_return
(jtype
)
103 var id
= self.full_id
105 if self.is_primitive_array
then
106 id
+= "[]" * array_dimension
107 else if self.has_generic_params
then
108 var params
= [for param
in generic_params
do param
.to_s
]
109 id
+= "<{params.join(", ")}>"
115 # To fully qualified package name
116 # Cuts the primitive array `[]`
117 fun to_package_name
: String
122 return str
.substring
(0, len
- (2*array_dimension
))
125 fun resolve_types
(conversion_map
: HashMap[String, Array[String]])
127 if identifier
.length
== 1 then
128 var resolved_id
= conversion_map
.get_or_null
(self.id
)
129 if resolved_id
!= null then self.identifier
= new Array[String].from
(resolved_id
)
132 if self.has_generic_params
then
133 for params
in generic_params
do params
.resolve_types
(conversion_map
)
137 # Comparison based on fully qualified named and generic params
138 # Ignores primitive array so `a.b.c[][] == a.b.c`
139 redef fun ==(other
) do return other
isa JavaType and self.full_id
== other
.full_id
141 redef fun hash
do return self.full_id
.hash
143 var collections_list
: Array[String] is lazy
do return ["List", "ArrayList", "LinkedList", "Vector", "Set", "SortedSet", "HashSet", "TreeSet", "LinkedHashSet", "Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
144 var iterable
: Array[String] is lazy
do return ["ArrayList", "Set", "HashSet", "LinkedHashSet", "LinkedList", "Stack", "TreeSet", "Vector"]
145 var maps
: Array[String] is lazy
do return ["Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
150 var identifier
: String
152 # If this NitType was found in `lib/android`, contains the module name to import
153 var mod
: nullable NitModule
155 # Is this type known, wrapped and available in Nit?
156 var is_known
: Bool = true
158 redef fun to_s
do return identifier
161 # Model of a single Java class
164 var class_type
: JavaType
166 # Attributes of this class
167 var attributes
= new HashMap[String, JavaType]
169 # Methods of this class organized by their name
170 var methods
= new MultiHashMap[String, JavaMethod]
172 # Constructors of this class
173 var constructors
= new Array[JavaConstructor]
175 # Importations from this class
176 var imports
= new HashSet[NitModule]
178 redef fun to_s
do return class_type
.to_s
181 # Model of all the Java class analyzed in one run
184 # All analyzed classes
185 var classes
= new HashMap[String, JavaClass]
187 # Add a class in `classes`
188 fun add_class
(jclass
: JavaClass)
190 var key
= jclass
.class_type
.full_id
191 classes
[key
] = jclass
194 # Unknown types, not already wrapped and not in this pass
195 private var unknown_types
= new HashMap[JavaType, NitType]
197 # Wrapped types, or classes analyzed in this pass
198 private var known_types
= new HashMap[JavaType, NitType]
200 # Get the `NitType` corresponding to the `JavaType`
202 # Also registers types so they can be reused and
203 # to keep track of unknown types.
204 fun java_to_nit_type
(jtype
: JavaType): NitType
207 if known_types
.keys
.has
(jtype
) then return known_types
[jtype
]
208 if unknown_types
.keys
.has
(jtype
) then return unknown_types
[jtype
]
210 # Is it a compatible primitive type?
211 if not jtype
.is_primitive_array
then
212 var name
= converter
.to_nit_type
(jtype
.id
)
214 # We got a Nit equivalent
215 var nit_type
= new NitType(name
)
216 known_types
[jtype
] = nit_type
221 # Is being wrapped in this pass?
222 var key
= jtype
.full_id
223 if classes
.keys
.has
(key
) then
224 var nit_type
= new NitType(jtype
.extern_name
)
225 known_types
[jtype
] = nit_type
231 var nit_type
= find_extern_class
[jtype
.full_id
]
232 if nit_type
!= null then
233 known_types
[jtype
] = nit_type
238 nit_type
= new NitType(jtype
.extern_name
)
239 nit_type
.is_known
= false
240 unknown_types
[jtype
] = nit_type
245 # A Java method, with its signature
247 # Type returned by the method
248 var return_type
: JavaType
250 # Type of the arguments of the method
251 var params
: Array[JavaType]
254 # A Java method, with its signature
255 class JavaConstructor
256 # Type of the parameters of this constructor
257 var params
: Array[JavaType]
260 # A Nit module, use to import the referenced extern classes
262 # Relative path to the module
266 var name
: String is lazy
do return path
.basename
(".nit")
268 redef fun to_s
do return self.name
269 redef fun ==(other
) do return other
isa NitModule and self.path
== other
.path
270 redef fun hash
do return self.path
.hash
274 # Collection of Java classes already wrapped in the library
276 # * The key is from `JavaType.full_id`.
277 # * The value is the corresponding `NitType`.
278 var find_extern_class
: DefaultMap[String, nullable NitType] is lazy
do
279 var map
= new DefaultMap[String, nullable NitType](null)
280 var modules
= new HashMap[String, NitModule]
282 var nit_dir
= "NIT_DIR".environ
283 if nit_dir
.is_empty
then
284 # Simple heuristic to find the Nit lib
285 var dir
= sys
.program_name
.dirname
/ "../../../"
286 nit_dir
= dir
.simplify_path
287 if not nit_dir
.file_exists
then return map
290 # Use grep to find all extern classes implemented in Java
291 var grep_regex
= "extern class [a-zA-Z0-9]\\\+[ ]\\\+in[ ]\\\+\"Java\
""
292 var grep_args
= ["-r", grep_regex
,
293 nit_dir
/"lib/android/",
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
334 # Get a copy of `self` where the first letter is capitalized
335 fun simple_capitalized
: String
337 if is_empty
then return to_s
339 var c
= chars
.first
.to_upper
340 var s
= c
.to_s
+ substring_from
(1)