From 82a518bdf61cf967a2bba45a2f59232f80ce1d04 Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Fri, 3 Jan 2020 15:12:45 -0600 Subject: [PATCH 1/2] [generator] Fix issue where generic types couldn't be found in SymbolTable (#543) --- .../Unit-Tests/SymbolTableTests.cs | 38 ++++++++++++++++ .../Symbols/SymbolTable.cs | 45 ++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 tests/generator-Tests/Unit-Tests/SymbolTableTests.cs diff --git a/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs b/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs new file mode 100644 index 000000000..65c265934 --- /dev/null +++ b/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs @@ -0,0 +1,38 @@ +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.NotNull (table.Lookup ("System.Collections.Generic.IList")); + Assert.NotNull (table.Lookup ("System.Collections.Generic.IList>")); + + Assert.NotNull (table.Lookup ("System.Collections.Generic.IDictionary")); + Assert.NotNull (table.Lookup ("System.Collections.Generic.IDictionary>")); + } + } +} 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..f8de93cd6 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,40 @@ 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 (nested_count > 0) { + if (c == '>') { + nested_count--; + continue; + } + + if (c == '<') { + nested_count++; + continue; + } + + continue; + } + + if (c == ',') + arity++; + } + + return $"{key}`{arity}"; + } + public void Dump () { foreach (var p in symbols) { From 95698bcc3ddc08e3ef7c46168f5f6ebcc5b0fe1d Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Mon, 6 Jan 2020 14:16:42 -0600 Subject: [PATCH 2/2] Add additional test and fix exposed error in nested generic types. --- .../Unit-Tests/SymbolTableTests.cs | 10 ++++++---- .../Symbols/SymbolTable.cs | 18 +++++------------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs b/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs index 65c265934..58b714bb9 100644 --- a/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs +++ b/tests/generator-Tests/Unit-Tests/SymbolTableTests.cs @@ -28,11 +28,13 @@ public void FindGenericTypes () table.AddType (dict); - Assert.NotNull (table.Lookup ("System.Collections.Generic.IList")); - Assert.NotNull (table.Lookup ("System.Collections.Generic.IList>")); + 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.NotNull (table.Lookup ("System.Collections.Generic.IDictionary")); - Assert.NotNull (table.Lookup ("System.Collections.Generic.IDictionary>")); + 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 f8de93cd6..0ffbd8317 100644 --- a/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/SymbolTable.cs +++ b/tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/SymbolTable.cs @@ -311,21 +311,13 @@ private string AddArity (string key, string typeParams) typeParams = typeParams.Substring (1, typeParams.Length - 2); foreach (var c in typeParams) { - if (nested_count > 0) { - if (c == '>') { - nested_count--; - continue; - } - - if (c == '<') { - nested_count++; - continue; - } + if (c == '>') + nested_count--; - continue; - } + if (c == '<') + nested_count++; - if (c == ',') + if (nested_count == 0 && c == ',') arity++; }