Skip to content

[generator] Fix issue where generic types couldn't be found in SymbolTable (#543) #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/generator-Tests/Unit-Tests/SymbolTableTests.cs
Original file line number Diff line number Diff line change
@@ -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<Java.Util.Locale.LanguageRange>").FullName);
Assert.AreEqual ("System.Collections.Generic.IList`1", table.Lookup ("System.Collections.Generic.IList<List<Java.Util.Locale.LanguageRange>>").FullName);

Assert.AreEqual ("System.Collections.Generic.IDictionary`2", table.Lookup ("System.Collections.Generic.IDictionary<string, Java.Util.Locale.LanguageRange>").FullName);
Assert.AreEqual ("System.Collections.Generic.IDictionary`2", table.Lookup ("System.Collections.Generic.IDictionary<string, List<Java.Util.Locale.LanguageRange>>").FullName);

Assert.AreEqual ("System.Collections.Generic.IList`1", table.Lookup ("System.Collections.Generic.IList<Dictionary<string, Java.Util.Locale.LanguageRange>>").FullName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,15 @@ public ISymbol Lookup (string java_type)
if (all_symbols_cache == null)
all_symbols_cache = new ConcurrentDictionary<string, ISymbol> (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<Java.Util.Locale.LanguageRange>
// 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;
Expand Down Expand Up @@ -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) {
Expand Down