contrib/jwrapper: accept [] (and anything) in search for existing wrappers
[nit.git] / contrib / jwrapper / src / model.nit
index 6200f28..011d0a8 100644 (file)
 module model
 
 import more_collections
+import opts
 
 import jtype_converter
 
 class JavaType
+       super Cloneable
+
        var identifier = new Array[String]
        var generic_params: nullable Array[JavaType] = null
 
@@ -55,56 +58,33 @@ class JavaType
                return converter.cast_as_param(self.id)
        end
 
-       fun to_nit_type: NitType
+       # Name to give an extern class wrapping this type
+       fun extern_name: String
        do
-               var nit_type: NitType
-               var type_id = null
-
-               if not is_primitive_array then
-                       type_id = converter.to_nit_type(self.id)
-               end
-
-               if type_id == null then
-                       nit_type = self.extern_name
-                       nit_type.is_complete = false
-               else
-                       nit_type = new NitType(type_id)
-               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[full_id] != null
-
-       fun extern_name: NitType
-       do
-               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 = "Java" + extern_class_name.join.capitalized + "Array"
+               var prefix = extern_class_prefix
+               if prefix == null then
+                       # Use the namespace, e.g. java.lang.String -> Java_lang_String
+                       assert not identifier.is_empty
+                       if identifier.length == 1 then
+                               name = identifier.last
+                       else
+                               var first = identifier.first
+                               var last = identifier.last
+                               var mid = identifier.subarray(1, identifier.length-2)
+                               name = first.simple_capitalized + "_"
+                               if mid.not_empty then name += mid.join("_") + "_"
+                               name += last
+                       end
                else
-                       name = "Java" + extern_class_name.join
+                       # Use the prefix and the short class name
+                       # e.g. given the prefix Native: java.lang.String -> NativeString
+                       name = prefix + id
                end
 
                name = name.replace("-", "_")
-
-               var nit_type = new NitType(name)
-               nit_type.is_complete = false
-               return nit_type
-       end
-
-       fun to_cast(jtype: String, is_param: Bool): String
-       do
-               if is_param then
-                       return converter.cast_as_param(jtype)
-               end
-
-               return converter.cast_as_return(jtype)
+               name = name.replace("$", "_")
+               return name
        end
 
        redef fun to_s
@@ -143,29 +123,24 @@ class JavaType
                end
        end
 
-       private fun extern_class_name: Array[String]
+       # Get a copy of `self`
+       redef fun clone
        do
-               var class_name = new Array[String]
-               class_name.add(self.id)
-
-               if not self.has_generic_params then return class_name
-
-               class_name.add "Of"
-
-               for param in generic_params do class_name.add_all param.extern_class_name
-
-               return class_name
+               var jtype = new JavaType
+               jtype.identifier = identifier
+               jtype.generic_params = generic_params
+               jtype.is_void = is_void
+               jtype.is_vararg = is_vararg
+               jtype.array_dimension = array_dimension
+               return jtype
        end
 
-       # Comparison based on fully qualified named and generic params
-       # Ignores primitive array so `a.b.c[][] == a.b.c`
-       redef fun ==(other) do return other isa JavaType and self.full_id == other.full_id
+       # Comparison based on fully qualified named
+       redef fun ==(other) do return other isa JavaType and
+               self.full_id == other.full_id and
+               self.is_primitive_array == other.is_primitive_array
 
        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"]
-       var maps: Array[String] is lazy do return ["Map", "SortedMap", "HashMap", "TreeMap", "Hashtable", "LinkedHashMap"]
 end
 
 class NitType
@@ -175,8 +150,8 @@ class NitType
        # 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
+       # Is this type known, wrapped and available in Nit?
+       var is_known: Bool = true
 
        redef fun to_s do return identifier
 end
@@ -187,7 +162,7 @@ class JavaClass
        var class_type: JavaType
 
        # Attributes of this class
-       var attributes = new HashMap[String, JavaType]
+       var attributes = new HashMap[String, JavaAttribute]
 
        # Methods of this class organized by their name
        var methods = new MultiHashMap[String, JavaMethod]
@@ -203,8 +178,6 @@ end
 
 # Model of all the Java class analyzed in one run
 class JavaModel
-       # Unknown Java types used in `classes`
-       var unknown_types = new HashSet[JavaType]
 
        # All analyzed classes
        var classes = new HashMap[String, JavaClass]
@@ -215,10 +188,69 @@ class JavaModel
                var key = jclass.class_type.full_id
                classes[key] = jclass
        end
+
+       # Unknown types, not already wrapped and not in this pass
+       private var unknown_types = new HashMap[JavaType, NitType]
+
+       # Wrapped types, or classes analyzed in this pass
+       private var known_types = new HashMap[JavaType, NitType]
+
+       # Get the `NitType` corresponding to the `JavaType`
+       #
+       # Also registers types so they can be reused and
+       # to keep track of unknown types.
+       fun java_to_nit_type(jtype: JavaType): NitType
+       do
+               # Check cache
+               if known_types.keys.has(jtype) then return known_types[jtype]
+               if unknown_types.keys.has(jtype) then return unknown_types[jtype]
+
+               # Is it a compatible primitive type?
+               if not jtype.is_primitive_array then
+                       var name = converter.to_nit_type(jtype.id)
+                       if name != null then
+                               # We got a Nit equivalent
+                               var nit_type = new NitType(name)
+                               known_types[jtype] = nit_type
+                               return nit_type
+                       end
+               end
+
+               # Is being wrapped in this pass?
+               var key = jtype.full_id
+               if classes.keys.has(key) then
+                       var nit_type = new NitType(jtype.extern_name)
+                       known_types[jtype] = nit_type
+
+                       return nit_type
+               end
+
+               # Search in lib
+               var nit_type = find_extern_class[jtype.full_id]
+               if nit_type != null then
+                       known_types[jtype] = nit_type
+                       return nit_type
+               end
+
+               # Unknown type
+               nit_type = new NitType(jtype.extern_name)
+               nit_type.is_known = false
+               unknown_types[jtype] = nit_type
+               return nit_type
+       end
+end
+
+# A property to a Java class
+abstract class JavaProperty
+
+       # Is this property marked static?
+       var is_static: Bool
 end
 
 # A Java method, with its signature
 class JavaMethod
+       super JavaProperty
+
        # Type returned by the method
        var return_type: JavaType
 
@@ -226,6 +258,14 @@ class JavaMethod
        var params: Array[JavaType]
 end
 
+# An attribute in a Java class
+class JavaAttribute
+       super JavaProperty
+
+       # Type of the attribute
+       var java_type: JavaType
+end
+
 # A Java method, with its signature
 class JavaConstructor
        # Type of the parameters of this constructor
@@ -254,19 +294,26 @@ redef class Sys
                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
+               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", grep_regex,
-                       nit_dir/"lib/android/",
-                       nit_dir/"lib/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
@@ -274,16 +321,16 @@ redef class Sys
                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
+               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
+                               var java_name = match[3].to_s.trim
 
                                # Debug code
-                               # print "+ Found {nit_name}:{java_name} at {path}"
+                               # print "+ Found {nit_name}: {java_name} at {path}"
 
                                var mod = modules.get_or_null(path)
                                if mod == null then
@@ -297,4 +344,25 @@ redef class Sys
 
                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
+
+       # Libraries to search for existing wrappers
+       var opt_libs = new OptionArray("Paths to libraries with wrappers of Java classes ('auto' to use the full Nit lib)", "-i")
+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
 end