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 and generic params
125 # Ignores primitive array so `a.b.c[][] == a.b.c`
126 redef fun ==(other
) do return other
isa JavaType and self.full_id
== other
.full_id
128 redef fun hash
do return self.full_id
.hash
133 var identifier
: String
135 # If this NitType was found in `lib/android`, contains the module name to import
136 var mod
: nullable NitModule
138 # Is this type known, wrapped and available in Nit?
139 var is_known
: Bool = true
141 redef fun to_s
do return identifier
144 # Model of a single Java class
147 var class_type
: JavaType
149 # Attributes of this class
150 var attributes
= new HashMap[String, JavaType]
152 # Methods of this class organized by their name
153 var methods
= new MultiHashMap[String, JavaMethod]
155 # Constructors of this class
156 var constructors
= new Array[JavaConstructor]
158 # Importations from this class
159 var imports
= new HashSet[NitModule]
161 redef fun to_s
do return class_type
.to_s
164 # Model of all the Java class analyzed in one run
167 # All analyzed classes
168 var classes
= new HashMap[String, JavaClass]
170 # Add a class in `classes`
171 fun add_class
(jclass
: JavaClass)
173 var key
= jclass
.class_type
.full_id
174 classes
[key
] = jclass
177 # Unknown types, not already wrapped and not in this pass
178 private var unknown_types
= new HashMap[JavaType, NitType]
180 # Wrapped types, or classes analyzed in this pass
181 private var known_types
= new HashMap[JavaType, NitType]
183 # Get the `NitType` corresponding to the `JavaType`
185 # Also registers types so they can be reused and
186 # to keep track of unknown types.
187 fun java_to_nit_type
(jtype
: JavaType): NitType
190 if known_types
.keys
.has
(jtype
) then return known_types
[jtype
]
191 if unknown_types
.keys
.has
(jtype
) then return unknown_types
[jtype
]
193 # Is it a compatible primitive type?
194 if not jtype
.is_primitive_array
then
195 var name
= converter
.to_nit_type
(jtype
.id
)
197 # We got a Nit equivalent
198 var nit_type
= new NitType(name
)
199 known_types
[jtype
] = nit_type
204 # Is being wrapped in this pass?
205 var key
= jtype
.full_id
206 if classes
.keys
.has
(key
) then
207 var nit_type
= new NitType(jtype
.extern_name
)
208 known_types
[jtype
] = nit_type
214 var nit_type
= find_extern_class
[jtype
.full_id
]
215 if nit_type
!= null then
216 known_types
[jtype
] = nit_type
221 nit_type
= new NitType(jtype
.extern_name
)
222 nit_type
.is_known
= false
223 unknown_types
[jtype
] = nit_type
228 # A Java method, with its signature
230 # Type returned by the method
231 var return_type
: JavaType
233 # Type of the arguments of the method
234 var params
: Array[JavaType]
237 # A Java method, with its signature
238 class JavaConstructor
239 # Type of the parameters of this constructor
240 var params
: Array[JavaType]
243 # A Nit module, use to import the referenced extern classes
245 # Relative path to the module
249 var name
: String is lazy
do return path
.basename
(".nit")
251 redef fun to_s
do return self.name
252 redef fun ==(other
) do return other
isa NitModule and self.path
== other
.path
253 redef fun hash
do return self.path
.hash
257 # Collection of Java classes already wrapped in the library
259 # * The key is from `JavaType.full_id`.
260 # * The value is the corresponding `NitType`.
261 var find_extern_class
: DefaultMap[String, nullable NitType] is lazy
do
262 var map
= new DefaultMap[String, nullable NitType](null)
263 var modules
= new HashMap[String, NitModule]
265 var nit_dir
= "NIT_DIR".environ
266 if nit_dir
.is_empty
then
267 # Simple heuristic to find the Nit lib
268 var dir
= sys
.program_name
.dirname
/ "../../../"
269 nit_dir
= dir
.simplify_path
270 if not nit_dir
.file_exists
then return map
273 # Use grep to find all extern classes implemented in Java
274 var grep_regex
= "extern class [a-zA-Z0-9]\\\+[ ]\\\+in[ ]\\\+\"Java\
""
275 var grep_args
= ["-r", grep_regex
,
276 nit_dir
/"lib/android/",
279 var grep
= new ProcessReader("grep", grep_args
...)
280 var lines
= grep
.read_lines
284 # Sort out the modules, Nit class names and Java types
285 var regex
= """(.+):\\s*extern +class +([a-zA-Z0-9]+) *in *"Java" *`\\{ *([a-zA-Z0-9.$/]+) *`\\}""".to_re
287 var matches
= line
.search_all
(regex
)
288 for match
in matches
do
289 var path
= match
[1].to_s
290 var nit_name
= match
[2].to_s
291 var java_name
= match
[3].to_s
294 # print "+ Found {nit_name}:{java_name} at {path}"
296 var mod
= modules
.get_or_null
(path
)
298 mod
= new NitModule(path
)
302 map
[java_name
] = new NitType(nit_name
, mod
)
309 # Option to set `extern_class_prefix`
310 var opt_extern_class_prefix
= new OptionString("Prefix to extern classes (By default uses the full namespace)", "-p")
312 # Prefix used to name extern classes, if `null` use the full namespace
313 var extern_class_prefix
: nullable String is lazy
do return opt_extern_class_prefix
.value
317 # Get a copy of `self` where the first letter is capitalized
318 fun simple_capitalized
: String
320 if is_empty
then return to_s
322 var c
= chars
.first
.to_upper
323 var s
= c
.to_s
+ substring_from
(1)