contrib/jwrapper: skip wrapping anonymous classes
[nit.git] / contrib / jwrapper / src / model.nit
index 656d092..50d54f1 100644 (file)
@@ -26,7 +26,12 @@ import jtype_converter
 class JavaType
        super Cloneable
 
+       # Identifiers composing the namespace and class name
+       #
+       # An array of all the names that would be separated by `.`.
+       # Each name may contain `$`.
        var identifier = new Array[String]
+
        var generic_params: nullable Array[JavaType] = null
 
        # Is this a void return type?
@@ -35,6 +40,16 @@ class JavaType
        # Is this type a vararg?
        var is_vararg = false is writable
 
+       # Is this type based on an anonymous class?
+       var is_anonymous: Bool is lazy do
+               for id in identifier do
+                       for part in id.split("$") do
+                               if part.chars.first.is_digit then return true
+                       end
+               end
+               return false
+       end
+
        # Has some generic type to be resolved (T extends foo => T is resolved to foo)
        var has_unresolved_types = false
 
@@ -44,8 +59,6 @@ class JavaType
        fun is_primitive_array: Bool do return array_dimension > 0
 
        fun has_generic_params: Bool do return not generic_params == null
-       fun full_id: String do return identifier.join(".")
-       fun id: String do return identifier.last.replace("$", "")
 
        fun return_cast: String do return converter.cast_as_return(self.id)
 
@@ -82,14 +95,27 @@ class JavaType
                        name = prefix + id
                end
 
+               if is_primitive_array then
+                       name += "_" + "Array" * array_dimension
+               end
+
                name = name.replace("-", "_")
                name = name.replace("$", "_")
                return name
        end
 
-       redef fun to_s
-       do
-               var id = self.full_id
+       # Short name of the class, mangled to remove `$` (e.g. `Set`)
+       fun id: String do return identifier.last.replace("$", "")
+
+       # Full name of this class as used in an importation (e.g. `java.lang.Set`)
+       fun package_name: String do return identifier.join(".")
+
+       # Name of this class for the extern declaration in Nit (e.g. `java.lang.Set[]`)
+       fun extern_equivalent: String do return package_name + "[]" * array_dimension
+
+       # Full name of this class with arrays and generic values (e.g. `java.lang.Set<E>[]`)
+       redef fun to_s do
+               var id = self.package_name
 
                if self.is_primitive_array then
                        id += "[]" * array_dimension
@@ -101,28 +127,6 @@ class JavaType
                return id
        end
 
-       # To fully qualified package name
-       # Cuts the primitive array `[]`
-       fun to_package_name: String
-       do
-               var str = self.to_s
-               var len = str.length
-
-               return str.substring(0, len - (2*array_dimension))
-       end
-
-       fun resolve_types(conversion_map: HashMap[String, Array[String]])
-       do
-               if identifier.length == 1 then
-                       var resolved_id = conversion_map.get_or_null(self.id)
-                       if resolved_id != null then self.identifier = new Array[String].from(resolved_id)
-               end
-
-               if self.has_generic_params then
-                       for params in generic_params do params.resolve_types(conversion_map)
-               end
-       end
-
        # Get a copy of `self`
        redef fun clone
        do
@@ -137,10 +141,10 @@ class JavaType
 
        # 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
+               self.package_name == other.package_name and
+               self.array_dimension == other.array_dimension
 
-       redef fun hash do return self.full_id.hash
+       redef fun hash do return self.package_name.hash
 end
 
 class NitType
@@ -174,6 +178,58 @@ class JavaClass
        var imports = new HashSet[NitModule]
 
        redef fun to_s do return class_type.to_s
+
+       # Resolve the types in `other` in the context of this class
+       private fun resolve_types_of(other: JavaClass)
+       do
+               # Methods
+               for mid, method in other.methods do
+                       for signature in method do
+                               self.resolve(signature.return_type, signature.generic_params)
+                               for param in signature.params do self.resolve(param, signature.generic_params)
+                       end
+               end
+
+               # Constructors
+               for signature in other.constructors do
+                       for param in signature.params do self.resolve(param, signature.generic_params)
+               end
+
+               # Attributes
+               for aid, attribute in other.attributes do
+                       self.resolve attribute.java_type
+               end
+       end
+
+       # Resolve `java_type` in the context of this class
+       #
+       # Replace, in place, parameter types by their bound.
+       private fun resolve(java_type: JavaType, property_generic_params: nullable Array[JavaType])
+       do
+               # Skip types with a full package name
+               if java_type.identifier.length != 1 then return
+
+               # Skip primitive types
+               if converter.type_map.keys.has(java_type.id) then return
+
+               # Gather the generic parameters of the method, then the class
+               var params = new Array[JavaType]
+               if property_generic_params != null then params.add_all property_generic_params
+               var class_generic_params = class_type.generic_params
+               if class_generic_params != null then params.add_all class_generic_params
+
+               # Skip if there is not parameters usable to resolve
+               if params.is_empty then return
+
+               for param in params do
+                       if param.identifier == java_type.identifier then
+                               # Found a marching parameter type
+                               # TODO use a more precise bound
+                               java_type.identifier = ["java", "lang", "Object"]
+                               return
+                       end
+               end
+       end
 end
 
 # Model of all the Java class analyzed in one run
@@ -185,15 +241,15 @@ class JavaModel
        # Add a class in `classes`
        fun add_class(jclass: JavaClass)
        do
-               var key = jclass.class_type.full_id
+               var key = jclass.class_type.package_name
                classes[key] = jclass
        end
 
        # Unknown types, not already wrapped and not in this pass
-       private var unknown_types = new HashMap[JavaType, NitType]
+       var unknown_types = new HashMap[JavaType, NitType]
 
        # Wrapped types, or classes analyzed in this pass
-       private var known_types = new HashMap[JavaType, NitType]
+       var known_types = new HashMap[JavaType, NitType]
 
        # Get the `NitType` corresponding to the `JavaType`
        #
@@ -217,16 +273,17 @@ class JavaModel
                end
 
                # Is being wrapped in this pass?
-               var key = jtype.full_id
+               var key = jtype.package_name
                if classes.keys.has(key) then
-                       var nit_type = new NitType(jtype.extern_name)
-                       known_types[jtype] = nit_type
-
-                       return nit_type
+                       if jtype.array_dimension <= opt_arrays.value then
+                               var nit_type = new NitType(jtype.extern_name)
+                               known_types[jtype] = nit_type
+                               return nit_type
+                       end
                end
 
                # Search in lib
-               var nit_type = find_extern_class[jtype.full_id]
+               var nit_type = find_extern_class[jtype.extern_equivalent]
                if nit_type != null then
                        known_types[jtype] = nit_type
                        return nit_type
@@ -238,6 +295,24 @@ class JavaModel
                unknown_types[jtype] = nit_type
                return nit_type
        end
+
+       # Resolve the types in methods and attributes of this class
+       fun resolve_types
+       do
+               for id, java_class in classes do
+                       java_class.resolve_types_of java_class
+
+                       # Ask nester classes for resolve too
+                       var matches = id.search_all("$")
+                       for match in matches do
+                               var nester_name = id.substring(0, match.from)
+                               if classes.keys.has(nester_name) then
+                                       var nester = classes[nester_name]
+                                       nester.resolve_types_of java_class
+                               end
+                       end
+               end
+       end
 end
 
 # A property to a Java class
@@ -256,6 +331,9 @@ class JavaMethod
 
        # Type of the arguments of the method
        var params: Array[JavaType]
+
+       # Generic parameters of this method
+       var generic_params: Array[JavaType]
 end
 
 # An attribute in a Java class
@@ -270,6 +348,9 @@ end
 class JavaConstructor
        # Type of the parameters of this constructor
        var params: Array[JavaType]
+
+       # Generic parameters of this constructor
+       var generic_params: Array[JavaType]
 end
 
 # A Nit module, use to import the referenced extern classes
@@ -288,7 +369,7 @@ end
 redef class Sys
        # Collection of Java classes already wrapped in the library
        #
-       # * The key is from `JavaType.full_id`.
+       # * 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)
@@ -321,16 +402,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
@@ -353,6 +434,9 @@ redef class Sys
 
        # 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")
+
+       # Generate the primitive array version of each class up to the given depth
+       var opt_arrays = new OptionInt("Depth of the primitive array for each wrapped class (default: 1)", 1, "-a")
 end
 
 redef class Text