+ var name: String is lazy do return path.basename(".nit")
+
+ redef fun to_s do return self.name
+ redef fun ==(other) do return other isa NitModuleRef 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 uses `JavaType.to_s`.
+ # * 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, NitModuleRef]
+
+ var lib_paths = opt_libs.value
+ if lib_paths == null then lib_paths = new Array[String]
+
+ if lib_paths.has("auto") then
+ lib_paths.remove "auto"
+ 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 / "../../../lib/"
+ dir = dir.simplify_path
+ if dir.file_exists then lib_paths.add dir.simplify_path
+ end
+ end
+
+ if lib_paths.is_empty then return map
+
+ # 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", "--with-filename", grep_regex]
+ grep_args.add_all lib_paths
+
+ 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" *`\\{(.+)`\\}""".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.trim
+
+ # Debug code
+ # print "+ Found {nit_name}: {java_name} at {path}"
+
+ var mod = modules.get_or_null(path)
+ if mod == null then
+ mod = new NitModuleRef(path)
+ modules[path] = mod
+ end
+
+ map[java_name] = new NitType(nit_name, mod)
+ end
+ end
+
+ return map
+ end