diff --git a/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs b/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs new file mode 100644 index 000000000..58b714bb9 --- /dev/null +++ b/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs @@ -0,0 +1,40 @@ +using System; +using MonoDroid.Generation; +using NUnit.Framework; + +namespace generatortests +{ + [TestFixture] + public class SymbolTableTests + { + [Test] + public void FindGenericTypes () + { + var table = new SymbolTable (); + + var list = new InterfaceGen (new GenBaseSupport { + Name = "System.Collections.Generic.IList`1", + FullName = "System.Collections.Generic.IList`1", + JavaSimpleName = "System.Collections.Generic.IList`1" + }); + + table.AddType (list); + + var dict = new InterfaceGen (new GenBaseSupport { + Name = "System.Collections.Generic.IDictionary`2", + FullName = "System.Collections.Generic.IDictionary`2", + JavaSimpleName = "System.Collections.Generic.IDictionary`2" + }); + + table.AddType (dict); + + Assert.AreEqual ("System.Collections.Generic.IList`1", table.Lookup ("System.Collections.Generic.IList").FullName); + Assert.AreEqual ("System.Collections.Generic.IList`1", table.Lookup ("System.Collections.Generic.IList>").FullName); + + Assert.AreEqual ("System.Collections.Generic.IDictionary`2", table.Lookup ("System.Collections.Generic.IDictionary").FullName); + Assert.AreEqual ("System.Collections.Generic.IDictionary`2", table.Lookup ("System.Collections.Generic.IDictionary>").FullName); + + Assert.AreEqual ("System.Collections.Generic.IList`1", table.Lookup ("System.Collections.Generic.IList>").FullName); + } + } +} diff --git a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/SymbolTable.cs b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/SymbolTable.cs index 5f6465209..0ffbd8317 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/SymbolTable.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/SymbolTable.cs @@ -260,7 +260,15 @@ public ISymbol Lookup (string java_type) if (all_symbols_cache == null) all_symbols_cache = new ConcurrentDictionary (symbols.Values.SelectMany (v => v).GroupBy (s => s.FullName).ToDictionary (s => s.Key, s => s.FirstOrDefault ())); - all_symbols_cache.TryGetValue (key, out sym); + if (!all_symbols_cache.TryGetValue (key, out sym)) { + // We may be looking for a type like: + // - System.Collections.Generic.IList + // Our key is "System.Collections.Generic.IList", but it's stored in + // the symbol table with the arity so we need to look for + // "System.Collections.Generic.IList`1" to find a match + key = AddArity (key, type_params); + all_symbols_cache.TryGetValue (key, out sym); + } } } ISymbol result; @@ -290,7 +298,32 @@ public ISymbol Lookup (string java_type) return result; } - + + private string AddArity (string key, string typeParams) + { + if (string.IsNullOrWhiteSpace (typeParams) || !typeParams.StartsWith ("<") || !typeParams.EndsWith (">")) + return key; + + var nested_count = 0; + var arity = 1; + + // Remove the outer <> + typeParams = typeParams.Substring (1, typeParams.Length - 2); + + foreach (var c in typeParams) { + if (c == '>') + nested_count--; + + if (c == '<') + nested_count++; + + if (nested_count == 0 && c == ',') + arity++; + } + + return $"{key}`{arity}"; + } + public void Dump () { foreach (var p in symbols) {