+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
+
+ # Option to set `extern_class_prefix`
+ var opt_extern_class_prefix = new OptionString("Prefix to extern classes (By default uses the full namespace)", "-p")
+
+ # Prefix used to name extern classes, if `null` use the full namespace
+ var extern_class_prefix: nullable String is lazy do return opt_extern_class_prefix.value
+end
+
+redef class Text
+ # Get a copy of `self` where the first letter is capitalized
+ fun simple_capitalized: String
+ do
+ if is_empty then return to_s
+
+ var c = chars.first.to_upper
+ var s = c.to_s + substring_from(1)
+ return s
+ end