import jtype_converter
class JavaType
- private var converter: JavaTypeConverter
var identifier = new Array[String]
var generic_params: nullable Array[JavaType] = null
+
+ # Is this a void return type?
var is_void = false
+ # Is this type a vararg?
+ var is_vararg = false is writable
+
# Has some generic type to be resolved (T extends foo => T is resolved to foo)
var has_unresolved_types = false
fun full_id: String do return identifier.join(".")
fun id: String do return identifier.last.replace("$", "")
- init(converter: JavaTypeConverter) do self.converter = converter
-
fun return_cast: String do return converter.cast_as_return(self.id)
fun param_cast: String
nit_type = new NitType(type_id)
end
- if not self.has_generic_params then return nit_type
-
- nit_type.generic_params = new Array[NitType]
-
- for param in generic_params do
- var nit_param = param.to_nit_type
-
- nit_type.generic_params.add(nit_param)
-
- if not nit_param.is_complete then nit_type.is_complete = false
- end
-
return nit_type
end
fun is_collection: Bool do return is_primitive_array or collections_list.has(self.id)
- fun is_wrapped: Bool do return find_extern_class != null
+ fun is_wrapped: Bool do return find_extern_class[full_id] != null
fun extern_name: NitType
do
- if is_wrapped then return new NitType.with_module(find_extern_class.as(not null).first, find_extern_class.as(not null).second)
+ var nit_type = find_extern_class[full_id]
+ if nit_type != null then return nit_type
var name
if is_primitive_array then
# Primitive arrays have a special naming convention
- name = "Native" + extern_class_name.join.capitalized + "Array"
+ name = "Java" + extern_class_name.join.capitalized + "Array"
else
- name = "Native" + extern_class_name.join
+ name = "Java" + extern_class_name.join
end
+ name = name.replace("-", "_")
+
var nit_type = new NitType(name)
nit_type.is_complete = false
return nit_type
return class_name
end
- # Search inside `lib/android` directory for already wrapped classes
- # If found, contains the class identifier and the Nit Module name
- var find_extern_class: nullable Couple[String, NitModule] is lazy do
-
- var regex = "extern class [a-zA-Z1-9]\\\+[ ]\\\+in[ ]\\\+\"Java\"[ ]*`\{[ ]*" + self.to_s + "\\\+[ ]*`\}"
- var nit_dir = "NIT_DIR".environ
- var grep = new ProcessReader("grep", "-r", regex, nit_dir/"lib/android/", nit_dir/"lib/java/")
- var to_eat = ["private", "extern", "class"]
-
- var output = grep.read_line
-
- var output_class = output.substring_from(output.index_of(':') + 1)
- var tokens = output_class.split(" ")
-
- var nclass_name = ""
-
- for token in tokens do
- if to_eat.has(token) then continue
- nclass_name = token
- break
- end
-
- if nclass_name == "" then return null
-
- var str = output.substring(0, output.search(".nit").from)
- str = str.substring_from(str.last_index_of('/') + 1)
- var mod = new NitModule(str)
-
- return new Couple[String, NitModule](nclass_name, mod)
- end
-
# Comparison based on fully qualified named and generic params
# Ignores primitive array so `a.b.c[][] == a.b.c`
- redef fun ==(other)
- do
- if other isa JavaType then
- return self.repr == other.repr
- end
- return false
- end
-
- redef fun hash do return self.repr.hash
-
- private fun repr: String
- do
- var id = self.full_id
-
- if self.has_generic_params then
- var gen_list = new Array[String]
-
- for param in generic_params do
- gen_list.add(param.to_s)
- end
-
- id += "<{gen_list.join(", ")}>"
- end
+ redef fun ==(other) do return other isa JavaType and self.full_id == other.full_id
- return id
- end
+ redef fun hash do return self.full_id.hash
var collections_list: Array[String] is lazy do return ["List", "ArrayList", "LinkedList", "Vector", "Set", "SortedSet", "HashSet", "TreeSet", "LinkedHashSet", "Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
var iterable: Array[String] is lazy do return ["ArrayList", "Set", "HashSet", "LinkedHashSet", "LinkedList", "Stack", "TreeSet", "Vector"]
end
class NitType
+ # Nit class name
var identifier: String
- var arg_id: String
- var generic_params: nullable Array[NitType] = null
# If this NitType was found in `lib/android`, contains the module name to import
var mod: nullable NitModule
# Returns `true` if all types have been successfully converted to Nit type
var is_complete: Bool = true
- fun has_generic_params: Bool do return not generic_params == null
-
- fun id: String do return identifier
-
- init (id: String)
- do
- self.identifier = id
- end
-
- init with_generic_params(id: String, gen_params: String...)
- do
- self.init(id)
- self.generic_params = new Array[NitType]
- for param in gen_params do self.generic_params.add new NitType(param)
- end
-
- init with_module(id: String, mod: NitModule)
- do
- self.init(id)
- self.mod = mod
- end
-
- redef fun to_s: String
- do
- var id = self.identifier
-
- if self.has_generic_params then
- var gen_list = new Array[String]
-
- for param in generic_params do
- gen_list.add(param.to_s)
- end
-
- id += "[{gen_list.join(", ")}]"
- end
-
- return id
- end
+ redef fun to_s do return identifier
end
# Model of a single Java class
class JavaClass
# Type of this class
- var class_type = new JavaType(new JavaTypeConverter)
+ var class_type: JavaType
# Attributes of this class
var attributes = new HashMap[String, JavaType]
# Methods of this class organized by their name
var methods = new MultiHashMap[String, JavaMethod]
+ # Constructors of this class
+ var constructors = new Array[JavaConstructor]
+
# Importations from this class
var imports = new HashSet[NitModule]
+
+ redef fun to_s do return class_type.to_s
end
# Model of all the Java class analyzed in one run
var unknown_types = new HashSet[JavaType]
# All analyzed classes
- var classes = new Array[JavaClass]
+ var classes = new HashMap[String, JavaClass]
+
+ # Add a class in `classes`
+ fun add_class(jclass: JavaClass)
+ do
+ var key = jclass.class_type.full_id
+ classes[key] = jclass
+ end
end
# A Java method, with its signature
var params: Array[JavaType]
end
+# A Java method, with its signature
+class JavaConstructor
+ # Type of the parameters of this constructor
+ var params: Array[JavaType]
+end
+
# A Nit module, use to import the referenced extern classes
class NitModule
+ # Relative path to the module
+ var path: String
+
# Name of the module
- var name: String
+ var name: String is lazy do return path.basename(".nit")
- redef fun ==(other): Bool do return self.to_s == other.to_s
- redef fun to_s: String do return self.name
- redef fun hash: Int do return self.name.hash
+ redef fun to_s do return self.name
+ redef fun ==(other) do return other isa NitModule and self.path == other.path
+ redef fun hash do return self.path.hash
+end
+
+redef class Sys
+ # Collection of Java classes already wrapped in the library
+ #
+ # * The key is from `JavaType.full_id`.
+ # * The value is the corresponding `NitType`.
+ var find_extern_class: DefaultMap[String, nullable NitType] is lazy do
+ var map = new DefaultMap[String, nullable NitType](null)
+ var modules = new HashMap[String, NitModule]
+
+ var nit_dir = "NIT_DIR".environ
+ if nit_dir.is_empty then
+ # Simple heuristic to find the Nit lib
+ var dir = sys.program_name.dirname / "../../../"
+ nit_dir = dir.simplify_path
+ if not nit_dir.file_exists then return map
+ end
+
+ # Use grep to find all extern classes implemented in Java
+ var grep_regex = "extern class [a-zA-Z0-9]\\\+[ ]\\\+in[ ]\\\+\"Java\""
+ var grep_args = ["-r", grep_regex,
+ nit_dir/"lib/android/",
+ nit_dir/"lib/java/"]
+
+ var grep = new ProcessReader("grep", grep_args...)
+ var lines = grep.read_lines
+ grep.close
+ grep.wait
+
+ # Sort out the modules, Nit class names and Java types
+ var regex = """(.+):\\s*extern +class +([a-zA-Z0-9]+) *in *"Java" *`\\{ *([a-zA-Z0-9.$/]+) *`\\}""".to_re
+ for line in lines do
+ var matches = line.search_all(regex)
+ for match in matches do
+ var path = match[1].to_s
+ var nit_name = match[2].to_s
+ var java_name = match[3].to_s
+
+ # Debug code
+ # print "+ Found {nit_name}:{java_name} at {path}"
+
+ var mod = modules.get_or_null(path)
+ if mod == null then
+ mod = new NitModule(path)
+ modules[path] = mod
+ end
+
+ map[java_name] = new NitType(nit_name, mod)
+ end
+ end
+
+ return map
+ end
end